From 5aa11c734e12b15fa1b9fffd96177d36f5330807 Mon Sep 17 00:00:00 2001 From: tanghc Date: Tue, 18 Aug 2020 15:07:01 +0800 Subject: [PATCH] 4.0.3 --- .../configuration/ServiceConfiguration.java | 79 +--------------- .../configuration/SpringmvcConfiguration.java | 89 +++++++++++++++++++ .../gitee/app/config/OpenServiceConfig.java | 9 +- 3 files changed, 97 insertions(+), 80 deletions(-) create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/SpringmvcConfiguration.java 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..38df8f49 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 @@ -2,72 +2,31 @@ 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; -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() { - 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 @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"); + String contextPath = environment.getProperty(METADATA_SERVER_CONTEXT_PATH); // 将context-path信息加入到metadata中 if (contextPath != null) { - metadata.put("context-path", contextPath); + metadata.put(METADATA_SERVER_CONTEXT_PATH, contextPath); } // 在元数据中新增启动时间,不能修改这个值,不然网关拉取接口会有问题 // 如果没有这个值,网关会忽略这个服务 @@ -75,36 +34,4 @@ public class ServiceConfiguration implements WebMvcConfigurer { return new NacosWatch(nacosDiscoveryProperties, taskScheduler); } - @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-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..a1c427b2 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 = "time.startup"; 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; }