升级Spring Boot到2.5.x,升级Spring Cloud到2020.x
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@
|
||||
<module>zlt-db-spring-boot-starter</module>
|
||||
<module>zlt-swagger2-spring-boot-starter</module>
|
||||
<module>zlt-log-spring-boot-starter</module>
|
||||
<module>zlt-ribbon-spring-boot-starter</module>
|
||||
<module>zlt-loadbalancer-spring-boot-starter</module>
|
||||
<module>zlt-auth-client-spring-boot-starter</module>
|
||||
<module>zlt-sentinel-spring-boot-starter</module>
|
||||
<module>zlt-common-core</module>
|
||||
|
||||
+4
-1
@@ -25,6 +25,9 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/10/28
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class DefaultPermissionServiceImpl {
|
||||
@@ -32,7 +35,7 @@ public abstract class DefaultPermissionServiceImpl {
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
private AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||
|
||||
/**
|
||||
* 查询当前用户拥有的资源权限
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package com.central.common.feign.fallback;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysUser;
|
||||
import feign.hystrix.FallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
|
||||
+48
-48
@@ -1,48 +1,48 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-commons</artifactId>
|
||||
<version>4.6.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>zlt-ribbon-spring-boot-starter</artifactId>
|
||||
<description>远程通信通用组件</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-commons</artifactId>
|
||||
<version>4.6.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>zlt-loadbalancer-spring-boot-starter</artifactId>
|
||||
<description>负载均衡通用组件</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zlt</groupId>
|
||||
<artifactId>zlt-common-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+80
-77
@@ -1,77 +1,80 @@
|
||||
package com.central.common.ribbon;
|
||||
|
||||
import com.central.common.ribbon.config.RestTemplateProperties;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/11/17
|
||||
*/
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RestTemplateAutoConfigure {
|
||||
@Autowired
|
||||
private RestTemplateProperties restTemplateProperties;
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setRequestFactory(httpRequestFactory());
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* httpclient 实现的ClientHttpRequestFactory
|
||||
*/
|
||||
@Bean
|
||||
public ClientHttpRequestFactory httpRequestFactory() {
|
||||
return new HttpComponentsClientHttpRequestFactory(httpClient());
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用连接池的 httpclient
|
||||
*/
|
||||
@Bean
|
||||
public HttpClient httpClient() {
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", SSLConnectionSocketFactory.getSocketFactory())
|
||||
.build();
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
|
||||
// 最大链接数
|
||||
connectionManager.setMaxTotal(restTemplateProperties.getMaxTotal());
|
||||
// 同路由并发数20
|
||||
connectionManager.setDefaultMaxPerRoute(restTemplateProperties.getMaxPerRoute());
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
// 读超时
|
||||
.setSocketTimeout(restTemplateProperties.getReadTimeout())
|
||||
// 链接超时
|
||||
.setConnectTimeout(restTemplateProperties.getConnectTimeout())
|
||||
// 链接不够用的等待时间
|
||||
.setConnectionRequestTimeout(restTemplateProperties.getReadTimeout())
|
||||
.build();
|
||||
|
||||
return HttpClientBuilder.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setConnectionManager(connectionManager)
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
package com.central.common.lb;
|
||||
|
||||
import com.central.common.lb.config.RestTemplateProperties;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2018/11/17
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RestTemplateAutoConfigure {
|
||||
@Autowired
|
||||
private RestTemplateProperties restTemplateProperties;
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory httpRequestFactory) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setRequestFactory(httpRequestFactory);
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* httpclient 实现的ClientHttpRequestFactory
|
||||
*/
|
||||
@Bean
|
||||
public ClientHttpRequestFactory httpRequestFactory(HttpClient httpClient) {
|
||||
return new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用连接池的 httpclient
|
||||
*/
|
||||
@Bean
|
||||
public HttpClient httpClient() {
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
||||
.register("http", PlainConnectionSocketFactory.getSocketFactory())
|
||||
.register("https", SSLConnectionSocketFactory.getSocketFactory())
|
||||
.build();
|
||||
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
|
||||
// 最大链接数
|
||||
connectionManager.setMaxTotal(restTemplateProperties.getMaxTotal());
|
||||
// 同路由并发数20
|
||||
connectionManager.setDefaultMaxPerRoute(restTemplateProperties.getMaxPerRoute());
|
||||
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
// 读超时
|
||||
.setSocketTimeout(restTemplateProperties.getReadTimeout())
|
||||
// 链接超时
|
||||
.setConnectTimeout(restTemplateProperties.getConnectTimeout())
|
||||
// 链接不够用的等待时间
|
||||
.setConnectionRequestTimeout(restTemplateProperties.getReadTimeout())
|
||||
.build();
|
||||
|
||||
return HttpClientBuilder.create()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setConnectionManager(connectionManager)
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
package com.central.common.ribbon.annotation;
|
||||
package com.central.common.lb.annotation;
|
||||
|
||||
import com.central.common.ribbon.config.FeignInterceptorConfig;
|
||||
import com.central.common.lb.config.FeignInterceptorConfig;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
@@ -22,4 +22,4 @@ import java.lang.annotation.Target;
|
||||
@Import(FeignInterceptorConfig.class)
|
||||
public @interface EnableBaseFeignInterceptor {
|
||||
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -1,7 +1,7 @@
|
||||
package com.central.common.ribbon.annotation;
|
||||
package com.central.common.lb.annotation;
|
||||
|
||||
import com.central.common.ribbon.config.FeignHttpInterceptorConfig;
|
||||
import com.central.common.ribbon.config.FeignInterceptorConfig;
|
||||
import com.central.common.lb.config.FeignHttpInterceptorConfig;
|
||||
import com.central.common.lb.config.FeignInterceptorConfig;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
@@ -14,9 +14,11 @@ import java.lang.annotation.Target;
|
||||
*
|
||||
* @author zlt
|
||||
*/
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Import({FeignInterceptorConfig.class, FeignHttpInterceptorConfig.class})
|
||||
public @interface EnableFeignInterceptor {
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.central.common.ribbon.config;
|
||||
package com.central.common.lb.config;
|
||||
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
+4
-1
@@ -1,4 +1,4 @@
|
||||
package com.central.common.ribbon.config;
|
||||
package com.central.common.lb.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
@@ -10,6 +10,9 @@ import org.springframework.context.annotation.Bean;
|
||||
* feign拦截器,只包含基础数据
|
||||
*
|
||||
* @author zlt
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class FeignInterceptorConfig {
|
||||
/**
|
||||
+36
-33
@@ -1,33 +1,36 @@
|
||||
package com.central.common.ribbon.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* RestTemplate 配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2017/11/17
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.rest-template")
|
||||
public class RestTemplateProperties {
|
||||
/**
|
||||
* 最大链接数
|
||||
*/
|
||||
private int maxTotal = 200;
|
||||
/**
|
||||
* 同路由最大并发数
|
||||
*/
|
||||
private int maxPerRoute = 50;
|
||||
/**
|
||||
* 读取超时时间 ms
|
||||
*/
|
||||
private int readTimeout = 35000;
|
||||
/**
|
||||
* 链接超时时间 ms
|
||||
*/
|
||||
private int connectTimeout = 10000;
|
||||
}
|
||||
package com.central.common.lb.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* RestTemplate 配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2017/11/17
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.rest-template")
|
||||
public class RestTemplateProperties {
|
||||
/**
|
||||
* 最大链接数
|
||||
*/
|
||||
private int maxTotal = 200;
|
||||
/**
|
||||
* 同路由最大并发数
|
||||
*/
|
||||
private int maxPerRoute = 50;
|
||||
/**
|
||||
* 读取超时时间 ms
|
||||
*/
|
||||
private int readTimeout = 35000;
|
||||
/**
|
||||
* 链接超时时间 ms
|
||||
*/
|
||||
private int connectTimeout = 10000;
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.common.lb.RestTemplateAutoConfigure
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.central.common.ribbon;
|
||||
|
||||
import feign.Logger;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Feign统一配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/9/18 14:04
|
||||
*/
|
||||
public class FeignAutoConfigure {
|
||||
|
||||
/**
|
||||
* Feign 日志级别
|
||||
*/
|
||||
@Bean
|
||||
Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.central.common.ribbon;
|
||||
|
||||
import com.central.common.constant.ConfigConstants;
|
||||
import com.central.common.ribbon.config.RuleConfigure;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
|
||||
/**
|
||||
* 自定义负载均衡配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/3
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@ConditionalOnProperty(value = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
|
||||
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
|
||||
public class LbIsolationAutoConfigure {
|
||||
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package com.central.common.ribbon;
|
||||
|
||||
import com.central.common.ribbon.config.RestTemplateProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Ribbon扩展配置类
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/11/17 9:24
|
||||
*/
|
||||
@EnableConfigurationProperties(RestTemplateProperties.class)
|
||||
public class RibbonAutoConfigure {
|
||||
@Bean
|
||||
public DefaultPropertiesFactory defaultPropertiesFactory() {
|
||||
return new DefaultPropertiesFactory();
|
||||
}
|
||||
}
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.central.common.ribbon.config;
|
||||
|
||||
import com.alibaba.cloud.nacos.ribbon.NacosServer;
|
||||
import com.central.common.ribbon.rule.VersionIsolationRule;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2020/4/24
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class RuleConfigure {
|
||||
@Bean
|
||||
@ConditionalOnClass(NacosServer.class)
|
||||
@ConditionalOnMissingBean
|
||||
public IRule versionIsolationRule() {
|
||||
return new VersionIsolationRule();
|
||||
}
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package com.central.common.ribbon.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.ConfigConstants;
|
||||
import com.central.common.context.LbIsolationContextHolder;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 负载均衡隔离规则过滤器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/15
|
||||
*/
|
||||
@ConditionalOnClass(Filter.class)
|
||||
public class LbIsolationFilter extends OncePerRequestFilter {
|
||||
@Value("${" + ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED + ":false}")
|
||||
private boolean enableIsolation;
|
||||
|
||||
@Override
|
||||
protected boolean shouldNotFilter(HttpServletRequest request) {
|
||||
return !enableIsolation;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain filterChain) throws IOException, ServletException {
|
||||
try {
|
||||
String version = request.getHeader(CommonConstant.Z_L_T_VERSION);
|
||||
if(StrUtil.isNotEmpty(version)){
|
||||
LbIsolationContextHolder.setVersion(version);
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
LbIsolationContextHolder.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
-80
@@ -1,80 +0,0 @@
|
||||
package com.central.common.ribbon.rule;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.cloud.nacos.ribbon.NacosServer;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.context.LbIsolationContextHolder;
|
||||
import com.netflix.loadbalancer.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 根据实例版本号隔离规则
|
||||
* 详细用法:https://mp.weixin.qq.com/s/9XQ-SIbYsov3KBx9TGFN0g
|
||||
*
|
||||
* 实例获取规则顺序如下(不满足则走下一个规则):
|
||||
* 1. 相同版本号的实例
|
||||
* 2. 无版本号的实例
|
||||
* 3. 所有实例中轮询
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/9/3
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class VersionIsolationRule extends RoundRobinRule {
|
||||
private final static String KEY_DEFAULT = "default";
|
||||
/**
|
||||
* 优先根据版本号取实例
|
||||
*/
|
||||
@Override
|
||||
public Server choose(ILoadBalancer lb, Object key) {
|
||||
if (lb == null) {
|
||||
return null;
|
||||
}
|
||||
String version;
|
||||
if (key != null && !KEY_DEFAULT.equals(key)) {
|
||||
version = key.toString();
|
||||
} else {
|
||||
version = LbIsolationContextHolder.getVersion();
|
||||
}
|
||||
|
||||
List<Server> targetList = null;
|
||||
List<Server> upList = lb.getReachableServers();
|
||||
if (StrUtil.isNotEmpty(version)) {
|
||||
//取指定版本号的实例
|
||||
targetList = upList.stream().filter(
|
||||
server -> version.equals(
|
||||
((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION)
|
||||
)
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
if (CollUtil.isEmpty(targetList)) {
|
||||
//只取无版本号的实例
|
||||
targetList = upList.stream().filter(
|
||||
server -> {
|
||||
String metadataVersion = ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION);
|
||||
return StrUtil.isEmpty(metadataVersion);
|
||||
}
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
if (CollUtil.isNotEmpty(targetList)) {
|
||||
return getServer(targetList);
|
||||
}
|
||||
return super.choose(lb, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机取一个实例
|
||||
*/
|
||||
private Server getServer(List<Server> upList) {
|
||||
int nextInt = RandomUtil.randomInt(upList.size());
|
||||
return upList.get(nextInt);
|
||||
}
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package org.springframework.cloud.netflix.ribbon;
|
||||
|
||||
import com.netflix.loadbalancer.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.cloud.netflix.ribbon.SpringClientFactory.NAMESPACE;
|
||||
|
||||
/**
|
||||
* 扩展 spring cloud ribbon的PropertiesFactory
|
||||
* 使其能够支持 配置全局的ribbon.NFLoadBalancerRuleClassName=package.YourRule
|
||||
* 然后各个微服务还可以根据自身情况做个性化定制。如:SERVICE_ID.ribbon.NFLoadBalancerRuleClassName=package.YourRule
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/11/17 9:29
|
||||
*/
|
||||
public class DefaultPropertiesFactory extends PropertiesFactory {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
private Map<Class, String> classToProperty = new HashMap<>(5);
|
||||
|
||||
public DefaultPropertiesFactory() {
|
||||
super();
|
||||
classToProperty.put(ILoadBalancer.class, "NFLoadBalancerClassName");
|
||||
classToProperty.put(IPing.class, "NFLoadBalancerPingClassName");
|
||||
classToProperty.put(IRule.class, "NFLoadBalancerRuleClassName");
|
||||
classToProperty.put(ServerList.class, "NIWSServerListClassName");
|
||||
classToProperty.put(ServerListFilter.class, "NIWSServerListFilterClassName");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写 支持 ribbon.NFLoadBalancerRuleClassName=package.YourRule 全局配置的方式
|
||||
*/
|
||||
@Override
|
||||
public String getClassName(Class clazz, String name) {
|
||||
String className = super.getClassName(clazz, name);
|
||||
// 读取全局配置
|
||||
if(!StringUtils.hasText(className) && this.classToProperty.containsKey(clazz)){
|
||||
String classNameProperty = this.classToProperty.get(clazz);
|
||||
className = environment.getProperty(NAMESPACE + "." + classNameProperty);
|
||||
}
|
||||
return className;
|
||||
}
|
||||
}
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.common.ribbon.RibbonAutoConfigure,\
|
||||
com.central.common.ribbon.FeignAutoConfigure,\
|
||||
com.central.common.ribbon.RestTemplateAutoConfigure,\
|
||||
com.central.common.ribbon.filter.LbIsolationFilter,\
|
||||
com.central.common.ribbon.LbIsolationAutoConfigure
|
||||
Reference in New Issue
Block a user