From 43e5707c0141470a75e7a564a73566f5c680e6fc Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 18 Aug 2020 14:04:29 +0800 Subject: [PATCH] 4.0.3 --- .../bridge/route/EurekaRegistryListener.java | 3 +- .../bridge/SopRegisterAutoConfiguration.java | 25 ---------- .../bridge/route/NacosRegistryListener.java | 48 ++++++++++--------- .../sop/gatewaycommon/bean/SopConstants.java | 2 + .../route/BaseRegistryListener.java | 5 +- .../gatewaycommon/route/ServiceHolder.java | 1 - .../configuration/ServiceConfiguration.java | 1 + 7 files changed, 33 insertions(+), 52 deletions(-) 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 f0ca2758..7b7e3797 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 @@ -48,6 +48,7 @@ public class EurekaRegistryListener extends BaseRegistryListener { return instanceInfos.get(0); }) .map(instanceInfo -> new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp())) + .filter(this::canOperator) .collect(Collectors.toList()); final Set currentServices = new HashSet<>(serviceList); @@ -57,7 +58,7 @@ public class EurekaRegistryListener extends BaseRegistryListener { if (currentServices.size() > 0) { List newApplications = registeredApplications.stream() .filter(application -> - this.canOperator(application.getName()) && containsService(currentServices, application.getName())) + containsService(currentServices, application.getName())) .collect(Collectors.toList()); this.doRegister(newApplications); diff --git a/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/SopRegisterAutoConfiguration.java b/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/SopRegisterAutoConfiguration.java index 6929b702..63c49e23 100644 --- a/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/SopRegisterAutoConfiguration.java +++ b/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/SopRegisterAutoConfiguration.java @@ -1,20 +1,9 @@ package com.gitee.sop.bridge; -import com.alibaba.cloud.nacos.NacosDiscoveryProperties; -import com.alibaba.cloud.nacos.discovery.NacosWatch; import com.gitee.sop.bridge.route.NacosRegistryListener; import com.gitee.sop.gatewaycommon.route.RegistryListener; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.scheduling.TaskScheduler; - -import java.util.Map; /** * @author tanghc @@ -22,20 +11,6 @@ import java.util.Map; @Configuration public class SopRegisterAutoConfiguration { - @Bean - @ConditionalOnMissingBean - 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); - } - /** * 微服务路由加载 */ diff --git a/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/route/NacosRegistryListener.java b/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/route/NacosRegistryListener.java index cc871f80..48058ad4 100644 --- a/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/route/NacosRegistryListener.java +++ b/sop-common/sop-bridge-nacos/src/main/java/com/gitee/sop/bridge/route/NacosRegistryListener.java @@ -6,9 +6,12 @@ import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; 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; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.math.NumberUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEvent; import org.springframework.util.CollectionUtils; @@ -29,8 +32,6 @@ import java.util.stream.Collectors; @Slf4j public class NacosRegistryListener extends BaseRegistryListener { - private static final String METADATA_KEY_TIME_STARTUP = "time.startup"; - private volatile Set cacheServices = new HashSet<>(); @Autowired @@ -73,6 +74,7 @@ public class NacosRegistryListener extends BaseRegistryListener { /** * 获取建康的服务实例 + * * @return 没有返回空的list */ private List getServiceList() { @@ -89,36 +91,36 @@ public class NacosRegistryListener extends BaseRegistryListener { return servicesOfServer .getData() .stream() - .filter(this::canOperator) .map(serviceName -> { + List allInstances; try { // 获取服务实例 - List allInstances = namingService.getAllInstances(serviceName); - if (CollectionUtils.isEmpty(allInstances)) { - return null; - } - Instance instance = allInstances.stream() - // 只获取建康实例 - .filter(Instance::isHealthy) - // 根据启动时间倒叙,找到最新的服务器 - .max(Comparator.comparing(ins -> { - String startupTime = ins.getMetadata().getOrDefault(METADATA_KEY_TIME_STARTUP, "0"); - return Long.valueOf(startupTime); - })) - .orElse(null); - if (instance == null) { - return null; - } else { - String startupTime = instance.getMetadata().getOrDefault("time.startup", "0"); - long time = Long.parseLong(startupTime); - return new NacosServiceHolder(serviceName, time, instance); - } + allInstances = namingService.getAllInstances(serviceName); } catch (NacosException e) { log.error("namingService.getAllInstances(serviceName)错误,serviceName:{}", serviceName, e); return null; } + if (CollectionUtils.isEmpty(allInstances)) { + return null; + } + return allInstances.stream() + // 只获取建康实例 + .filter(Instance::isHealthy) + .map(instance -> { + String startupTime = instance.getMetadata().get(SopConstants.METADATA_KEY_TIME_STARTUP); + if (startupTime == null) { + return null; + } + long time = NumberUtils.toLong(startupTime, 0); + return new NacosServiceHolder(serviceName, time, instance); + }) + .filter(Objects::nonNull) + .max(Comparator.comparing(ServiceHolder::getLastUpdatedTimestamp)) + .orElse(null); + }) .filter(Objects::nonNull) + .filter(this::canOperator) .collect(Collectors.toList()); } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java index 47abfbd3..7c04cec7 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java @@ -54,4 +54,6 @@ public class SopConstants { public static final String TARGET_SERVICE = "sop-target-service"; public static final String RESTFUL_REQUEST = "sop-restful-request"; + public static final String METADATA_KEY_TIME_STARTUP = "time.startup"; + } diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java index 2e7128b5..06db6e24 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java @@ -20,7 +20,7 @@ public abstract class BaseRegistryListener implements RegistryListener { private static final int FIVE_SECONDS = 1000 * 5; - private Map updateTimeMap = new ConcurrentHashMap<>(16); + private final Map updateTimeMap = new ConcurrentHashMap<>(16); public static List EXCLUDE_SERVICE_ID_LIST = new ArrayList<>(8); @@ -54,7 +54,8 @@ public abstract class BaseRegistryListener implements RegistryListener { serviceListener.onAddInstance(instance); } - protected boolean canOperator(String serviceId) { + protected boolean canOperator(ServiceHolder serviceHolder) { + String serviceId = serviceHolder.getServiceId(); // 被排除的服务,不能操作 if (isExcludeService(serviceId)) { return false; diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceHolder.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceHolder.java index a5641f08..568ada29 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceHolder.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/ServiceHolder.java @@ -1,7 +1,6 @@ package com.gitee.sop.gatewaycommon.route; import lombok.AllArgsConstructor; -import lombok.Data; import lombok.Getter; import lombok.Setter; 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 adb68d04..e502b0f3 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 @@ -70,6 +70,7 @@ public class ServiceConfiguration implements WebMvcConfigurer { metadata.put("context-path", contextPath); } // 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题 + // 如果没有这个值,网关会忽略这个服务 metadata.put("time.startup", String.valueOf(System.currentTimeMillis())); return new NacosWatch(nacosDiscoveryProperties, taskScheduler); }