5、微服務調用方式之feign 實戰 簡介:改造電商項目 訂單服務 調用商品服務獲取商品信息 Feign: 偽RPC客戶端(本質還是用http) 官方文檔: https://cloud.spring.io/spring-cloud-openfeign/ 1、使用feign步驟講解(新舊版本依賴名稱不一樣) 加入依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> 啟動類增加@EnableFeignClients 增加一個接口 並@FeignClient(name="product-service") 如:
/**
* 商品服務客戶端
*/
@FeignClient(name = "product-service")
public interface ProductClient {
@GetMapping("/api/v1/product/find")
String findById(@RequestParam(value = "id") int id);
}
2、編碼實戰 3、注意點: 1、路徑 2、Http方法必須對應 3、使用requestBody,應該使用@PostMapping 4、多個參數的時候,通過@RequestParam("id") int id)方式調用 6、Feign核心源碼解讀和服務調用方式ribbon和Feign選擇 簡介: 講解Feign核心源碼解讀和 服務間的調用方式ribbon、feign選擇 1、ribbon和feign兩個的區別和選擇 選擇feign 默認集成了ribbon 寫起來更加思路清晰和方便 采用注解方式進行配置,配置熔斷等方式方便
負載均衡用的是ribbon 2、超時配置 默認optons readtimeout是60,但是由於hystrix默認是1秒超時 #修改調用超時時間 feign: client: config: default: connectTimeout: 2000 readTimeout: 2000 模擬接口響應慢,線程睡眠新的方式 try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
默認路由方式是輪詢
推薦feign