Feign獨立使用-遠端呼叫
阿新 • • 發佈:2020-11-25
1.引入依賴
Maven座標
<!--feign 依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${feign.version}</version> </dependency>
2. 定義FeignClient
/** * @description: 基礎服務 * * FeignClient註解: * name/value屬性: 這兩個的作用是一樣的,指定的是呼叫服務的微服務名稱(獨立使用時,只需唯一) 可以使用環境變數或者jvm屬性定義url欄位 * url: 定義遠端服務的domain * fallbackFactory: 定義熔斷器(可以在熔斷器中定義熔斷處理,返回兜底的結果) * * @date: 2020/11/24 14:49 * @author: fdh */ //@FeignClient(name = "baseSapi", url = "${qs.sapi.base.url.prefix}", fallbackFactory = RemoteBaseFallBackFactory.class) @FeignClient(name = "baseSapi", url = "http://localhost:9001", fallbackFactory = RemoteBaseFallBackFactory.class) public interface RemoteBaseService { /** * 獲取考季知識點 * * 介面地址: http:// xxxxx * * @param seasonId * @return */ @RequestMapping(value = "/s1/v1/seasonKnowledgePoint/kpListBySeasonId", method = RequestMethod.GET) SapiResult<List<KnowledgePointVo>> getKpListBySeasonId(@RequestParam("seasonId") Long seasonId); }
3.定義熔斷器
/** * @description: 遠端基礎熔斷工廠 * @date: 2020/11/25 13:48 * @author: fdh */ @Component public class RemoteBaseFallBackFactory implements FallbackFactory<RemoteBaseService> { @Override public RemoteBaseService create(Throwable throwable) { RemoteBaseFallBackServiceImpl remoteBaseFallBackService = new RemoteBaseFallBackServiceImpl(); remoteBaseFallBackService.setThrowable(throwable); return remoteBaseFallBackService; } }
4.定義FeignClient熔斷實現
/** * @description: RemoteBaseService熔斷處理 * @date: 2020/11/25 13:49 * @author: fdh */ public class RemoteBaseFallBackServiceImpl implements RemoteBaseService { private static final Logger log = LoggerFactory.getLogger(RemoteBaseFallBackServiceImpl.class); @Setter private Throwable throwable; private void printLog(String modules, Throwable throwable){ if( modules == null ){ modules = StringUtils.EMPTY; } log.info("{}異常:", modules, throwable); } @Override public SapiResult<List<KnowledgePointVo>> getKpListBySeasonId(Long seasonId) { printLog("知識點介面", throwable); return SapiResultUtil.success(Collections.emptyList()); } }
5. 掃描FeignClient
/** * @description: 啟動類 * @date: 2020/11/25 13:52 * @author: fdh */ @SpringBootApplication @EnableFeignClients(basePackages = "com.xxx.feign") public class DaApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DaApplication.class); } public static void main(String[] args) { SpringApplication.run(DaApplication.class, args); } }
以上就可以實現Feign獨立使用,不依賴註冊中心,進行獨立遠端呼叫
補充:
1. 生產環境上往往需要記錄FeignClient遠端呼叫日誌,我們可以定義日誌輸出級別
/** * @description: Feign日誌 * @date: 2020/11/5 16:35 * @author: fdh */ @Configuration public class FeignConfig { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
推薦日誌級別:
級別 | 列印內容 |
NONE(預設值) | 不記錄任何日誌 |
BASIC(更適合生產環境) | 僅記錄請求方法、URL、響應狀態程式碼以及執行時間 |
HEADERS | 記錄BASIC級別的基礎上,記錄請求和響應的 header |
FULL(更適合開發環境) | 記錄請求和響應的 header、body和元資料 |
2. 啟用熔斷並設定熔斷超時時間(預設1秒)
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 30000
統一處理,需要增加FeignClient遠端呼叫攜帶加密引數
@Component public class FeignRequestInterceptor implements RequestInterceptor { private org.slf4j.Logger log = LoggerFactory.getLogger(FeignRequestInterceptor.class); @Override public void apply(RequestTemplate requestTemplate) { try { // xxxx 演算法 //String sign = DigestUtils.md5DigestAsHex(params.getBytes("utf-8")); requestTemplate.query("sign", "xxxxxx"); } catch (UnsupportedEncodingException e) { log.error("簽名失敗:", e); } } }