Spring Cloud認知學習(三):聲明式調用Feign的使用



💡上一篇介紹微服務構建起來后,使用Ribbon來解決多個服務的負載均衡問題。 Spring Cloud認知學習(二):Ribbon使用

💡這一篇介紹一個用於聲明式調用服務的組件Fegin,主要用於解決前面的服務調用與restTemplate耦合緊密的問題。


Feign

🔵Feign用於聲明式調用服務
🔵在上面的服務調用中,我們始終還是沒有擺脫restTemplate,我們調用別的服務始終要使用restTemplate來發起。想想我們以前是怎么開發的(三層架構,controller調用service,service調用dao),controller調用service,feign就是為這種面向接口化編程需求而產生的。為什么說他能面向接口化編程呢?我們下面來演示。
20200512115715



使用示例

代碼參考:Feign簡單使用實驗
考慮到服務可能有多個消費者,所以我們把共有的代碼寫到spring-cloud-common-data中,這樣所有的消費者都可以通過繼承這個依賴包來獲取Feign修飾的接口。



1.導入依賴:

spring-cloud-common-data中導入依賴:

<dependencies>
        <!--增加feign依賴 start-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--舊版本的-->
            <!--<artifactId>spring-cloud-starter-feign</artifactId>-->
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--增加feign依賴 end-->
    </dependencies>


2.新建Feign Interface

spring-cloud-common-data創建一個interface:

package com.progor.study.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

// 由於這種服務的服務消費者可能比較多,放到共有依賴中。
@FeignClient(value = "MESSAGESERIVE")
public interface MessageService {
    // 這里使用RequestMapping將服務提供者的方法與本地Service方法建立映射
    @RequestMapping(value = "/msg/list", method = RequestMethod.GET)
    Map<String, String> list();
}

💡@FeignClient用來配置信息,可以配置當前interface對應哪個服務。搭配@RequestMapping的效果就是調用對應服務的對應API接口。上面代碼的效果就是從eureka中獲取名為MESSAGESERIVE的服務的服務示例,調用服務實例的/msg/list路由方法。



3.創建服務消費者

3.1 在服務消費者spring-cloud-user-consumer-80中創建一個MessageController2,注入MessageService:

package com.progor.study.Controller;

import com.progor.study.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

// 這個控制器用來處理使用fegin的情況

@RestController
public class MessageController2 {

    // 注入MessageService
    @Autowired
    private MessageService messageService; 

    @GetMapping("/msg2/list")
    public Map<String, String> list() {
        return messageService.list();
    }

}


3.2.在spring-cloud-user-consumer-80啟用Feign:
💡由於在spring-cloud-common-data中導入了fegin依賴,所以這里不需要再導入了。

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "USERSERIVE", configuration = MyConfig.class)
@EnableFeignClients // 使用feign
public class UserConsumer80Application {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumer80Application.class, args);
    }
}

4.測試

4.1:啟動spring-cloud-eureka-server-7001spring-cloud-message-service-8004spring-cloud-message-service-8005

4.2:訪問http://localhost/msg2/list,這個URL調用的是MessageController2的API,而MessageController2里面調用的是被Fegin封裝的MessageService的方法。
如果能訪問成功,那說明了是我們使用Feign成功了。
從上面可以看出,使用了Feign之后,我們可以像以前一樣調用Service來調用業務邏輯了。


補充:

  • Feign默認是有負載均衡的,看一下spring-cloud-starter-openfeign依賴包,你會發現它有導入依賴spring-cloud-starter-netflix-ribbon
  • 更多內容包括其工作原理,將會單章講解,咕咕咕。



免責聲明!

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



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