mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0
This commit is contained in:
@@ -36,12 +36,6 @@
|
|||||||
<version>0.2.3</version>
|
<version>0.2.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.gitee.sop</groupId>
|
|
||||||
<artifactId>sop-registry-api</artifactId>
|
|
||||||
<version>2.0.0-SNAPSHOT</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- easyopen starter -->
|
<!-- easyopen starter -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.oschina.durcframework</groupId>
|
<groupId>net.oschina.durcframework</groupId>
|
||||||
@@ -55,6 +49,12 @@
|
|||||||
<version>1.8.1</version>
|
<version>1.8.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>${okhttp.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mysql</groupId>
|
<groupId>mysql</groupId>
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
@@ -18,6 +18,8 @@ import com.gitee.sop.adminserver.bean.ChannelMsg;
|
|||||||
import com.gitee.sop.adminserver.bean.MetadataEnum;
|
import com.gitee.sop.adminserver.bean.MetadataEnum;
|
||||||
import com.gitee.sop.adminserver.bean.NacosConfigs;
|
import com.gitee.sop.adminserver.bean.NacosConfigs;
|
||||||
import com.gitee.sop.adminserver.bean.ServiceGrayDefinition;
|
import com.gitee.sop.adminserver.bean.ServiceGrayDefinition;
|
||||||
|
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
||||||
|
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||||
import com.gitee.sop.adminserver.common.BizException;
|
import com.gitee.sop.adminserver.common.BizException;
|
||||||
import com.gitee.sop.adminserver.common.ChannelOperation;
|
import com.gitee.sop.adminserver.common.ChannelOperation;
|
||||||
import com.gitee.sop.adminserver.common.StatusEnum;
|
import com.gitee.sop.adminserver.common.StatusEnum;
|
||||||
@@ -26,9 +28,7 @@ import com.gitee.sop.adminserver.entity.ConfigGrayInstance;
|
|||||||
import com.gitee.sop.adminserver.mapper.ConfigGrayInstanceMapper;
|
import com.gitee.sop.adminserver.mapper.ConfigGrayInstanceMapper;
|
||||||
import com.gitee.sop.adminserver.mapper.ConfigGrayMapper;
|
import com.gitee.sop.adminserver.mapper.ConfigGrayMapper;
|
||||||
import com.gitee.sop.adminserver.service.ConfigPushService;
|
import com.gitee.sop.adminserver.service.ConfigPushService;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInfo;
|
import com.gitee.sop.adminserver.service.RegistryService;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
|
||||||
import com.gitee.sop.registryapi.service.RegistryService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
@@ -2,7 +2,7 @@ package com.gitee.sop.adminserver.api.service.param;
|
|||||||
|
|
||||||
import com.gitee.easyopen.doc.annotation.ApiDocField;
|
import com.gitee.easyopen.doc.annotation.ApiDocField;
|
||||||
import com.gitee.easyopen.util.CopyUtil;
|
import com.gitee.easyopen.util.CopyUtil;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
package com.gitee.sop.adminserver.bean;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
@@ -1,4 +1,4 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
package com.gitee.sop.adminserver.bean;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
package com.gitee.sop.adminserver.bean;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
@@ -1,12 +0,0 @@
|
|||||||
package com.gitee.sop.adminserver.config;
|
|
||||||
|
|
||||||
import com.gitee.sop.registryapi.config.BaseRegistryConfig;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class RegistryConfig extends BaseRegistryConfig {
|
|
||||||
|
|
||||||
}
|
|
@@ -9,6 +9,8 @@ import com.gitee.easyopen.ParamNames;
|
|||||||
import com.gitee.easyopen.interceptor.ApiInterceptor;
|
import com.gitee.easyopen.interceptor.ApiInterceptor;
|
||||||
import com.gitee.easyopen.session.ApiSessionManager;
|
import com.gitee.easyopen.session.ApiSessionManager;
|
||||||
import com.gitee.sop.adminserver.interceptor.LoginInterceptor;
|
import com.gitee.sop.adminserver.interceptor.LoginInterceptor;
|
||||||
|
import com.gitee.sop.adminserver.service.RegistryService;
|
||||||
|
import com.gitee.sop.adminserver.service.impl.RegistryServiceNacosImpl;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -65,6 +67,16 @@ public class WebConfig {
|
|||||||
return apiConfig;
|
return apiConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当配置了registry.name=nacos生效。没有配置同样生效
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
RegistryService registryServiceNacos() {
|
||||||
|
return new RegistryServiceNacosImpl();
|
||||||
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void after() {
|
public void after() {
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
package com.gitee.sop.registryapi.service;
|
package com.gitee.sop.adminserver.service;
|
||||||
|
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInfo;
|
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
package com.gitee.sop.registryapi.service.impl;
|
package com.gitee.sop.adminserver.service.impl;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
@@ -7,10 +7,10 @@ import com.alibaba.nacos.api.naming.NamingFactory;
|
|||||||
import com.alibaba.nacos.api.naming.NamingService;
|
import com.alibaba.nacos.api.naming.NamingService;
|
||||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
import com.gitee.sop.registryapi.bean.HttpTool;
|
import com.gitee.sop.adminserver.bean.HttpTool;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInfo;
|
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||||
import com.gitee.sop.registryapi.service.RegistryService;
|
import com.gitee.sop.adminserver.service.RegistryService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -29,9 +29,9 @@ import java.util.Map;
|
|||||||
* @author tanghc
|
* @author tanghc
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class RegistryServiceNacos implements RegistryService {
|
public class RegistryServiceNacosImpl implements RegistryService {
|
||||||
|
|
||||||
static HttpTool httpTool = new HttpTool();
|
private static HttpTool httpTool = new HttpTool();
|
||||||
|
|
||||||
@Value("${nacos.discovery.server-addr:${registry.nacos-server-addr:}}")
|
@Value("${nacos.discovery.server-addr:${registry.nacos-server-addr:}}")
|
||||||
private String nacosAddr;
|
private String nacosAddr;
|
||||||
@@ -40,9 +40,10 @@ public class RegistryServiceNacos implements RegistryService {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void after() throws NacosException {
|
public void after() throws NacosException {
|
||||||
if (StringUtils.isNotBlank(nacosAddr)) {
|
if (StringUtils.isBlank(nacosAddr)) {
|
||||||
namingService = NamingFactory.createNamingService(nacosAddr);
|
throw new IllegalArgumentException("请在配置文件中指定nacos.discovery.server-addr参数");
|
||||||
}
|
}
|
||||||
|
namingService = NamingFactory.createNamingService(nacosAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
@@ -32,9 +32,6 @@ spring.datasource.password=${mysql.password}
|
|||||||
easyopen.show-doc=false
|
easyopen.show-doc=false
|
||||||
easyopen.ignore-validate=true
|
easyopen.ignore-validate=true
|
||||||
|
|
||||||
# 固定不用改
|
|
||||||
registry.name=nacos
|
|
||||||
|
|
||||||
logging.level.com.gitee=debug
|
logging.level.com.gitee=debug
|
||||||
|
|
||||||
# 不用改
|
# 不用改
|
||||||
|
@@ -35,7 +35,6 @@
|
|||||||
<modules>
|
<modules>
|
||||||
<module>sop-gateway-common</module>
|
<module>sop-gateway-common</module>
|
||||||
<module>sop-service-common</module>
|
<module>sop-service-common</module>
|
||||||
<module>sop-registry-api</module>
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
# sop-common
|
# sop-common
|
||||||
|
|
||||||
- sop-gateway-common:提供给网关使用
|
- sop-gateway-common:提供给网关使用
|
||||||
- sop-registry-api:注册中心API
|
|
||||||
- sop-service-common:提供给微服务端使用,需要打成jar
|
- sop-service-common:提供给微服务端使用,需要打成jar
|
||||||
|
|
||||||
正式开发请将这些模块上传的maven私服
|
正式开发请将这些模块上传的maven私服
|
||||||
|
@@ -1,74 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<groupId>com.gitee.sop</groupId>
|
|
||||||
<artifactId>sop-registry-api</artifactId>
|
|
||||||
<version>2.0.0-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
<okhttp.version>3.11.0</okhttp.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.squareup.okhttp3</groupId>
|
|
||||||
<artifactId>okhttp</artifactId>
|
|
||||||
<version>${okhttp.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- nacos sdk -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.nacos</groupId>
|
|
||||||
<artifactId>nacos-client</artifactId>
|
|
||||||
<version>1.1.3</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-context</artifactId>
|
|
||||||
<version>5.1.6.RELEASE</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<version>1.18.6</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
|
||||||
<version>2.1.4.RELEASE</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.11</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<plugins>
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>3.1</version>
|
|
||||||
<configuration>
|
|
||||||
<source>${java.version}</source>
|
|
||||||
<target>${java.version}</target>
|
|
||||||
<encoding>UTF-8</encoding>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
</build>
|
|
||||||
|
|
||||||
</project>
|
|
@@ -1,3 +0,0 @@
|
|||||||
# sop-registry-api
|
|
||||||
|
|
||||||
统一各注册中心接口
|
|
@@ -1,14 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class EurekaApplication {
|
|
||||||
private String name;
|
|
||||||
private List<EurekaInstance> instance;
|
|
||||||
}
|
|
@@ -1,13 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class EurekaApplications {
|
|
||||||
private List<EurekaApplication> application;
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class EurekaApps {
|
|
||||||
private EurekaApplications applications;
|
|
||||||
}
|
|
@@ -1,70 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {
|
|
||||||
* "instance": {
|
|
||||||
* "instanceId": "demo-order2:11101",
|
|
||||||
* "app": "demo-order2",
|
|
||||||
* "appGroutName": null,
|
|
||||||
* "ipAddr": "127.0.0.1",
|
|
||||||
* "sid": "na",
|
|
||||||
* "homePageUrl": null,
|
|
||||||
* "statusPageUrl": null,
|
|
||||||
* "healthCheckUrl": null,
|
|
||||||
* "secureHealthCheckUrl": null,
|
|
||||||
* "vipAddress": "demo-order2",
|
|
||||||
* "secureVipAddress": "demo-order2",
|
|
||||||
* "countryId": 1,
|
|
||||||
* "dataCenterInfo": {
|
|
||||||
* "@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
|
|
||||||
* "name": "MyOwn"
|
|
||||||
* },
|
|
||||||
* "hostName": "127.0.0.1",
|
|
||||||
* "status": "UP",
|
|
||||||
* "leaseInfo": null,
|
|
||||||
* "isCoordinatingDiscoveryServer": false,
|
|
||||||
* "lastUpdatedTimestamp": 1529391461000,
|
|
||||||
* "lastDirtyTimestamp": 1529391461000,
|
|
||||||
* "actionType": null,
|
|
||||||
* "asgName": null,
|
|
||||||
* "overridden_status": "UNKNOWN",
|
|
||||||
* "port": {
|
|
||||||
* "$": 11102,
|
|
||||||
* "@enabled": "false"
|
|
||||||
* },
|
|
||||||
* "securePort": {
|
|
||||||
* "$": 7002,
|
|
||||||
* "@enabled": "false"
|
|
||||||
* },
|
|
||||||
* "metadata": {
|
|
||||||
* "@class": "java.util.Collections$EmptyMap"
|
|
||||||
* }* }
|
|
||||||
* }
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class EurekaInstance {
|
|
||||||
private String instanceId;
|
|
||||||
private String ipAddr;
|
|
||||||
private Map<String, Object> port;
|
|
||||||
|
|
||||||
private String status;
|
|
||||||
private String statusPageUrl;
|
|
||||||
private String healthCheckUrl;
|
|
||||||
private String lastUpdatedTimestamp;
|
|
||||||
|
|
||||||
private Map<String, String> metadata;
|
|
||||||
|
|
||||||
public String fetchPort() {
|
|
||||||
if (CollectionUtils.isEmpty(port)) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
return String.valueOf(port.getOrDefault("$", ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,84 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.bean;
|
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.internal.http.HttpMethod;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
|
|
||||||
*
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
public enum EurekaUri {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询所有实例 Query for all instances
|
|
||||||
*/
|
|
||||||
QUERY_APPS("GET", "/apps"),
|
|
||||||
/**
|
|
||||||
* 查询一个实例
|
|
||||||
*/
|
|
||||||
QUERY_INSTANCES("GET", "/instances/%s"),
|
|
||||||
/**
|
|
||||||
* 下线 Take instance out of service
|
|
||||||
*/
|
|
||||||
OFFLINE_SERVICE("PUT", "/apps/%s/%s/status?value=OUT_OF_SERVICE"),
|
|
||||||
/**
|
|
||||||
* 上线 Move instance back into service (remove override)
|
|
||||||
*/
|
|
||||||
ONLINE_SERVICE("DELETE", "/apps/%s/%s/status?value=UP"),
|
|
||||||
/**
|
|
||||||
* 设置metadata信息
|
|
||||||
*
|
|
||||||
* /apps/{appID}/{instanceID}/metadata?key=value
|
|
||||||
*/
|
|
||||||
SET_METADATA("PUT", "/apps/%s/%s/metadata?%s=%s")
|
|
||||||
;
|
|
||||||
public static final String URL_PREFIX = "/";
|
|
||||||
|
|
||||||
String uri;
|
|
||||||
String requestMethod;
|
|
||||||
|
|
||||||
EurekaUri(String httpMethod, String uri) {
|
|
||||||
if (!uri.startsWith(URL_PREFIX)) {
|
|
||||||
uri = "/" + uri;
|
|
||||||
}
|
|
||||||
this.uri = uri;
|
|
||||||
this.requestMethod = httpMethod;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUri(String... args) {
|
|
||||||
if (args == null || args.length == 0) {
|
|
||||||
return uri;
|
|
||||||
}
|
|
||||||
Object[] param = new Object[args.length];
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
|
||||||
param[i] = args[i];
|
|
||||||
}
|
|
||||||
return String.format(uri, param);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Request getRequest(String url, String... args) {
|
|
||||||
if (url.endsWith("/")) {
|
|
||||||
url = url.substring(0, url.length() - 1);
|
|
||||||
}
|
|
||||||
String requestUrl = url + getUri(args);
|
|
||||||
Request request = this.getBuilder()
|
|
||||||
.url(requestUrl)
|
|
||||||
.addHeader("Content-Type", "application/json")
|
|
||||||
.addHeader("Accept", "application/json")
|
|
||||||
.build();
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Request.Builder getBuilder() {
|
|
||||||
String method = requestMethod;
|
|
||||||
RequestBody requestBody = null;
|
|
||||||
if (HttpMethod.requiresRequestBody(method)) {
|
|
||||||
MediaType contentType = MediaType.parse("application/json");
|
|
||||||
requestBody = RequestBody.create(contentType, "{}");
|
|
||||||
}
|
|
||||||
return new Request.Builder().method(requestMethod, requestBody);
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,36 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.config;
|
|
||||||
|
|
||||||
import com.gitee.sop.registryapi.service.RegistryService;
|
|
||||||
import com.gitee.sop.registryapi.service.impl.RegistryServiceEureka;
|
|
||||||
import com.gitee.sop.registryapi.service.impl.RegistryServiceNacos;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
public class BaseRegistryConfig {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当配置了registry.name=eureka生效
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "eureka")
|
|
||||||
RegistryService registryServiceEureka() {
|
|
||||||
return new RegistryServiceEureka();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 当配置了registry.name=nacos生效
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnProperty(prefix = "registry", name = "name", havingValue = "nacos")
|
|
||||||
RegistryService registryServiceNacos() {
|
|
||||||
return new RegistryServiceNacos();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,100 +0,0 @@
|
|||||||
package com.gitee.sop.registryapi.service.impl;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.gitee.sop.registryapi.bean.EurekaApplication;
|
|
||||||
import com.gitee.sop.registryapi.bean.EurekaApps;
|
|
||||||
import com.gitee.sop.registryapi.bean.EurekaInstance;
|
|
||||||
import com.gitee.sop.registryapi.bean.EurekaUri;
|
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInfo;
|
|
||||||
import com.gitee.sop.registryapi.bean.ServiceInstance;
|
|
||||||
import com.gitee.sop.registryapi.service.RegistryService;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Response;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* eureka接口实现
|
|
||||||
* @author tanghc
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class RegistryServiceEureka implements RegistryService {
|
|
||||||
|
|
||||||
private OkHttpClient client = new OkHttpClient();
|
|
||||||
|
|
||||||
@Value("${registry.eureka-server-addr:}")
|
|
||||||
private String eurekaUrl;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception {
|
|
||||||
if (StringUtils.isBlank(eurekaUrl)) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
String json = this.requestEurekaServer(EurekaUri.QUERY_APPS);
|
|
||||||
EurekaApps eurekaApps = JSON.parseObject(json, EurekaApps.class);
|
|
||||||
|
|
||||||
List<ServiceInfo> serviceInfoList = new ArrayList<>();
|
|
||||||
List<EurekaApplication> applicationList = eurekaApps.getApplications().getApplication();
|
|
||||||
for (EurekaApplication eurekaApplication : applicationList) {
|
|
||||||
ServiceInfo serviceInfo = new ServiceInfo();
|
|
||||||
serviceInfo.setServiceId(eurekaApplication.getName());
|
|
||||||
List<EurekaInstance> instanceList = eurekaApplication.getInstance();
|
|
||||||
if (!CollectionUtils.isEmpty(instanceList)) {
|
|
||||||
serviceInfo.setInstances(new ArrayList<>(instanceList.size()));
|
|
||||||
for (EurekaInstance eurekaInstance : instanceList) {
|
|
||||||
ServiceInstance serviceInstance = new ServiceInstance();
|
|
||||||
serviceInstance.setInstanceId(eurekaInstance.getInstanceId());
|
|
||||||
serviceInstance.setServiceId(serviceInfo.getServiceId());
|
|
||||||
serviceInstance.setIp(eurekaInstance.getIpAddr());
|
|
||||||
serviceInstance.setPort(Integer.valueOf(eurekaInstance.fetchPort()));
|
|
||||||
serviceInstance.setStatus(eurekaInstance.getStatus());
|
|
||||||
Date updateTime = new Date(Long.valueOf(eurekaInstance.getLastUpdatedTimestamp()));
|
|
||||||
serviceInstance.setUpdateTime(DateFormatUtils.format(updateTime, TIMESTAMP_PATTERN));
|
|
||||||
serviceInstance.setMetadata(eurekaInstance.getMetadata());
|
|
||||||
serviceInfo.getInstances().add(serviceInstance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
serviceInfoList.add(serviceInfo);
|
|
||||||
}
|
|
||||||
return serviceInfoList;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onlineInstance(ServiceInstance serviceInstance) throws Exception {
|
|
||||||
this.requestEurekaServer(EurekaUri.ONLINE_SERVICE, serviceInstance.getServiceId(), serviceInstance.getInstanceId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void offlineInstance(ServiceInstance serviceInstance) throws Exception {
|
|
||||||
this.requestEurekaServer(EurekaUri.OFFLINE_SERVICE, serviceInstance.getServiceId(), serviceInstance.getInstanceId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setMetadata(ServiceInstance serviceInstance, String key, String value) throws Exception {
|
|
||||||
this.requestEurekaServer(EurekaUri.SET_METADATA, serviceInstance.getServiceId(), serviceInstance.getInstanceId(), key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String requestEurekaServer(EurekaUri eurekaUri, String... args) throws IOException {
|
|
||||||
Request request = eurekaUri.getRequest(this.eurekaUrl, args);
|
|
||||||
Response response = client.newCall(request).execute();
|
|
||||||
if (response.isSuccessful()) {
|
|
||||||
return response.body().string();
|
|
||||||
} else {
|
|
||||||
log.error("操作失败,url:{}, msg:{}, code:{}", eurekaUri.getUri(args), response.message(), response.code());
|
|
||||||
throw new RuntimeException("操作失败");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Reference in New Issue
Block a user