eureka
tanghc 4 years ago
parent 8052f308dd
commit c555b5aa33
  1. 2
      sop-admin/sop-admin-server/pom.xml
  2. 20
      sop-common/sop-bridge-eureka/src/main/java/com/gitee/sop/bridge/route/EurekaRegistryListener.java
  3. 24
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.java
  4. 2
      sop-example/sop-story/src/main/resources/application-dev.properties
  5. 2
      sop-gateway/pom.xml
  6. 2
      sop-website/pom.xml

@ -24,7 +24,7 @@
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-bridge-eureka</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.3-SNAPSHOT</version>
</dependency>
<!-- easyopen starter -->

@ -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<InstanceInfo> 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());

@ -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> 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);
}
@Bean
@ConditionalOnMissingBean
GlobalExceptionHandler globalExceptionHandler() {

@ -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/

@ -24,7 +24,7 @@
<groupId>com.gitee.sop</groupId>
<!-- <artifactId>sop-bridge-nacos</artifactId>-->
<artifactId>sop-bridge-eureka</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.3-SNAPSHOT</version>
</dependency>
<dependency>

@ -25,7 +25,7 @@
<groupId>com.gitee.sop</groupId>
<!-- <artifactId>sop-bridge-nacos</artifactId>-->
<artifactId>sop-bridge-eureka</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.3-SNAPSHOT</version>
</dependency>
<dependency>

Loading…
Cancel
Save