diff --git a/sop-admin/sop-admin-server/pom.xml b/sop-admin/sop-admin-server/pom.xml index 9d942fdd..44d13862 100644 --- a/sop-admin/sop-admin-server/pom.xml +++ b/sop-admin/sop-admin-server/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-bridge-eureka - 4.0.0-SNAPSHOT + 4.0.3-SNAPSHOT diff --git a/sop-common/sop-bridge-eureka/src/main/java/com/gitee/sop/bridge/route/EurekaRegistryListener.java b/sop-common/sop-bridge-eureka/src/main/java/com/gitee/sop/bridge/route/EurekaRegistryListener.java index 7b7e3797..e6b9b264 100644 --- a/sop-common/sop-bridge-eureka/src/main/java/com/gitee/sop/bridge/route/EurekaRegistryListener.java +++ b/sop-common/sop-bridge-eureka/src/main/java/com/gitee/sop/bridge/route/EurekaRegistryListener.java @@ -1,6 +1,7 @@ package com.gitee.sop.bridge.route; import com.gitee.sop.gatewaycommon.bean.InstanceDefinition; +import com.gitee.sop.gatewaycommon.bean.SopConstants; import com.gitee.sop.gatewaycommon.route.BaseRegistryListener; import com.gitee.sop.gatewaycommon.route.RegistryEvent; import com.gitee.sop.gatewaycommon.route.ServiceHolder; @@ -15,6 +16,8 @@ import org.springframework.context.ApplicationEvent; import java.util.Comparator; import java.util.HashSet; import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -42,12 +45,19 @@ public class EurekaRegistryListener extends BaseRegistryListener { .filter(application -> CollectionUtils.isNotEmpty(application.getInstances())) .map(Application::getInstances) .map(instanceInfos -> { - // 根据更新时间倒叙 - instanceInfos.sort(Comparator.comparing(InstanceInfo::getLastUpdatedTimestamp).reversed()); - // 获取最新的个服务实例,说明这个服务实例刚刚重启过 - return instanceInfos.get(0); + // 找到最新的一个 + Optional instanceInfoOptional = instanceInfos + .stream() + .max(Comparator.comparing(InstanceInfo::getLastUpdatedTimestamp)); + return instanceInfoOptional.map(instanceInfo -> { + String startupTime = instanceInfo.getMetadata().get(SopConstants.METADATA_KEY_TIME_STARTUP); + if (startupTime == null) { + return null; + } + return new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp()); + }).orElse(null); }) - .map(instanceInfo -> new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp())) + .filter(Objects::nonNull) .filter(this::canOperator) .collect(Collectors.toList()); diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.java index e502b0f3..9067984a 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.java @@ -1,20 +1,14 @@ package com.gitee.sop.servercommon.configuration; -import com.alibaba.cloud.nacos.NacosDiscoveryProperties; -import com.alibaba.cloud.nacos.discovery.NacosWatch; import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.interceptor.ServiceContextInterceptor; import com.gitee.sop.servercommon.message.ServiceErrorFactory; import com.gitee.sop.servercommon.route.ServiceRouteController; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; -import org.springframework.core.env.Environment; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.scheduling.TaskScheduler; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -22,7 +16,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.annotation.PostConstruct; import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.Map; /** * @author tanghc @@ -31,6 +24,7 @@ import java.util.Map; public class ServiceConfiguration implements WebMvcConfigurer { public ServiceConfiguration() { + System.setProperty("eureka.instance.metadata-map.time.startup", String.valueOf(System.currentTimeMillis())); ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror"); } @@ -59,22 +53,6 @@ public class ServiceConfiguration implements WebMvcConfigurer { registry.addInterceptor(new ServiceContextInterceptor()); } - @Bean - @ConditionalOnMissingBean - @ConditionalOnProperty("spring.cloud.nacos.discovery.server-addr") - public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties, ObjectProvider taskScheduler, Environment environment) { - Map metadata = nacosDiscoveryProperties.getMetadata(); - String contextPath = environment.getProperty("server.servlet.context-path"); - // 将context-path信息加入到metadata中 - if (contextPath != null) { - metadata.put("context-path", contextPath); - } - // 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题 - // 如果没有这个值,网关会忽略这个服务 - metadata.put("time.startup", String.valueOf(System.currentTimeMillis())); - return new NacosWatch(nacosDiscoveryProperties, taskScheduler); - } - @Bean @ConditionalOnMissingBean GlobalExceptionHandler globalExceptionHandler() { diff --git a/sop-example/sop-story/src/main/resources/application-dev.properties b/sop-example/sop-story/src/main/resources/application-dev.properties index ea5b6216..8d5ebeea 100644 --- a/sop-example/sop-story/src/main/resources/application-dev.properties +++ b/sop-example/sop-story/src/main/resources/application-dev.properties @@ -1,5 +1,5 @@ server.port=2222 spring.application.name=story-service # 注册中心 -spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index 8fc00331..f35c5fba 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -24,7 +24,7 @@ com.gitee.sop sop-bridge-eureka - 4.0.0-SNAPSHOT + 4.0.3-SNAPSHOT diff --git a/sop-website/pom.xml b/sop-website/pom.xml index 5e66d5a6..c5fa8bd4 100644 --- a/sop-website/pom.xml +++ b/sop-website/pom.xml @@ -25,7 +25,7 @@ com.gitee.sop sop-bridge-eureka - 4.0.0-SNAPSHOT + 4.0.3-SNAPSHOT