增加自定义负载均衡规则,实现服务实例版本号隔离

This commit is contained in:
zlt
2019-09-04 12:08:38 +08:00
parent 1c17de3292
commit b060d8ee16
15 changed files with 254 additions and 5 deletions
@@ -1,11 +1,15 @@
package com.central.common.config;
import com.central.common.constant.ConfigConstants;
import com.central.common.feign.UserService;
import com.central.common.interceptor.LbIsolationInterceptor;
import com.central.common.interceptor.TenantInterceptor;
import com.central.common.interceptor.TraceInterceptor;
import com.central.common.resolver.ClientArgumentResolver;
import com.central.common.resolver.TokenArgumentResolver;
import com.central.log.properties.TraceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@@ -23,6 +27,12 @@ public class DefaultWebMvcConfig extends WebMvcConfigurationSupport {
@Autowired
private UserService userService;
@Autowired
private TraceProperties traceProperties;
@Value("${" + ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED + ":false}")
private boolean enableIsolation;
/**
* 配置SpringMVC拦截器,添加租户拦截器
*/
@@ -31,8 +41,15 @@ public class DefaultWebMvcConfig extends WebMvcConfigurationSupport {
//租户拦截器
registry.addInterceptor(new TenantInterceptor()).addPathPatterns("/**");
//日志链路追踪拦截器
registry.addInterceptor(new TraceInterceptor()).addPathPatterns("/**");
if (traceProperties.getEnable()) {
//日志链路追踪拦截器
registry.addInterceptor(new TraceInterceptor()).addPathPatterns("/**");
}
if (enableIsolation) {
//负债均衡隔离规则拦截器
registry.addInterceptor(new LbIsolationInterceptor()).addPathPatterns("/**");
}
super.addInterceptors(registry);
}
@@ -117,4 +117,12 @@ public interface CommonConstant {
* 日志链路追踪id日志标志
*/
String LOG_TRACE_ID = "traceId";
/**
* 负载均衡策略-版本号 信息头
*/
String Z_L_T_VERSION = "z-l-t-version";
/**
* 注册中心元数据 版本号
*/
String METADATA_VERSION = "version";
}
@@ -0,0 +1,14 @@
package com.central.common.constant;
/**
* 配置项常量
*
* @author zlt
* @date 2019/9/3
*/
public interface ConfigConstants {
/**
* 是否开启自定义隔离规则
*/
String CONFIG_RIBBON_ISOLATION_ENABLED = "zlt.ribbon.isolation.enabled";
}
@@ -0,0 +1,25 @@
package com.central.common.context;
import com.alibaba.ttl.TransmittableThreadLocal;
/**
* 负载均衡策略Holder
*
* @author zlt
* @date 2019/9/2
*/
public class LbIsolationContextHolder {
private static final ThreadLocal<String> VERSION_CONTEXT = new TransmittableThreadLocal<>();
public static void setVersion(String version) {
VERSION_CONTEXT.set(version);
}
public static String getVersion() {
return VERSION_CONTEXT.get();
}
public static void clear() {
VERSION_CONTEXT.remove();
}
}
@@ -0,0 +1,26 @@
package com.central.common.interceptor;
import cn.hutool.core.util.StrUtil;
import com.central.common.constant.CommonConstant;
import com.central.common.context.LbIsolationContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 负载均衡隔离规则截器
*
* @author zlt
* @date 2019/8/5
*/
public class LbIsolationInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String version = request.getHeader(CommonConstant.Z_L_T_VERSION);
if(StrUtil.isNotEmpty(version)){
LbIsolationContextHolder.setVersion(version);
}
return true;
}
}
@@ -24,6 +24,11 @@
<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>
@@ -1,9 +1,14 @@
package com.central.common.ribbon;
import com.central.common.constant.ConfigConstants;
import com.central.common.ribbon.config.RestTemplateProperties;
import com.central.common.ribbon.config.RuleConfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.ribbon.DefaultPropertiesFactory;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Ribbon扩展配置类
@@ -12,9 +17,15 @@ import org.springframework.context.annotation.Bean;
* @date 2018/11/17 9:24
*/
@EnableConfigurationProperties(RestTemplateProperties.class)
public class RibbonAutoConfigure {
public class RibbonAutoConfigure {
@Bean
public DefaultPropertiesFactory defaultPropertiesFactory() {
return new DefaultPropertiesFactory();
}
@Configuration
@ConditionalOnProperty(value = ConfigConstants.CONFIG_RIBBON_ISOLATION_ENABLED, havingValue = "true")
@RibbonClients(defaultConfiguration = {RuleConfigure.class})
public class LbIsolationConfig {
}
}
@@ -29,6 +29,7 @@ public class FeignInterceptorConfig {
requestHeaders.add(SecurityConstants.USER_ID_HEADER);
requestHeaders.add(SecurityConstants.USER_HEADER);
requestHeaders.add(SecurityConstants.ROLE_HEADER);
requestHeaders.add(CommonConstant.Z_L_T_VERSION);
}
/**
@@ -0,0 +1,16 @@
package com.central.common.ribbon.config;
import com.central.common.ribbon.rule.CustomIsolationRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
/**
* @author zlt
* @date 2019/9/3
*/
public class RuleConfigure {
@Bean
public IRule isolationRule() {
return new CustomIsolationRule();
}
}
@@ -0,0 +1,64 @@
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;
/**
* 自定义隔离随机规则
*
* @author zlt
* @date 2019/9/3
*/
public class CustomIsolationRule extends RoundRobinRule {
/**
* 优先根据版本号取实例
*/
@Override
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
}
String 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);
}
}