diff --git a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties index 62b798f0..cc9d0fa9 100644 --- a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties +++ b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties @@ -17,8 +17,17 @@ admin.access-token.timeout-minutes=30 sop.sign-type=rsa # nacos配置 -nacos.config.server-addr=${nacos.url} -nacos.discovery.server-addr=${nacos.url} +#nacos.config.server-addr=${nacos.url} +#nacos.discovery.server-addr=${nacos.url} + +# 网关地址,多个用逗号隔开 +# 在不使用nacos时有用,使用nacos时注释掉 +gateway.host=127.0.0.1:8081 + +# eureka注册中心 +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ +# 如果使用eureka,填eureka,使用nacos,填eureka +registry.name=eureka # 数据库配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/EurekaRegistryListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/EurekaRegistryListener.java index 5bc9a403..bc822b07 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/EurekaRegistryListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/EurekaRegistryListener.java @@ -2,10 +2,13 @@ package com.gitee.sop.gatewaycommon.route; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; import com.gitee.sop.gatewaycommon.loadbalancer.EurekaEnvironmentServerChooser; +import com.gitee.sop.gatewaycommon.loadbalancer.SopPropertiesFactory; import com.gitee.sop.gatewaycommon.manager.EnvironmentKeys; import com.netflix.appinfo.InstanceInfo; import com.netflix.discovery.shared.Application; import com.netflix.discovery.shared.Applications; +import lombok.AllArgsConstructor; +import lombok.Data; import org.apache.commons.collections.CollectionUtils; import org.springframework.cloud.netflix.eureka.CloudEurekaClient; import org.springframework.context.ApplicationEvent; @@ -27,50 +30,77 @@ public class EurekaRegistryListener extends BaseRegistryListener { System.setProperty(EnvironmentKeys.ZUUL_CUSTOM_RULE_CLASSNAME.getKey(), EurekaEnvironmentServerChooser.class.getName()); } - private Set cacheServices = new HashSet<>(); + private Set cacheServices = new HashSet<>(); - /** - * 注册中心触发事件,可以从中获取服务
- * - * 这个方法做的事情有2个:
- * - * 1. 找出新注册的服务,调用pullRoutes方法
- * 2. 找出删除的服务,调用removeRoutes方法
- * - * @param applicationEvent 事件体 - */ @Override public void onEvent(ApplicationEvent applicationEvent) { Object source = applicationEvent.getSource(); CloudEurekaClient cloudEurekaClient = (CloudEurekaClient) source; Applications applications = cloudEurekaClient.getApplications(); List registeredApplications = applications.getRegisteredApplications(); - List serviceList = registeredApplications + + List serviceList = registeredApplications .stream() - .map(Application::getName) + .filter(application -> CollectionUtils.isNotEmpty(application.getInstances())) + .map(Application::getInstances) + .map(instanceInfos -> { + // 根据更新时间倒叙 + instanceInfos.sort(Comparator.comparing(InstanceInfo::getLastUpdatedTimestamp).reversed()); + // 获取最新的个服务实例,说明这个服务实例刚刚重启过 + return instanceInfos.get(0); + }) + .map(instanceInfo -> new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp())) .collect(Collectors.toList()); - final Set currentServices = new HashSet<>(serviceList); + final Set currentServices = new HashSet<>(serviceList); currentServices.removeAll(cacheServices); // 如果有新的服务注册进来 if (currentServices.size() > 0) { List newApplications = registeredApplications.stream() - .filter(application -> this.canOperator(application.getName()) - && currentServices.contains(application.getName())) + .filter(application -> + this.canOperator(application.getName()) && containsService(currentServices, application.getName())) .collect(Collectors.toList()); this.doRegister(newApplications); } - cacheServices.removeAll(new HashSet<>(serviceList)); + Set removedServiceIdList = getRemovedServiceId(serviceList); // 如果有服务删除 - if (cacheServices.size() > 0) { - this.doRemove(cacheServices); + if (removedServiceIdList.size() > 0) { + this.doRemove(removedServiceIdList); } - // 缓存最新服务 + cacheServices = new HashSet<>(serviceList); } + /** + * 获取已经下线的serviceId + * + * @param serviceList 最新的serviceId集合 + * @return 返回已下线的serviceId + */ + private Set getRemovedServiceId(List serviceList) { + Set cache = cacheServices.stream() + .map(ServiceHolder::getServiceId) + .collect(Collectors.toSet()); + + Set newList = serviceList.stream() + .map(ServiceHolder::getServiceId) + .collect(Collectors.toSet()); + + cache.removeAll(newList); + return cache; + } + + private static boolean containsService(Set currentServices, String serviceId) { + for (ServiceHolder currentService : currentServices) { + if (currentService.getServiceId().equalsIgnoreCase(serviceId)) { + return true; + } + } + return false; + } + private void doRegister(List registeredApplications) { registeredApplications.forEach(application -> { List instances = application.getInstances(); @@ -92,4 +122,10 @@ public class EurekaRegistryListener extends BaseRegistryListener { deletedServices.forEach(this::removeRoutes); } + @Data + @AllArgsConstructor + private static class ServiceHolder { + private String serviceId; + private long lastUpdatedTimestamp; + } } diff --git a/sop-example/sop-book/sop-book-web/pom.xml b/sop-example/sop-book/sop-book-web/pom.xml index 9c09ad48..79b64a77 100644 --- a/sop-example/sop-book/sop-book-web/pom.xml +++ b/sop-example/sop-book/sop-book-web/pom.xml @@ -45,7 +45,7 @@ + org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery @@ -56,8 +56,14 @@ nacos-client ${nacos-client.version} + --> + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + org.springframework.cloud spring-cloud-starter-openfeign diff --git a/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties b/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties index 05644cb7..5d705fbc 100644 --- a/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties +++ b/sop-example/sop-book/sop-book-web/src/main/resources/application-dev.properties @@ -2,7 +2,10 @@ server.port=3333 spring.application.name=book-service # nacos注册中心 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +# eureka注册中心 +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ # consumer不需要检查provider是否启动 dubbo.consumer.check=false diff --git a/sop-example/sop-story/sop-story-web/pom.xml b/sop-example/sop-story/sop-story-web/pom.xml index 08ce4c4f..4e5ce0f3 100644 --- a/sop-example/sop-story/sop-story-web/pom.xml +++ b/sop-example/sop-story/sop-story-web/pom.xml @@ -39,7 +39,7 @@ + org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery @@ -50,8 +50,14 @@ nacos-client ${nacos-client.version} + --> + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + org.springframework.cloud spring-cloud-starter-openfeign diff --git a/sop-example/sop-story/sop-story-web/src/main/resources/application-dev.properties b/sop-example/sop-story/sop-story-web/src/main/resources/application-dev.properties index 28615eab..ea928967 100644 --- a/sop-example/sop-story/sop-story-web/src/main/resources/application-dev.properties +++ b/sop-example/sop-story/sop-story-web/src/main/resources/application-dev.properties @@ -4,8 +4,10 @@ spring.application.name=story-service # 如果有context-path,必须配下面这句 #spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servlet.context-path} # nacos注册中心 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +# eureka注册中心 +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ # dubbo配置 dubbo.protocol.name=dubbo diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 078d768c..3d191568 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -57,7 +57,7 @@ + org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery @@ -68,8 +68,14 @@ nacos-client ${nacos-client.version} + --> + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + +