提交 877a2e7d 编写于 作者: J jarvis

feat: loadBalancer兼容gateway和普通http请求,后续增加webflux版本控制

上级 979e91c4
package com.central.common.lb.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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;
/**
* web过滤器
* 作用:将请求中的存放在参数和header的版本号获取出来并存入TreadLocal中
*
* @author jarvis create by 2022/3/9
*/
@ConditionalOnClass(Filter.class)
public class LbIsolationFilter extends OncePerRequestFilter {
@Value("${" + ConfigConstants.CONFIG_LOADBALANCE_ISOLATION_ENABLE + ":false}")
private boolean enableIsolation;
@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
return !enableIsolation;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
try {
String version = StringUtils.isNotBlank(request.getParameter(CommonConstant.Z_L_T_VERSION))?
request.getParameter(CommonConstant.Z_L_T_VERSION):
request.getHeader(CommonConstant.Z_L_T_VERSION);
if(StrUtil.isNotEmpty(version)){
LbIsolationContextHolder.setVersion(version);
}
filterChain.doFilter(request, response);
} finally {
LbIsolationContextHolder.clear();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册