详解feign调用session丢失解决方案

作者:zl1zl2zl3 时间:2021-08-29 20:38:20 

最近在做项目的时候发现,微服务使用feign相互之间调用时,存在session丢失的问题。例如,使用Feign调用某个远程API,这个远程API需要传递一个鉴权信息,我们可以把cookie里面的session信息放到Header里面,这个Header是动态的,跟你的HttpRequest相关,我们选择编写一个 * 来实现Header的传递,也就是需要实现RequestInterceptor接口,具体代码如下:


@Configuration
@EnableFeignClients(basePackages = "com.xxx.xxx.client")
public class FeignClientsConfigurationCustom implements RequestInterceptor {

@Override
public void apply(RequestTemplate template) {

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
 if (requestAttributes == null) {
  return;
 }

HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
 Enumeration<String> headerNames = request.getHeaderNames();
 if (headerNames != null) {
  while (headerNames.hasMoreElements()) {
   String name = headerNames.nextElement();
   Enumeration<String> values = request.getHeaders(name);
   while (values.hasMoreElements()) {
    String value = values.nextElement();
    template.header(name, value);
   }
  }
 }

}

}

经过测试,上面的解决方案可以正常的使用; 

但是,当引入Hystrix熔断策略时,出现了一个新的问题:


RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

此时requestAttributes会返回null,从而无法传递session信息,最终发现RequestContextHolder.getRequestAttributes(),该方法是从ThreadLocal变量里面取得对应信息的,这就找到问题原因了,是由于Hystrix熔断机制导致的。 
Hystrix有2个隔离策略:THREAD以及SEMAPHORE,当隔离策略为 THREAD 时,是没办法拿到 ThreadLocal 中的值的。

因此有两种解决方案:

方案一:调整格隔离策略:


hystrix.command.default.execution.isolation.strategy: SEMAPHORE

这样配置后,Feign可以正常工作。

但该方案不是特别好。原因是Hystrix官方强烈建议使用THREAD作为隔离策略! 可以参考官方文档说明。

方案二:自定义策略

记得之前在研究zipkin日志追踪的时候,看到过Sleuth有自己的熔断机制,用来在thread之间传递Trace信息,Sleuth是可以拿到自己上下文信息的,查看源码找到了 
org.springframework.cloud.sleuth.instrument.hystrix.SleuthHystrixConcurrencyStrategy 
这个类,查看SleuthHystrixConcurrencyStrategy的源码,继承了HystrixConcurrencyStrategy,用来实现了自己的并发策略。


/**
* Abstract class for defining different behavior or implementations for concurrency related aspects of the system with default implementations.
* <p>
* For example, every {@link Callable} executed by {@link HystrixCommand} will call {@link #wrapCallable(Callable)} to give a chance for custom implementations to decorate the {@link Callable} with
* additional behavior.
* <p>
* When you implement a concrete {@link HystrixConcurrencyStrategy}, you should make the strategy idempotent w.r.t ThreadLocals.
* Since the usage of threads by Hystrix is internal, Hystrix does not attempt to apply the strategy in an idempotent way.
* Instead, you should write your strategy to work idempotently. See https://github.com/Netflix/Hystrix/issues/351 for a more detailed discussion.
* <p>
* See {@link HystrixPlugins} or the Hystrix GitHub Wiki for information on configuring plugins: <a
* href="https://github.com/Netflix/Hystrix/wiki/Plugins" rel="external nofollow" >https://github.com/Netflix/Hystrix/wiki/Plugins</a>.
*/
public abstract class HystrixConcurrencyStrategy

搜索发现有好几个地方继承了HystrixConcurrencyStrategy类 

详解feign调用session丢失解决方案

其中就有我们熟悉的Spring Security和刚才提到的Sleuth都是使用了自定义的策略,同时由于Hystrix只允许有一个并发策略,因此为了不影响Spring Security和Sleuth,我们可以参考他们的策略实现自己的策略,大致思路: 

将现有的并发策略作为新并发策略的成员变量; 

在新并发策略中,返回现有并发策略的线程池、Queue; 

代码如下:


public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {

private static final Logger log = LoggerFactory.getLogger(FeignHystrixConcurrencyStrategy.class);
private HystrixConcurrencyStrategy delegate;

public FeignHystrixConcurrencyStrategy() {
 try {
  this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
  if (this.delegate instanceof FeignHystrixConcurrencyStrategy) {
   // Welcome to singleton hell...
   return;
  }
  HystrixCommandExecutionHook commandExecutionHook =
    HystrixPlugins.getInstance().getCommandExecutionHook();
  HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
  HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
  HystrixPropertiesStrategy propertiesStrategy =
    HystrixPlugins.getInstance().getPropertiesStrategy();
  this.logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher, propertiesStrategy);
  HystrixPlugins.reset();
  HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
  HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
  HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
  HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
  HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
 } catch (Exception e) {
  log.error("Failed to register Sleuth Hystrix Concurrency Strategy", e);
 }
}

private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
  HystrixMetricsPublisher metricsPublisher, HystrixPropertiesStrategy propertiesStrategy) {
 if (log.isDebugEnabled()) {
  log.debug("Current Hystrix plugins configuration is [" + "concurrencyStrategy ["
    + this.delegate + "]," + "eventNotifier [" + eventNotifier + "]," + "metricPublisher ["
    + metricsPublisher + "]," + "propertiesStrategy [" + propertiesStrategy + "]," + "]");
  log.debug("Registering Sleuth Hystrix Concurrency Strategy.");
 }
}

@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
 return new WrappedCallable<>(callable, requestAttributes);
}

@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
  HystrixProperty<Integer> corePoolSize, HystrixProperty<Integer> maximumPoolSize,
  HystrixProperty<Integer> keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
 return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime,
   unit, workQueue);
}

@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
  HystrixThreadPoolProperties threadPoolProperties) {
 return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
}

@Override
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
 return this.delegate.getBlockingQueue(maxQueueSize);
}

@Override
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
 return this.delegate.getRequestVariable(rv);
}

static class WrappedCallable<T> implements Callable<T> {
 private final Callable<T> target;
 private final RequestAttributes requestAttributes;

public WrappedCallable(Callable<T> target, RequestAttributes requestAttributes) {
  this.target = target;
  this.requestAttributes = requestAttributes;
 }

@Override
 public T call() throws Exception {
  try {
   RequestContextHolder.setRequestAttributes(requestAttributes);
   return target.call();
  } finally {
   RequestContextHolder.resetRequestAttributes();
  }
 }
}
}

最后,将这个策略类作为bean配置到feign的配置类FeignClientsConfigurationCustom中


@Bean
public FeignHystrixConcurrencyStrategy feignHystrixConcurrencyStrategy() {
 return new FeignHystrixConcurrencyStrategy();
}

至此,结合FeignClientsConfigurationCustom配置feign调用session丢失的问题完美解决。

来源:https://blog.csdn.net/zl1zl2zl3/article/details/79084368

标签:feign,session,丢失
0
投稿

猜你喜欢

  • Maven 多profile及指定编译问题的解决

    2022-04-22 23:43:24
  • 关于Spring事务隔离、传播属性与@Transactional注解

    2021-11-15 04:37:11
  • SpringBoot系列教程之防重放与操作幂等

    2021-12-07 11:51:12
  • JVM 心得分享(加载 链接 初始化)

    2023-09-01 19:17:50
  • 深入Java Final

    2022-06-22 09:38:14
  • java链式创建json对象的实现

    2023-11-12 12:36:51
  • Java集合TreeSet用法详解

    2023-11-10 22:53:34
  • spring循环依赖策略解析

    2022-09-13 13:49:10
  • springBoot项目如何实现启动多个实例

    2021-09-22 00:13:47
  • nacos使用占位符${}进行参数配置的方法

    2022-07-15 08:59:07
  • 使用Flutter实现一个走马灯布局的示例代码

    2023-06-19 03:50:03
  • Java设计模式之接口隔离原则精解

    2022-05-05 16:42:05
  • Java8通过Function获取字段名的步骤

    2022-07-14 14:06:23
  • Java Web Fragment在项目中使用方法详解

    2022-04-11 14:17:38
  • Linux下动静态库的打包与使用指南(C/C++)

    2023-06-21 20:22:11
  • 一文带你了解Java选择排序的原理与实现

    2022-05-13 21:01:31
  • C#数据类型转换(显式转型、隐式转型、强制转型)

    2021-11-24 13:44:25
  • Maven引入本地Jar包并打包进War包中的方法

    2023-06-16 12:43:28
  • Seata AT模式如何实现行锁详解

    2022-11-18 23:43:34
  • mybatis快速上手并运行程序

    2022-09-24 07:56:08
  • asp之家 软件编程 m.aspxhome.com