Spring Cloud:Feign消費服務

什麼是Feign?

Feign是一個聲明性的Web服務客戶端,它使Web服務客戶端更容易編寫。

回顧一下,之前consumer應用消費bootiful應用的服務時使用的是RestTemplate。

Feign如何消費服務?

創建consumerfeign應用,引入feign依賴

創建Feigin客戶端BootifulService接口。注意加上@FeignClient("bootiful")註解指明是bootiful應用的Feign客戶端,並指明消費具體的接口

創建consumerFeigin主類和對外提供的接口(consumerfeign/getBootiful/{appName}接口消費bootiful應用的/service-instances/{appName}接口)

配置consumerfeign應用名稱為consumerfeign、端口號為9003

啟動consumerfeign應用,打開http://localhost:8761/註冊中心,consumerfeign應用已經註冊上去

成功訪問consumerfeign接口http://localhost:9003/consumerfeign/getBootiful/bootiful

反覆刷新,看到消費了不同bootiful實例,因為Feign默認啟用了Ribbon實現了負載均衡

Feign就是這樣消費服務的啦,你學會了嗎?