上一篇集成了ZuulGateway和Eureka併進行了測試。在實際場景中,我們肯定會有很多的微服務,而他們之間可能會存在相互調用的關係,那麼,如何優雅的處理服務之間的調用問題呢?接下來就是我們要解決的。 簡單的說下Feign Feign 是一個聲明式REST Web服務客戶端,可以處理微服務間的W ...
上一篇集成了ZuulGateway和Eureka併進行了測試。在實際場景中,我們肯定會有很多的微服務,而他們之間可能會存在相互調用的關係,那麼,如何優雅的處理服務之間的調用問題呢?接下來就是我們要解決的。
簡單的說下Feign
Feign 是一個聲明式REST Web服務客戶端,可以處理微服務間的Web服務調用。他是使用註解加介面的形式形成去調用服務的,相對來說不是很難,有興趣可去官方地址瞭解下。這裡不多介紹。
如何用
這裡我們還是基於之前的Spring cloud demo去改造,老規矩先附上源碼地址spring cloud demo
步驟
- 這裡Consumer與Provider分別代表兩個微服務,測試時,使用Controller通過Feign調用Provider。調用流程如下: 網關zuul -> consumer -> provider
- 引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
- 在Consumer的啟動類上增加註解,開啟Feign的支持
@EnableFeignClients
- 在Consumer新增Controller以供測試時調用
package cn.kxtop.blog.consumer.controller; import cn.kxtop.blog.consumer.client.ProviderClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("/feign") public class TestFeignController { @Autowired private ProviderClient providerClient; @GetMapping public String get() { log.info("consumer feign get action"); return providerClient.get(); } @PostMapping public String post() { log.info("consumer feign post action"); return providerClient.post(); } }
- 在Consumer定義Feingn介面
package cn.kxtop.blog.consumer.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @FeignClient(name = "kxtop-provider", path = "/api/test-feign") public interface ProviderClient { @GetMapping("/") String get(); @PostMapping("/") String post(); }
- 在Provider中新增REST介面,這裡主要用於測試,供Consumer調用
package cn.kxtop.blog.provider.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("/test-feign") public class TestFeignController { @GetMapping public String get() { log.info("provider feign get action"); return "test feign get"; } @PostMapping public String post() { log.info("provider feign post action"); return "test feign post"; } }
- 使用Postman請求Consumer測試
觀察得知,Postman請求到網關之後分發到consumer微服務,微服務通過Feign介面調用Provider微服務並接收到返回值,之後原路返回到Consumer。當然,這裡只是簡單的演示下如何使用Feign,實際生產環境中,使用遠不止這麼簡單,這就需要我們慢慢去摸索了...
最後
到這裡,我們的基本框架已經搭建完成,我們用SpringCloud集成了網關(Zuul),還加入了服務發現與註冊(Eureka),也演示了微服務間的調用並集成了Feign。
那麼基於以上,我們會發現還是會有些場景沒有解決。比如,我的配置都在properties裡面,參數都是寫死的,到線上後怎樣在不重啟服務的情況下修改參數?怎樣進行灰度發佈或金絲雀測試?還有我們的微服務已經通過Feign可以相互調用了,那我怎樣監測他們的運行情況?如果出故障時,如何快速的知道並修複?數據量太大,一臺扛不住又該如何?在SpringCloud中又如何處理分庫分表讀寫分離?
別急,後面我們都會講到...
持續學習,記錄點滴。更多文章請訪問 文章首發