最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud Alibaba微服務(wù)實(shí)戰(zhàn)之遠(yuǎn)程Feign請(qǐng)求頭丟失問(wèn)題解決方案

 更新時(shí)間:2024年02月20日 14:39:46   作者:竹林幽深  
這篇文章主要介紹了SpringCloud Alibaba微服務(wù)實(shí)戰(zhàn)之遠(yuǎn)程Feign請(qǐng)求頭丟失問(wèn)題,對(duì)SpringCloud Alibaba Feign請(qǐng)求頭問(wèn)題感興趣的朋友跟隨小編一起看看吧

導(dǎo)讀:在上一篇文章中我們講解了如何利用Spring Security OAuth2實(shí)現(xiàn)微服務(wù)統(tǒng)一認(rèn)證,今天繼續(xù)講解Feign遠(yuǎn)程調(diào)用和異步調(diào)用請(qǐng)求頭丟失問(wèn)題。

簡(jiǎn)介

之前演示只是測(cè)試了調(diào)用用戶微服務(wù)下查詢接口,并沒(méi)有在用戶服務(wù)再調(diào)用其他微服務(wù)接口,調(diào)測(cè)看著一切都很正常,但今天測(cè)試了用戶微服務(wù)調(diào)用商品微服務(wù),出現(xiàn)了異常:

Debug跟蹤,發(fā)現(xiàn)用戶微服務(wù)正常接收到token,而遠(yuǎn)程調(diào)用商品微服務(wù)token消失不見(jiàn)了:

原因

源碼:ReflectiveFeign.class

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (!"equals".equals(method.getName())) {
            if ("hashCode".equals(method.getName())) {
                return this.hashCode();
            } else {
                return "toString".equals(method.getName()) ? this.toString() : ((InvocationHandlerFactory.MethodHandler)this.dispatch.get(method)).invoke(args);
            }
        } else {
            try {
                Object otherHandler = args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;
                return this.equals(otherHandler);
            } catch (IllegalArgumentException var5) {
                return false;
            }
        }
    }

進(jìn)入((InvocationHandlerFactory.MethodHandler)this.dispatch.get(method)).invoke(args)

  public Object invoke(Object[] argv) throws Throwable {
      //就是在這 構(gòu)建了一個(gè)新的RequestTemplate ,而瀏覽器帶給我們的請(qǐng)求頭都會(huì)丟失
      RequestTemplate template = this.buildTemplateFromArgs.create(argv);
      Request.Options options = this.findOptions(argv);
      Retryer retryer = this.retryer.clone();
      while(true) {
          try {
              // 在這即將執(zhí)行該方法
              return this.executeAndDecode(template, options);
          } catch (RetryableException var9) {
              RetryableException e = var9;
              try {
                  retryer.continueOrPropagate(e);
              } catch (RetryableException var8) {
                  Throwable cause = var8.getCause();
                  if (this.propagationPolicy == ExceptionPropagationPolicy.UNWRAP && cause != null) {
                      throw cause;
                  }
                  throw var8;
              }
              if (this.logLevel != Level.NONE) {
                  this.logger.logRetry(this.metadata.configKey(), this.logLevel);
              }
          }
      }
  }

進(jìn)入this.executeAndDecode(template, options)

    Object executeAndDecode(RequestTemplate template, Request.Options options) throws Throwable {
        //這里 它會(huì)對(duì)我們的請(qǐng)求進(jìn)行一些包裝 
        Request request = this.targetRequest(template);
        if (this.logLevel != Level.NONE) {
            this.logger.logRequest(this.metadata.configKey(), this.logLevel, request);
        }
        long start = System.nanoTime();
        Response response;
        try {
            response = this.client.execute(request, options);
            response = response.toBuilder().request(request).requestTemplate(template).build();
        } catch (IOException var12) {
            if (this.logLevel != Level.NONE) {
                this.logger.logIOException(this.metadata.configKey(), this.logLevel, var12, this.elapsedTime(start));
            }
            throw FeignException.errorExecuting(request, var12);
        }
            .......
}

進(jìn)入this.targetRequest(template)

  Request targetRequest(RequestTemplate template) {
      //拿到對(duì)應(yīng)的所有請(qǐng)求攔截器的迭代器
      Iterator var2 = this.requestInterceptors.iterator();
      //遍歷所有的請(qǐng)求攔截器
      while(var2.hasNext()) {
          RequestInterceptor interceptor = (RequestInterceptor)var2.next();
          //這里是每個(gè)請(qǐng)求攔截器 依次對(duì)該方法進(jìn)行包裝
          interceptor.apply(template);
      }
      return this.target.apply(template);
  }

進(jìn)入interceptor.apply(template)

public interface RequestInterceptor {
    void apply(RequestTemplate var1);
}

發(fā)現(xiàn)它是一個(gè)接口,所以可以重寫(xiě)一下這個(gè)方法對(duì)我們的請(qǐng)求做一些包裝,借鑒一下別的實(shí)現(xiàn)方法:

方案

/**
 * 微服務(wù)之間feign調(diào)用請(qǐng)求頭丟失的問(wèn)題
 * @author yian
 * @since 2023-04-05
 */
@Configuration
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        HttpServletRequest httpServletRequest =   getHttpServletRequest();
        if(httpServletRequest!=null){
            Map<String, String> headers = getHeaders(httpServletRequest);
            // 傳遞所有請(qǐng)求頭,防止部分丟失
            //此處也可以只傳遞認(rèn)證的header
            //requestTemplate.header("json-token", request.getHeader("json-token"));
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                template.header(entry.getKey(), entry.getValue());
            }
            log.debug("FeignRequestInterceptor:{}", template.toString());
        }
    }
    private HttpServletRequest getHttpServletRequest() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 獲取原請(qǐng)求頭
     */
    private Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> map = new LinkedHashMap<>();
        Enumeration<String> enumeration = request.getHeaderNames();
        if(enumeration!=null){
            while (enumeration.hasMoreElements()) {
                String key = enumeration.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
        }
        return map;
    }
}

補(bǔ)充說(shuō)明

實(shí)際開(kāi)發(fā)中,在業(yè)務(wù)復(fù)雜情況下難免使用異步編排的方式實(shí)現(xiàn),這個(gè)時(shí)候你會(huì)發(fā)現(xiàn)請(qǐng)求頭又丟失了。

源碼:RequestContextHolder.class

    @Nullable
    public static RequestAttributes getRequestAttributes() {
        RequestAttributes attributes = (RequestAttributes)requestAttributesHolder.get();
        if (attributes == null) {
            attributes = (RequestAttributes)inheritableRequestAttributesHolder.get();
        }
        return attributes;
    }

查看requestAttributesHolder變量:

private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal("Request attributes");

查看NamedThreadLocal

public class NamedThreadLocal<T> extends ThreadLocal<T> {
    private final String name;
    public NamedThreadLocal(String name) {
        Assert.hasText(name, "Name must not be empty");
        this.name = name;
    }
    public String toString() {
        return this.name;
    }
}

ThreadLocal是一個(gè)線程局部變量,在不同線程之間是獨(dú)立的所以我們獲取不到原先主線程的請(qǐng)求屬性。

方案

//獲取之前的請(qǐng)求
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
CompletableFuture<Void> getAddress = CompletableFuture.runAsync(() -> {
    System.out.println(Thread.currentThread().getId());
    //每一個(gè)線程都來(lái)共享之前請(qǐng)求的數(shù)據(jù)
    RequestContextHolder.setRequestAttributes(requestAttributes);
    List<MemberAddressVo> address = memberFeignService.getAddress(memberRespVo.getId());
    confirmVo.setAddress(address);
}, executor);

至此我們已經(jīng)解決了Feign遠(yuǎn)程以及異步編排下導(dǎo)致的請(qǐng)求頭丟失問(wèn)題。

到此這篇關(guān)于SpringCloud Alibaba微服務(wù)實(shí)戰(zhàn)之遠(yuǎn)程Feign請(qǐng)求頭丟失的文章就介紹到這了,更多相關(guān)SpringCloud Alibaba Feign請(qǐng)求頭內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

武清区| 鹤庆县| 井冈山市| 武隆县| 九台市| 富平县| 娄底市| 长岭县| 蕲春县| 德安县| 石阡县| 始兴县| 宾阳县| 读书| 桑植县| 达拉特旗| 华坪县| 株洲市| 育儿| 阿城市| 长武县| 科技| 张掖市| 勐海县| 花垣县| 平江县| 治县。| 桐柏县| 内乡县| 镇远县| 五莲县| 团风县| 凭祥市| 保定市| 阳原县| 弋阳县| 施甸县| 盐山县| 巩义市| 嘉善县| 密云县|