微服務實戰——SpringCloud與Feign集成


上一篇集成了ZuulGateway和Eureka並進行了測試。在實際場景中,我們肯定會有很多的微服務,而他們之間可能會存在相互調用的關系,那么,如何優雅的處理服務之間的調用問題呢?接下來就是我們要解決的。

簡單的說下Feign

Feign 是一個聲明式REST Web服務客戶端,可以處理微服務間的Web服務調用。他是使用注解加接口的形式形成去調用服務的,相對來說不是很難,有興趣可去官方地址了解下。這里不多介紹。

如何用

這里我們還是基於之前的Spring cloud demo去改造,老規矩先附上源碼地址spring cloud demo

步驟
  1. 這里Consumer與Provider分別代表兩個微服務,測試時,使用Controller通過Feign調用Provider。調用流程如下: 網關zuul -> consumer -> provider
  2. 引入依賴
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
  3. 在Consumer的啟動類上增加注解,開啟Feign的支持
    @EnableFeignClients
    
  4. 在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();
         }
    
     }
    
    
  5. 在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();
    
     }
    
    
  6. 在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";
         }
    
     }
    
    
  7. 使用Postman請求Consumer測試
    觀察得知,Postman請求到網關之后分發到consumer微服務,微服務通過Feign接口調用Provider微服務並接收到返回值,之后原路返回到Consumer。當然,這里只是簡單的演示下如何使用Feign,實際生產環境中,使用遠不止這么簡單,這就需要我們慢慢去摸索了...
    20200303165439
    20200303165525
    20200303165547

最后

到這里,我們的基本框架已經搭建完成,我們用SpringCloud集成了網關(Zuul),還加入了服務發現與注冊(Eureka),也演示了微服務間的調用並集成了Feign。

那么基於以上,我們會發現還是會有些場景沒有解決。比如,我的配置都在properties里面,參數都是寫死的,到線上后怎樣在不重啟服務的情況下修改參數?怎樣進行灰度發布或金絲雀測試?還有我們的微服務已經通過Feign可以相互調用了,那我怎樣監測他們的運行情況?如果出故障時,如何快速的知道並修復?數據量太大,一台扛不住又該如何?在SpringCloud中又如何處理分庫分表讀寫分離?

別急,后面我們都會講到...

持續學習,記錄點滴。更多文章請訪問 文章首發


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM