增加自定义负载均衡规则,实现服务实例版本号隔离
This commit is contained in:
+19
-2
@@ -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);
|
||||
}
|
||||
|
||||
+8
@@ -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";
|
||||
}
|
||||
|
||||
+14
@@ -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";
|
||||
}
|
||||
+25
@@ -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();
|
||||
}
|
||||
}
|
||||
+26
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user