如何通过SpringCloud Feign实现请求头转发以避免Session失效?

2026-05-15 23:201阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计529个文字,预计阅读时间需要3分钟。

如何通过SpringCloud Feign实现请求头转发以避免Session失效?

在微服务开发中,经常会有这样的需求,公司自定义了通用的请求头,需要在微服务的调用链中转发这些头部信息,例如在请求头中添加token或自定义的uniqueId等信息,总之就是自定义的一个键值对。

微服务开发中经常有这样的需求,公司自定义了通用的请求头,需要在微服务的调用链中转发,比如在请求头中加入了token,或者某个自定义的信息uniqueId,总之就是自定义的一个键值对的东东,A服务调用B服务,B服务调用C服务,这样通用的东西如何让他在一个调用链中不断地传递下去呢?以A服务为例:

方案1

最傻的办法,在程序中获取,调用B的时候再转发,怎么获取在Controller中国通过注解获取,或者通过request对象获取,这个不难,在请求B服务的时候,通过注解将值放进去即可;简代码如下:

获取: @RequestMapping(value = "/api/test", method = RequestMethod.GET) public String testFun(@RequestParam String name, @RequestHeader("uniqueId") String uniqueId) { if(uniqueId == null ){ return "Must defined the uniqueId , it can not be null"; } log.info(uniqueId, "begin testFun... "); return uniqueId; }

然后A使用Feign调用B服务的时候,传过去:

@FeignClient(value = "DEMO-SERVICE") public interface CallClient { /** * 访问DEMO-SERVICE服务的/api/test接口,通过注解将logId传递给下游服务 */ @RequestMapping(value = "/api/test", method = RequestMethod.GET) String callApiTest(@RequestParam(value = "name") String name, @RequestHeader(value = "uniqueId") String uniqueId); }

方案弊端:毫无疑问,这方案不好,因为对代码有侵入,需要开发人员没次手动的获取和添加,因此舍弃

方案2

服务通过请求拦截器,在请求从A发送到B之后,在拦截器内将自己需要的东东加到请求头:

import com.intellif.log.LoggerUtilI; import feign.RequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.blog.csdn.net/Crystalqy/article/details/79083857

如何通过SpringCloud Feign实现请求头转发以避免Session失效?

到此这篇关于SpringCloud Feign转发请求头(防止session失效)的解决方案的文章就介绍到这了,更多相关SpringCloud Feign转发请求头内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计529个文字,预计阅读时间需要3分钟。

如何通过SpringCloud Feign实现请求头转发以避免Session失效?

在微服务开发中,经常会有这样的需求,公司自定义了通用的请求头,需要在微服务的调用链中转发这些头部信息,例如在请求头中添加token或自定义的uniqueId等信息,总之就是自定义的一个键值对。

微服务开发中经常有这样的需求,公司自定义了通用的请求头,需要在微服务的调用链中转发,比如在请求头中加入了token,或者某个自定义的信息uniqueId,总之就是自定义的一个键值对的东东,A服务调用B服务,B服务调用C服务,这样通用的东西如何让他在一个调用链中不断地传递下去呢?以A服务为例:

方案1

最傻的办法,在程序中获取,调用B的时候再转发,怎么获取在Controller中国通过注解获取,或者通过request对象获取,这个不难,在请求B服务的时候,通过注解将值放进去即可;简代码如下:

获取: @RequestMapping(value = "/api/test", method = RequestMethod.GET) public String testFun(@RequestParam String name, @RequestHeader("uniqueId") String uniqueId) { if(uniqueId == null ){ return "Must defined the uniqueId , it can not be null"; } log.info(uniqueId, "begin testFun... "); return uniqueId; }

然后A使用Feign调用B服务的时候,传过去:

@FeignClient(value = "DEMO-SERVICE") public interface CallClient { /** * 访问DEMO-SERVICE服务的/api/test接口,通过注解将logId传递给下游服务 */ @RequestMapping(value = "/api/test", method = RequestMethod.GET) String callApiTest(@RequestParam(value = "name") String name, @RequestHeader(value = "uniqueId") String uniqueId); }

方案弊端:毫无疑问,这方案不好,因为对代码有侵入,需要开发人员没次手动的获取和添加,因此舍弃

方案2

服务通过请求拦截器,在请求从A发送到B之后,在拦截器内将自己需要的东东加到请求头:

import com.intellif.log.LoggerUtilI; import feign.RequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.blog.csdn.net/Crystalqy/article/details/79083857

如何通过SpringCloud Feign实现请求头转发以避免Session失效?

到此这篇关于SpringCloud Feign转发请求头(防止session失效)的解决方案的文章就介绍到这了,更多相关SpringCloud Feign转发请求头内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!