Feign如何解決服務之間調用傳遞token
解決服務之間調用傳遞token
現(xiàn)在的微服務基本就是SpringSecurity+Oauth2做的授權和認證,假如多個服務直接要通過Fegin來調用,會報錯401
- a、有做權限處理的服務接口直接調用會造成調用時出現(xiàn)http 401未授權的錯誤,繼而導致最終服務的http 500內(nèi)部服務器錯誤
- b、解決方式:最方便的就是往請求頭里加上token,一起帶過去;
Feign有提供一個接口RequestInterceptor
只要實現(xiàn)這個接口,簡單做一些處理,比如說我們驗證請求頭的token叫Access-Token,我們就先取出當前請求的token,然后放到feign請求頭上;
public class FeignConfig implements RequestInterceptor {
? ? ? ? @Override
? ? ? ? public void apply(RequestTemplate requestTemplate) {
? ? ? ? ? ? ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
? ? ? ? ? ? HttpServletRequest request = attributes.getRequest();
? ? ? ? ? ? //添加token
? ? ? ? ? ? requestTemplate.header(HttpHeaders.AUTHORIZATION, request.getHeader(HttpHeaders.AUTHORIZATION));
? ? ? ? }
? ? }調用方式
? ? @FeignClient(name = "qtjuaa", configuration = FeignConfig.class)
? ? public interface UaaClient {
? ? ? ? @RequestMapping(value = "/api/test", method= RequestMethod.GET)
? ? ? ? String test();
? ? }Feign調用服務各種坑處理
編寫被調用服務
@RefreshScope
@RestController
public class XXXController extends BaseController implements IndicatorsFeignApi{
@Resource
private XXXService xxx;
@Override
public Wrapper<CommonVo> getXXXX(@RequestBody CommonDto commonDto) {
try {
CommonVo vo = xxx.getdata(commonDto);
return WrapMapper.ok(vo);
}catch(Exception e) {
e.printStackTrace();
return WrapMapper.error("系統(tǒng)異常,請聯(lián)系管理員!");
}
}
}
//Service不進行展示,注意參數(shù)傳遞至service層時要加入注解@RequestBody等才能獲取參數(shù)
在配置文件添加feign相關配置

編寫調用api

pom文件中添加相關依賴
org.springframework.cloud spring-cloud-starter-hystrix org.springframework.cloud spring-cloud-starter-netflix-hystrix-dashboard
調用Api
@FeignClient(value = "被調用服務名")
public interface IndicatorsFeignApi {
@PostMapping(value = "/api/getXXXX",consumes="application/json", headers = {"Accept=application/json", "Content-Type=application/json"})
Wrapper<CommonVo> getXXXX(@RequestBody CommonDto commonDto);
}
Feign調用錯誤處理,發(fā)生相關錯誤是會跳轉至fallback處理
@Component
public class IndicatorsFeignApiHystrix implements IndicatorsFeignApi {
@Override
public Wrapper<CommonVo> getXXXX(CommonDto commonDto) {
System.out.println("=====調用服務獲數(shù)據(jù)發(fā)生異常======");
return null;
}
}
當啟用fallback后,有些報錯不會打印在控制臺上,這時可以修改配置中的
feign: ? hystrix: ? ? enabled: true
將enabled改為false,錯誤發(fā)生后將不會跳轉fallback。
此處有一個坑,當時調用的時候服務是可以調用成功的,但是有一個報錯:
Could not extract response: no suitable HttpMessageConverter found for response type [XXXX] and content type [text/html;charset=UTF-8]
貌似是返回數(shù)據(jù)的編碼與接收實體類不一樣,導致報錯。加上headers = {"Accept=application/json", "Content-Type=application/json"}解決了相關問題.
編寫客戶端服務
//serviceImp層
? ?@Autowired
?? ?private IndicatorsFeignApi api;//聲明調用api
?? ?
?? ?@Override
?? ?public CommonVo getXXX(CommonDto commonDto) {
?? ??? ?Wrapper<CommonVo> result = ? api.getXXXX(commonDto);//服務調用
?? ??? ?if(result!=null) {
?? ??? ??? ?return result.getResult();
?? ??? ?}else {
?? ??? ??? ?return new CommonVo();
?? ??? ?}
?? ?}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java實現(xiàn)合并2個文件中的內(nèi)容到新文件中
這篇文章主要介紹了java實現(xiàn)合并2個文件中的內(nèi)容到新文件中,思路非常不錯,這里推薦給大家。2015-03-03

