首页 > 其他分享 >Springcloud~openfeign开启hystrix基于线程池熔断的传值问题

Springcloud~openfeign开启hystrix基于线程池熔断的传值问题

时间:2023-02-24 11:12:24浏览次数:38  
标签:NextHttpHeader return hystrix openfeign get Springcloud inheritableThreadLocal n

我们都知道,在hystrix默认情况下,采用线程池的熔断方式,每个openfeign都有自己的线程,而这使得它无法获取主线程上的变量;现在有个现实的问题就是,当前端登录后,把token经过gateway传到服务A,服务A再调用服务B时,B是无法收到请求头里的token信息的;一种不太好的解决方案就是使用信号量的方式。

使用ThreadLocal存储变量

public class NextHttpHeader {

	static final InheritableThreadLocal<Map<String, String>> inheritableThreadLocal = new InheritableThreadLocal<>();
	public static void set(String key, String val) {
		if (inheritableThreadLocal.get() == null) {
			inheritableThreadLocal.set(MapUtils.<String, String>hashMapBuilder(8).put(key, val).build());
		} else {
			inheritableThreadLocal.get().put(key, val);
		}
	}

	public static String get(String key) {
		if (inheritableThreadLocal.get() == null) {
			return null;
		}
		return inheritableThreadLocal.get().get(key);
	}

	public static Set<String> get() {
		if (inheritableThreadLocal.get() == null) {
			return null;
		}
		return inheritableThreadLocal.get().keySet();
	}


}

继承HystrixConcurrencyStrategy抽象类,实现自己的赋值逻辑

public class RequestContextHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {

	public <T> Callable<T> wrapCallable(Callable<T> callable) {
		// 先包装一下要执行的任务,在这里把ThreadLocal的值取出来
		return new ThreadLocalCallable<T>(callable);
	}

	public static class ThreadLocalCallable<V> implements Callable<V> {

		private Callable<V> target;

		private Map<String, String> dic = new HashMap<>();

		public ThreadLocalCallable(Callable<V> target) {
			this.target = target;
			NextHttpHeader.get().forEach(o -> {
				this.dic.put(o, NextHttpHeader.get(o));
			});
		}

		@Override
		public V call() throws Exception {
			this.dic.keySet().forEach(o -> {
				NextHttpHeader.set(o, this.dic.get(o));
			});
			return target.call();
		}

	}

}

注册

@Configuration
@Slf4j
public class HystrixCircuitBreakerConfiguration {

	@PostConstruct
	public void init() {
		HystrixPlugins.getInstance().registerConcurrencyStrategy(new RequestContextHystrixConcurrencyStrategy());
	}

}

发现openFeign请求时,将变量放到请求头

/**
 * 基于openFeign的拦截器,处理需要向下游传递的信息.
 *
 * @author lind
 * @date 2023/1/29 11:44
 * @since 1.0.0
 */
@Configuration
public class FeignTraceIdInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate template) {

		if (NextHttpHeader.get() != null) {
			NextHttpHeader.get().forEach(o -> {
				template.header(o, NextHttpHeader.get(o));
			});
		}
	}

}

spring中的口子太多,太灵活,需要不断的去找那个最优雅,最正确的方法

标签:NextHttpHeader,return,hystrix,openfeign,get,Springcloud,inheritableThreadLocal,n
From: https://www.cnblogs.com/lori/p/17150559.html

相关文章

  • Hystrix核⼼源码剖析
    springboot装配、⾯向切⾯编程、RxJava响应式编程的知识等等,我们剖析下主体脉络。 分析⼊⼝:@EnableCircuitBreaker注解激活了熔断功能,那么该注解就是Hystrix源码追踪的......
  • SpringCloud导入spring boot项目当作子模块微服务IDEA不识别子module问题
    1.在父工程下面引入module。<modules><module>study-design-mode</module></modules>2.点击子工程module中的pom文件,进行更新,引入成功。3.配置子工程的pom文件......
  • SpringCloud GateWay+Nacos 集群
    新建模块gateway port:8888 POM<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w......
  • javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String; spring
    2023-02-2111:44:13.924ERROR27256---[main]o.s.b.d.LoggingFailureAnalysisReporter:***************************APPLICATIONFAILEDTOSTART************......
  • SpringCloud集成Seata并使用Nacos做注册中心与配置中心
    本文为博主原创,未经允许不得转载:目录:1. 下载并启动SeataServer,并指定nacos作为配置中心和注册中心2. 同步seataserver的配置到nacos3.启动SeataServer......
  • Springcloud环境中bootstrap.yml加载原理
    如果是Springcloud项目,一般会将配置中心、注册中心等地址存入bootstrap.yml中,同时将其他的配置存入配置中心。也就是说bootstrap.yml的读取会比较靠前。下面研究其机......
  • CICD流水线 Jenkins + Docker compose 分环境 一键部署SpringCloud项目
    一、环境准备接上篇:上篇搭建好了Jenkins 环境 并把docker-compose.yml Dockerfile 相关jar包推送到了目标服务器。二、分环境部署1、SpringBoot配置pom.xml<pro......
  • openfeign源码浅析
    问题:1.@FeignClient怎么解析的。2.@FeignClient注解的接口放到spring容器里存的是什么。3.调用@FeignClien接口的方法是怎么执行的。 1.@FeignClient怎么解析的?首先......
  • springcloud day01
    单体架构:业务所有功能都在一个项目中开发,打成一个包部署优点是架构简单部署成本低缺点是耦合度高分布式架构:根据业务功能对系统做拆分,每个业务功能模块作为一个独立的......
  • SpringCloud Alibaba框架都有啥啊
    前言springcloud是一个基于springboot实现的微服务架构开发工具,使用springcloud框架进行微服务业务开发是java后端开发必备技能,目前主流的SpringCloud分为SpringCloudNet......