mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
nacos注册中心
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.3.RELEASE</version>
|
||||
<version>2.1.4.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@@ -27,12 +27,12 @@
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 将前端页面打包成webjars,然后打开注释
|
||||
<dependency>
|
||||
<groupId>com.gitee.sop</groupId>
|
||||
<artifactId>sop-admin-front</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>-->
|
||||
<artifactId>sop-registry-api</artifactId>
|
||||
<version>1.11.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<!-- easyopen starter -->
|
||||
<dependency>
|
||||
<groupId>net.oschina.durcframework</groupId>
|
||||
@@ -78,12 +78,6 @@
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@@ -109,6 +103,12 @@
|
||||
<version>1.18.4</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
<version>2.1.0.RELEASE</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@@ -10,22 +10,21 @@ import com.gitee.sop.adminserver.api.service.param.ServiceSearchParam;
|
||||
import com.gitee.sop.adminserver.api.service.result.RouteServiceInfo;
|
||||
import com.gitee.sop.adminserver.api.service.result.ServiceInfoVo;
|
||||
import com.gitee.sop.adminserver.api.service.result.ServiceInstanceVO;
|
||||
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
||||
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||
import com.gitee.sop.adminserver.bean.ServiceRouteInfo;
|
||||
import com.gitee.sop.adminserver.bean.ZookeeperContext;
|
||||
import com.gitee.sop.adminserver.common.BizException;
|
||||
import com.gitee.sop.adminserver.common.ZookeeperPathExistException;
|
||||
import com.gitee.sop.adminserver.common.ZookeeperPathNotExistException;
|
||||
import com.gitee.sop.adminserver.service.RegistrationService;
|
||||
import com.gitee.sop.adminserver.service.impl.RegistrationServiceEureka;
|
||||
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 org.apache.commons.lang.BooleanUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.curator.framework.recipes.cache.ChildData;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -42,8 +41,8 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class ServiceApi {
|
||||
|
||||
@Resource(type = RegistrationServiceEureka.class)
|
||||
private RegistrationService registrationService;
|
||||
@Autowired
|
||||
private RegistryService registryService;
|
||||
|
||||
@Api(name = "zookeeper.service.list")
|
||||
@ApiDocMethod(description = "zk中的服务列表", elementClass = RouteServiceInfo.class)
|
||||
@@ -113,10 +112,10 @@ public class ServiceApi {
|
||||
|
||||
@Api(name = "service.instance.list")
|
||||
@ApiDocMethod(description = "获取注册中心的服务列表", elementClass = ServiceInfoVo.class)
|
||||
List<ServiceInstanceVO> listService(ServiceSearchParam param) {
|
||||
List<ServiceInstanceVO> listService(ServiceSearchParam param) {
|
||||
List<ServiceInfo> serviceInfos;
|
||||
try {
|
||||
serviceInfos = registrationService.listAllService(1, 99999/* 获取所有实例 */);
|
||||
serviceInfos = registryService.listAllService(1, 99999/* 获取所有实例 */);
|
||||
} catch (Exception e) {
|
||||
log.error("获取服务实例失败", e);
|
||||
return Collections.emptyList();
|
||||
@@ -156,7 +155,7 @@ public class ServiceApi {
|
||||
@ApiDocMethod(description = "服务下线")
|
||||
void serviceOffline(ServiceInstance param) {
|
||||
try {
|
||||
registrationService.offlineInstance(param);
|
||||
registryService.offlineInstance(param);
|
||||
} catch (Exception e) {
|
||||
log.error("下线失败,param:{}", param, e);
|
||||
throw new BizException("下线失败,请查看日志");
|
||||
@@ -167,7 +166,7 @@ public class ServiceApi {
|
||||
@ApiDocMethod(description = "服务上线")
|
||||
void serviceOnline(ServiceInstance param) throws IOException {
|
||||
try {
|
||||
registrationService.onlineInstance(param);
|
||||
registryService.onlineInstance(param);
|
||||
} catch (Exception e) {
|
||||
log.error("上线失败,param:{}", param, e);
|
||||
throw new BizException("上线失败,请查看日志");
|
||||
|
@@ -24,6 +24,12 @@ public class ServiceInstanceVO {
|
||||
@ApiDocField(description = "ipPort")
|
||||
private String ipPort;
|
||||
|
||||
@ApiDocField(description = "ip")
|
||||
private String ip;
|
||||
|
||||
@ApiDocField(description = "port")
|
||||
private int port;
|
||||
|
||||
@ApiDocField(description = "status,服务状态,UP:已上线,OUT_OF_SERVICE:已下线")
|
||||
private String status;
|
||||
|
||||
@@ -41,4 +47,8 @@ public class ServiceInstanceVO {
|
||||
return new Date(Long.valueOf(lastUpdatedTimestamp));
|
||||
}
|
||||
|
||||
public String getIpPort() {
|
||||
return ip != null && port > 0 ? ip + ":" + port : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,14 +0,0 @@
|
||||
package com.gitee.sop.adminserver.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.adminserver.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.adminserver.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
public class EurekaApps {
|
||||
private EurekaApplications applications;
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
package com.gitee.sop.adminserver.bean;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
public String fetchPort() {
|
||||
if (CollectionUtils.isEmpty(port)) {
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(port.getOrDefault("$", ""));
|
||||
}
|
||||
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
package com.gitee.sop.adminserver.bean;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.internal.http.HttpMethod;
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
* https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
public enum EurekaUri {
|
||||
|
||||
/**
|
||||
* 查询所有实例 Query for all instances
|
||||
*/
|
||||
QUERY_APPS(RequestMethod.GET, "/apps"),
|
||||
/**
|
||||
* 下线 Take instance out of service
|
||||
*/
|
||||
OFFLINE_SERVICE(RequestMethod.PUT, "/apps/%s/%s/status?value=OUT_OF_SERVICE"),
|
||||
/**
|
||||
* 上线 Move instance back into service (remove override)
|
||||
*/
|
||||
ONLINE_SERVICE(RequestMethod.DELETE, "/apps/%s/%s/status?value=UP"),
|
||||
;
|
||||
public static final String URL_PREFIX = "/";
|
||||
|
||||
String uri;
|
||||
RequestMethod requestMethod;
|
||||
|
||||
EurekaUri(RequestMethod httpMethod, String uri) {
|
||||
if (!uri.startsWith(URL_PREFIX)) {
|
||||
uri = "/" + uri;
|
||||
}
|
||||
this.uri = uri;
|
||||
this.requestMethod = httpMethod;
|
||||
}
|
||||
|
||||
public String getUri(String... args) {
|
||||
if (ArrayUtils.isEmpty(args)) {
|
||||
return uri;
|
||||
}
|
||||
Object[] param = ArrayUtils.clone(args);
|
||||
return String.format(uri, param);
|
||||
}
|
||||
|
||||
public Request getRequest(String url, String... args) {
|
||||
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.name();
|
||||
RequestBody requestBody = null;
|
||||
if (HttpMethod.requiresRequestBody(method)) {
|
||||
MediaType contentType = MediaType.parse(org.springframework.http.MediaType.APPLICATION_JSON_VALUE);
|
||||
requestBody = RequestBody.create(contentType, "{}");
|
||||
}
|
||||
return new Request.Builder().method(requestMethod.name(), requestBody);
|
||||
}
|
||||
}
|
@@ -1,17 +0,0 @@
|
||||
package com.gitee.sop.adminserver.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
public class ServiceInfo {
|
||||
/** 服务名称 */
|
||||
private String serviceId;
|
||||
/** 实例列表 */
|
||||
private List<ServiceInstance> instances = Collections.emptyList();
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
package com.gitee.sop.adminserver.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ServiceInstance {
|
||||
/**
|
||||
* 实例id
|
||||
*/
|
||||
private String instanceId;
|
||||
|
||||
/**
|
||||
* 服务名称
|
||||
*/
|
||||
private String serviceId;
|
||||
|
||||
/**
|
||||
* IP 端口
|
||||
*/
|
||||
private String ipPort;
|
||||
|
||||
/**
|
||||
* 状态,1:上线,2:下线
|
||||
*/
|
||||
private String status;
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
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,36 +0,0 @@
|
||||
package com.gitee.sop.adminserver.service;
|
||||
|
||||
import com.gitee.sop.adminserver.bean.ServiceInfo;
|
||||
import com.gitee.sop.adminserver.bean.ServiceInstance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
public interface RegistrationService {
|
||||
/**
|
||||
* 获取所有服务列表
|
||||
*
|
||||
* @param pageNo 当前页码
|
||||
* @param pageSize 分页大小
|
||||
* @return 返回服务列表
|
||||
*/
|
||||
List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception;
|
||||
|
||||
/**
|
||||
* 服务上线
|
||||
*
|
||||
* @param serviceInstance
|
||||
*/
|
||||
void onlineInstance(ServiceInstance serviceInstance) throws Exception;
|
||||
|
||||
/**
|
||||
* 服务下线
|
||||
*
|
||||
* @param serviceInstance
|
||||
*/
|
||||
void offlineInstance(ServiceInstance serviceInstance) throws Exception;
|
||||
|
||||
|
||||
}
|
@@ -1,104 +0,0 @@
|
||||
package com.gitee.sop.adminserver.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.adminserver.bean.EurekaApplication;
|
||||
import com.gitee.sop.adminserver.bean.EurekaApps;
|
||||
import com.gitee.sop.adminserver.bean.EurekaInstance;
|
||||
import com.gitee.sop.adminserver.bean.EurekaUri;
|
||||
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.service.RegistrationService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class RegistrationServiceEureka implements RegistrationService {
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
private String eurekaUrl;
|
||||
|
||||
@Override
|
||||
public List<ServiceInfo> listAllService(int pageNo, int pageSize) throws Exception {
|
||||
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.isNotEmpty(instanceList)) {
|
||||
serviceInfo.setInstances(new ArrayList<>(instanceList.size()));
|
||||
for (EurekaInstance eurekaInstance : instanceList) {
|
||||
ServiceInstance serviceInstance = new ServiceInstance();
|
||||
serviceInstance.setInstanceId(eurekaInstance.getInstanceId());
|
||||
serviceInstance.setServiceId(serviceInfo.getServiceId());
|
||||
serviceInstance.setIpPort(eurekaInstance.getIpAddr() + ":" + eurekaInstance.fetchPort());
|
||||
serviceInstance.setStatus(eurekaInstance.getStatus());
|
||||
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());
|
||||
}
|
||||
|
||||
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 BizException("操作失败", String.valueOf(response.code()));
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
protected void after() {
|
||||
String eurekaUrls = environment.getProperty("eureka.client.serviceUrl.defaultZone");
|
||||
if (StringUtils.isBlank(eurekaUrls)) {
|
||||
throw new BizException("未指定eureka.client.serviceUrl.defaultZone参数");
|
||||
}
|
||||
// 取第一个
|
||||
String url = eurekaUrls.split("\\,")[0];
|
||||
if (url.endsWith("/")) {
|
||||
url = eurekaUrls.substring(0, eurekaUrls.length() - 1);
|
||||
}
|
||||
this.eurekaUrl = url;
|
||||
}
|
||||
}
|
@@ -12,6 +12,8 @@ spring:
|
||||
name: sop-admin
|
||||
|
||||
cloud:
|
||||
|
||||
# zookeeper地址,根据实际情况修改
|
||||
zookeeper:
|
||||
connect-string: localhost:2181
|
||||
baseSleepTimeMs: 3000
|
||||
@@ -31,12 +33,12 @@ easyopen:
|
||||
ignore-validate: true
|
||||
|
||||
# 注册中心地址,根据实际情况改,这里只是参数,并不会去注册
|
||||
eureka:
|
||||
port: 1111
|
||||
host: localhost
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
registry:
|
||||
eureka-server-addr: http://localhost:1111/eureka/
|
||||
nacos-server-addr: 127.0.0.1:8848
|
||||
# 使用哪个配置中心,使用eureka,填eureka;使用nacos填nacos
|
||||
name: nacos
|
||||
|
||||
|
||||
# 根据实际情况改
|
||||
logging:
|
||||
|
Reference in New Issue
Block a user