天天看點

openfeign調用服務是否需要網關_微服務實戰——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>
           
  1. 在Consumer的啟動類上增加注解,開啟Feign的支援
@EnableFeignClients
           
  1. 在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();
        }

    }
           
  1. 在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();

    }
           
  1. 在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";
        }

    }
           
  1. 使用Postman請求Consumer測試

    觀察得知,Postman請求到網關之後分發到consumer微服務,微服務通過Feign接口調用Provider微服務并接收到傳回值,之後原路傳回到Consumer。當然,這裡隻是簡單的示範下如何使用Feign,實際生産環境中,使用遠不止這麼簡單,這就需要我們慢慢去摸索了...

    openfeign調用服務是否需要網關_微服務實戰——SpringCloud與Feign內建
    openfeign調用服務是否需要網關_微服務實戰——SpringCloud與Feign內建
    openfeign調用服務是否需要網關_微服務實戰——SpringCloud與Feign內建

最後

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

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

别急,後面我們都會講到...

持續學習,記錄點滴。更多請點選檢視原文