一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解feign調用session丟失解決方案

詳解feign調用session丟失解決方案

2021-07-16 15:00zl1zl2zl3 Java教程

這篇文章主要介紹了詳解feign調用session丟失解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

最近在做項目的時候發現,微服務使用feign相互之間調用時,存在session丟失的問題。例如,使用feign調用某個遠程api,這個遠程api需要傳遞一個鑒權信息,我們可以把cookie里面的session信息放到header里面,這個header是動態的,跟你的httprequest相關,我們選擇編寫一個攔截器來實現header的傳遞,也就是需要實現requestinterceptor接口,具體代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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熔斷策略時,出現了一個新的問題:

?
1
requestattributes requestattributes = requestcontextholder.getrequestattributes();

此時requestattributes會返回null,從而無法傳遞session信息,最終發現requestcontextholder.getrequestattributes(),該方法是從threadlocal變量里面取得對應信息的,這就找到問題原因了,是由于hystrix熔斷機制導致的。 
hystrix有2個隔離策略:thread以及semaphore,當隔離策略為 thread 時,是沒辦法拿到 threadlocal 中的值的。

因此有兩種解決方案:

方案一:調整格隔離策略:

?
1
hystrix.command.default.execution.isolation.strategy: semaphore

這樣配置后,feign可以正常工作。

但該方案不是特別好。原因是hystrix官方強烈建議使用thread作為隔離策略! 可以參考官方文檔說明。

方案二:自定義策略

記得之前在研究zipkin日志追蹤的時候,看到過sleuth有自己的熔斷機制,用來在thread之間傳遞trace信息,sleuth是可以拿到自己上下文信息的,查看源碼找到了 
org.springframework.cloud.sleuth.instrument.hystrix.sleuthhystrixconcurrencystrategy 
這個類,查看sleuthhystrixconcurrencystrategy的源碼,繼承了hystrixconcurrencystrategy,用來實現了自己的并發策略。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * 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; 

代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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中

?
1
2
3
4
@bean
public feignhystrixconcurrencystrategy feignhystrixconcurrencystrategy() {
 return new feignhystrixconcurrencystrategy();
}

至此,結合feignclientsconfigurationcustom配置feign調用session丟失的問題完美解決。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。 

原文鏈接:https://blog.csdn.net/zl1zl2zl3/article/details/79084368

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: japanesen女同| 精品一久久香蕉国产线看观 | 四缺一小说 | 精品99在线观看 | 欧美jjvideo| 韩国男女做性全过程视频 | 色综合视频在线观看 | 糖心hd在线观看 | 久久久无码精品亚洲欧美 | 国产成人精品实拍在线 | 国产成人在线影院 | 18捆绑调教在线高清 | 男人j进女屁股视频在线观看 | 亚洲 欧美 国产 综合 在线 | 亚洲欧美优优色在线影院 | www.精品在线| 国产高清在线精品一区二区 | 99午夜高清在线视频在观看 | 欧美二区视频 | 亚洲精品乱码久久久久久蜜桃 | 国产剧情一区 | 欧美a一片xxxx片与善交 | 青草视频免费观看 | 国产精品久久久久毛片真精品 | 日韩亚洲国产欧美精品 | 天天夜夜草草久久伊人天堂 | 日韩精品一区二区三区免费视频 | 色视频综合| 国产91素人搭讪系列天堂 | 成人午夜爽爽爽免费视频 | 美女机巴 | 俄罗斯三级完整版在线观看 | 九九在线精品视频 | 欧美成人免费观看的 | 欧美又硬又粗又长又大 | 日韩精选 | 日本成人免费在线视频 | 国产福利一区二区三区四区 | 午夜视频网站 | 亚洲毛片免费看 | 国产自拍专区 |