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 7c04cec7..c6d56b4c 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,6 +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"; + public static final String METADATA_KEY_TIME_STARTUP = "server.startup-time"; } 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 9067984a..1f302e88 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,88 +1,37 @@ package com.gitee.sop.servercommon.configuration; -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 com.alibaba.cloud.nacos.NacosDiscoveryProperties; +import com.alibaba.cloud.nacos.discovery.NacosWatch; 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.http.converter.HttpMessageConverter; -import org.springframework.http.converter.StringHttpMessageConverter; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.core.env.Environment; +import org.springframework.scheduling.TaskScheduler; -import javax.annotation.PostConstruct; -import java.nio.charset.StandardCharsets; -import java.util.List; +import java.util.Map; /** * @author tanghc */ @Slf4j -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"); - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - // 支持swagger-bootstrap-ui首页 - registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); - // 支持默认swagger - registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); - registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); - } - - @Override - public void extendMessageConverters(List> converters) { - // 解决controller返回字符串中文乱码问题 - for (HttpMessageConverter converter : converters) { - if (converter instanceof StringHttpMessageConverter) { - ((StringHttpMessageConverter)converter).setDefaultCharset(StandardCharsets.UTF_8); - } - } - } - - @Override - public void addInterceptors(InterceptorRegistry registry) { - // 添加拦截器 - registry.addInterceptor(new ServiceContextInterceptor()); - } +public class ServiceConfiguration extends SpringmvcConfiguration { @Bean @ConditionalOnMissingBean - GlobalExceptionHandler globalExceptionHandler() { - return new GlobalExceptionHandler(); - } - - @Bean - @ConditionalOnMissingBean - ServiceRouteController serviceRouteInfoHandler() { - return new ServiceRouteController(); - } - - @PostConstruct - public final void after() { - log.info("-----spring容器加载完毕-----"); - initMessage(); - doAfter(); - } - - - /** - * spring容器加载完毕后执行 - */ - protected void doAfter() { - - } - - - protected void initMessage() { - ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules()); + @ConditionalOnProperty("spring.cloud.nacos.discovery.server-addr") + public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties, ObjectProvider taskScheduler, Environment environment) { + Map metadata = nacosDiscoveryProperties.getMetadata(); + String contextPath = environment.getProperty(METADATA_SERVER_CONTEXT_PATH); + // 将context-path信息加入到metadata中 + if (contextPath != null) { + metadata.put(METADATA_SERVER_CONTEXT_PATH, contextPath); + } + // 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题 + // 如果没有这个值,网关会忽略这个服务 + metadata.put("server.startup-time", String.valueOf(System.currentTimeMillis())); + return new NacosWatch(nacosDiscoveryProperties, taskScheduler); } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringmvcConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringmvcConfiguration.java new file mode 100644 index 00000000..4162f0f8 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringmvcConfiguration.java @@ -0,0 +1,89 @@ +package com.gitee.sop.servercommon.configuration; + +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.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.annotation.PostConstruct; +import java.nio.charset.StandardCharsets; +import java.util.List; + +/** + * @author tanghc + */ +@Slf4j +public class SpringmvcConfiguration implements WebMvcConfigurer { + + public static final String METADATA_SERVER_CONTEXT_PATH = "server.servlet.context-path"; + + public SpringmvcConfiguration() { + ServiceConfig.getInstance().getI18nModules().add("i18n/isp/bizerror"); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // 支持swagger-bootstrap-ui首页 + registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); + // 支持默认swagger + registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } + + @Override + public void extendMessageConverters(List> converters) { + // 解决controller返回字符串中文乱码问题 + for (HttpMessageConverter converter : converters) { + if (converter instanceof StringHttpMessageConverter) { + ((StringHttpMessageConverter)converter).setDefaultCharset(StandardCharsets.UTF_8); + } + } + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 添加拦截器 + registry.addInterceptor(new ServiceContextInterceptor()); + } + + @Bean + @ConditionalOnMissingBean + GlobalExceptionHandler globalExceptionHandler() { + return new GlobalExceptionHandler(); + } + + @Bean + @ConditionalOnMissingBean + ServiceRouteController serviceRouteInfoHandler() { + return new ServiceRouteController(); + } + + @PostConstruct + public final void after() { + log.info("-----spring容器加载完毕-----"); + initMessage(); + doAfter(); + } + + + /** + * spring容器加载完毕后执行 + */ + protected void doAfter() { + + } + + + protected void initMessage() { + ServiceErrorFactory.initMessageSource(ServiceConfig.getInstance().getI18nModules()); + } + +} diff --git a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java index 5c2bd1c8..f9ddde68 100644 --- a/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java +++ b/sop-example/sop-springmvc/src/main/java/com/gitee/app/config/OpenServiceConfig.java @@ -1,14 +1,13 @@ package com.gitee.app.config; import com.alibaba.nacos.api.annotation.NacosInjected; -import com.alibaba.nacos.api.annotation.NacosProperties; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.client.naming.utils.NetUtils; import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery; import com.gitee.sop.servercommon.bean.ServiceConfig; -import com.gitee.sop.servercommon.configuration.ServiceConfiguration; +import com.gitee.sop.servercommon.configuration.SpringmvcConfiguration; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -19,13 +18,14 @@ import org.springframework.beans.factory.annotation.Value; */ @Slf4j @EnableNacosDiscovery -public class OpenServiceConfig extends ServiceConfiguration { +public class OpenServiceConfig extends SpringmvcConfiguration { public static final String SPRING_APPLICATION_NAME = "spring.application.name"; public static final String SERVER_CONTEXT_PATH = "server.servlet.context-path"; public static final String SERVER_IP = "server.ip"; public static final String SERVER_PORT = "server.port"; + public static final String METADATA_TIME_STARTUP = "server.startup-time"; static { ServiceConfig.getInstance().setDefaultVersion("1.0"); @@ -65,7 +65,8 @@ public class OpenServiceConfig extends ServiceConfiguration { instance.setServiceName(serviceId); instance.setIp(ip); instance.setPort(port); - instance.getMetadata().put(SERVER_CONTEXT_PATH, contextPath); + instance.getMetadata().put(METADATA_SERVER_CONTEXT_PATH, contextPath); + instance.getMetadata().put(METADATA_TIME_STARTUP, String.valueOf(System.currentTimeMillis())); return instance; }