# Conflicts: # sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/ServiceConfiguration.javaeureka
commit
b05e0409da
@ -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<HttpMessageConverter<?>> 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> taskScheduler, Environment environment) { |
||||
Map<String, String> 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); |
||||
} |
||||
|
||||
} |
||||
|
@ -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<HttpMessageConverter<?>> 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()); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue