mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-12 07:02:14 +08:00
4.0.3
This commit is contained in:
@@ -48,6 +48,7 @@ public class EurekaRegistryListener extends BaseRegistryListener {
|
|||||||
return instanceInfos.get(0);
|
return instanceInfos.get(0);
|
||||||
})
|
})
|
||||||
.map(instanceInfo -> new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp()))
|
.map(instanceInfo -> new ServiceHolder(instanceInfo.getAppName(), instanceInfo.getLastUpdatedTimestamp()))
|
||||||
|
.filter(this::canOperator)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
final Set<ServiceHolder> currentServices = new HashSet<>(serviceList);
|
final Set<ServiceHolder> currentServices = new HashSet<>(serviceList);
|
||||||
@@ -57,7 +58,7 @@ public class EurekaRegistryListener extends BaseRegistryListener {
|
|||||||
if (currentServices.size() > 0) {
|
if (currentServices.size() > 0) {
|
||||||
List<Application> newApplications = registeredApplications.stream()
|
List<Application> newApplications = registeredApplications.stream()
|
||||||
.filter(application ->
|
.filter(application ->
|
||||||
this.canOperator(application.getName()) && containsService(currentServices, application.getName()))
|
containsService(currentServices, application.getName()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
this.doRegister(newApplications);
|
this.doRegister(newApplications);
|
||||||
|
@@ -1,20 +1,9 @@
|
|||||||
package com.gitee.sop.bridge;
|
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.bridge.route.NacosRegistryListener;
|
||||||
import com.gitee.sop.gatewaycommon.route.RegistryListener;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.scheduling.TaskScheduler;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author tanghc
|
* @author tanghc
|
||||||
@@ -22,20 +11,6 @@ import java.util.Map;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SopRegisterAutoConfiguration {
|
public class SopRegisterAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties, ObjectProvider<TaskScheduler> taskScheduler, Environment environment) {
|
|
||||||
Map<String, String> 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微服务路由加载
|
* 微服务路由加载
|
||||||
*/
|
*/
|
||||||
|
@@ -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.Instance;
|
||||||
import com.alibaba.nacos.api.naming.pojo.ListView;
|
import com.alibaba.nacos.api.naming.pojo.ListView;
|
||||||
import com.gitee.sop.gatewaycommon.bean.InstanceDefinition;
|
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.BaseRegistryListener;
|
||||||
import com.gitee.sop.gatewaycommon.route.RegistryEvent;
|
import com.gitee.sop.gatewaycommon.route.RegistryEvent;
|
||||||
|
import com.gitee.sop.gatewaycommon.route.ServiceHolder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationEvent;
|
import org.springframework.context.ApplicationEvent;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
@@ -29,8 +32,6 @@ import java.util.stream.Collectors;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class NacosRegistryListener extends BaseRegistryListener {
|
public class NacosRegistryListener extends BaseRegistryListener {
|
||||||
|
|
||||||
private static final String METADATA_KEY_TIME_STARTUP = "time.startup";
|
|
||||||
|
|
||||||
private volatile Set<NacosServiceHolder> cacheServices = new HashSet<>();
|
private volatile Set<NacosServiceHolder> cacheServices = new HashSet<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -73,6 +74,7 @@ public class NacosRegistryListener extends BaseRegistryListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取建康的服务实例
|
* 获取建康的服务实例
|
||||||
|
*
|
||||||
* @return 没有返回空的list
|
* @return 没有返回空的list
|
||||||
*/
|
*/
|
||||||
private List<NacosServiceHolder> getServiceList() {
|
private List<NacosServiceHolder> getServiceList() {
|
||||||
@@ -89,36 +91,36 @@ public class NacosRegistryListener extends BaseRegistryListener {
|
|||||||
return servicesOfServer
|
return servicesOfServer
|
||||||
.getData()
|
.getData()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(this::canOperator)
|
|
||||||
.map(serviceName -> {
|
.map(serviceName -> {
|
||||||
|
List<Instance> allInstances;
|
||||||
try {
|
try {
|
||||||
// 获取服务实例
|
// 获取服务实例
|
||||||
List<Instance> allInstances = namingService.getAllInstances(serviceName);
|
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);
|
|
||||||
}
|
|
||||||
} catch (NacosException e) {
|
} catch (NacosException e) {
|
||||||
log.error("namingService.getAllInstances(serviceName)错误,serviceName:{}", serviceName, e);
|
log.error("namingService.getAllInstances(serviceName)错误,serviceName:{}", serviceName, e);
|
||||||
return null;
|
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(Objects::nonNull)
|
||||||
|
.filter(this::canOperator)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -54,4 +54,6 @@ public class SopConstants {
|
|||||||
public static final String TARGET_SERVICE = "sop-target-service";
|
public static final String TARGET_SERVICE = "sop-target-service";
|
||||||
public static final String RESTFUL_REQUEST = "sop-restful-request";
|
public static final String RESTFUL_REQUEST = "sop-restful-request";
|
||||||
|
|
||||||
|
public static final String METADATA_KEY_TIME_STARTUP = "time.startup";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -20,7 +20,7 @@ public abstract class BaseRegistryListener implements RegistryListener {
|
|||||||
|
|
||||||
private static final int FIVE_SECONDS = 1000 * 5;
|
private static final int FIVE_SECONDS = 1000 * 5;
|
||||||
|
|
||||||
private Map<String, Long> updateTimeMap = new ConcurrentHashMap<>(16);
|
private final Map<String, Long> updateTimeMap = new ConcurrentHashMap<>(16);
|
||||||
|
|
||||||
public static List<String> EXCLUDE_SERVICE_ID_LIST = new ArrayList<>(8);
|
public static List<String> EXCLUDE_SERVICE_ID_LIST = new ArrayList<>(8);
|
||||||
|
|
||||||
@@ -54,7 +54,8 @@ public abstract class BaseRegistryListener implements RegistryListener {
|
|||||||
serviceListener.onAddInstance(instance);
|
serviceListener.onAddInstance(instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean canOperator(String serviceId) {
|
protected boolean canOperator(ServiceHolder serviceHolder) {
|
||||||
|
String serviceId = serviceHolder.getServiceId();
|
||||||
// 被排除的服务,不能操作
|
// 被排除的服务,不能操作
|
||||||
if (isExcludeService(serviceId)) {
|
if (isExcludeService(serviceId)) {
|
||||||
return false;
|
return false;
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package com.gitee.sop.gatewaycommon.route;
|
package com.gitee.sop.gatewaycommon.route;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@@ -70,6 +70,7 @@ public class ServiceConfiguration implements WebMvcConfigurer {
|
|||||||
metadata.put("context-path", contextPath);
|
metadata.put("context-path", contextPath);
|
||||||
}
|
}
|
||||||
// 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题
|
// 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题
|
||||||
|
// 如果没有这个值,网关会忽略这个服务
|
||||||
metadata.put("time.startup", String.valueOf(System.currentTimeMillis()));
|
metadata.put("time.startup", String.valueOf(System.currentTimeMillis()));
|
||||||
return new NacosWatch(nacosDiscoveryProperties, taskScheduler);
|
return new NacosWatch(nacosDiscoveryProperties, taskScheduler);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user