parent
90fdbbccf2
commit
7d5d6ed3e3
@ -1,4 +1,4 @@ |
||||
package com.gitee.sop.registryapi.bean; |
||||
package com.gitee.sop.adminserver.bean; |
||||
|
||||
import lombok.Data; |
||||
import lombok.Getter; |
@ -1,4 +1,4 @@ |
||||
package com.gitee.sop.registryapi.bean; |
||||
package com.gitee.sop.adminserver.bean; |
||||
|
||||
import lombok.Data; |
||||
|
@ -1,4 +1,4 @@ |
||||
package com.gitee.sop.registryapi.bean; |
||||
package com.gitee.sop.adminserver.bean; |
||||
|
||||
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 { |
||||
|
||||
} |
@ -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.registryapi.bean.ServiceInstance; |
||||
import com.gitee.sop.adminserver.bean.ServiceInfo; |
||||
import com.gitee.sop.adminserver.bean.ServiceInstance; |
||||
|
||||
import java.util.List; |
||||
|
@ -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("操作失败"); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue