微服務(wù)如何通過feign.RequestInterceptor傳遞參數(shù)
微服務(wù)通過feign.RequestInterceptor傳遞參數(shù)
Feign 支持請求攔截器,在發(fā)送請求前,可以對發(fā)送的模板進(jìn)行操作,例如設(shè)置請求頭等屬性,自定請求攔截器需要實(shí)現(xiàn) feign.RequestInterceptor 接口,該接口的方法 apply 有參數(shù) template ,該參數(shù)類型為 RequestTemplate,我們可以根據(jù)實(shí)際情況對請求信息進(jìn)行調(diào)整,示例如下:
創(chuàng)建自定義請求攔截器
在發(fā)送請求前增加了一個(gè)請求頭信息,進(jìn)行身份校驗(yàn)。
import feign.RequestInterceptor;?
import feign.RequestTemplate; ? ? ?
public class MyRequestInterceptor implements RequestInterceptor{ ? ? ?
public void apply(RequestTemplatetemplate){?
template.header("Authorization","123");?
}?
}服務(wù)端可以通過HttpServletRequest獲取到前面?zhèn)鬟f的參數(shù)
具體獲取邏輯如下
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
if (requestAttributes != null) {
? ? ?HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
? ? ?request.getHeader("Authorization");
?}feign的攔截器RequestInterceptor
SpringCloud的微服務(wù)使用Feign進(jìn)行服務(wù)間調(diào)用的時(shí)候可以使用RequestInterceptor統(tǒng)一攔截請求來完成設(shè)置header等相關(guān)請求,但RequestInterceptor和ClientHttpRequestInterceptor有點(diǎn)不同,它拿不到原本的請求,所以要通過其他方法來獲取原本的請求
首先創(chuàng)建自定義的RequestInterceptor
這里通過RequestContextHolder獲取到當(dāng)前的request
@Slf4j
@Component
public class MyFeignInterceptor implements RequestInterceptor {
? ? @Override
? ? public void apply(RequestTemplate template) {
? ? ? ? ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
? ? ? ? log.info("===request: {}", template.url());
? ? ? ? template.header("aaaa", "bbbb");
? ? }
}然后定義一個(gè)feign的接口類
@FeignClient(name = "SPRING-CLOUD-CLIENT-DEMO")
public interface ClientFeign {
? ? @GetMapping("/demo/get")
? ? String weight(@RequestParam String param);
}再創(chuàng)建一個(gè)controller
@Slf4j
@RestController
@RequestMapping("/interceptor")
public class InterceptorController {
? ? @Autowired
? ? private ClientFeign clientFeign;
? ? @GetMapping("/feign")
? ? public String feign(@RequestParam String param) {
? ? ? ? return clientFeign.weight(param);
? ? }
}運(yùn)行程序測試一下接口

查看程序打印
可以看到進(jìn)入了我們自定義的RequestInterceptor
INFO 25936 --- [ctor-http-nio-3] c.m.d.g.interceptor.MyFeignInterceptor : ===request: /demo/weight?param=a
再查看feign調(diào)用的程序日志,可以看到設(shè)置的header信息
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : aaaa: a
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : websession: ECF4D97D02EEAFDDA3C15A7F1F050F61
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: aaaa, bbbb
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: accept, */*
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: user-agent, Java/1.8.0_191
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: host, 39.108.15.147:8801
INFO 1 --- [nio-8801-exec-6] c.m.d.client.controller.DemoController : header: connection, keep-alive
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 中的 NoSuchMethodException 異常及解決思路(最新推薦)
NoSuchMethodException異常是Java中使用反射機(jī)制時(shí)常見的錯(cuò)誤,它通常由方法名或參數(shù)不匹配、訪問權(quán)限問題、方法簽名不匹配等原因引發(fā),解決方法包括核實(shí)方法名及其參數(shù)類型、確認(rèn)方法訪問權(quán)限、檢查方法簽名和重載問題、確保方法存在于正確的類中,感興趣的朋友一起看看吧2025-01-01
Java File類 mkdir 不能創(chuàng)建多層目錄的解決
這篇文章主要介紹了Java File類 mkdir 不能創(chuàng)建多層目錄的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java實(shí)現(xiàn)水仙花數(shù)的計(jì)算
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)水仙花數(shù)的計(jì)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
SpringCloud配置動(dòng)態(tài)更新原理解析
在微服務(wù)架構(gòu)的浩瀚星海中,服務(wù)配置的動(dòng)態(tài)更新如同魔法一般,能夠讓應(yīng)用在不重啟的情況下,實(shí)時(shí)響應(yīng)配置的變更,Spring Cloud作為微服務(wù)架構(gòu)中的佼佼者,其動(dòng)態(tài)配置更新的能力尤為引人注目,本文給大家介紹了SpringCloud配置動(dòng)態(tài)更新原理,需要的朋友可以參考下2025-01-01
@PathVariable注解,讓spring支持參數(shù)帶值功能的案例
這篇文章主要介紹了@PathVariable注解,讓spring支持參數(shù)帶值功能的案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Spring MVC實(shí)現(xiàn)一次簡單的CRUD示例
這篇文章主要介紹了Spring MVC實(shí)現(xiàn)一次簡單的CRUD示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
SpringBoot如何打印mybatis的執(zhí)行sql問題
這篇文章主要介紹了SpringBoot如何打印mybatis的執(zhí)行sql問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

