1. 程式人生 > >一起來學Spring Cloud | 第四章:服務消費者 ( Feign )

一起來學Spring Cloud | 第四章:服務消費者 ( Feign )

上一章節,講解了SpringCloud如何通過RestTemplate+Ribbon去負載均衡消費服務,本章主要講述如何通過Feign去消費服務。

一、Feign 簡介:

Feign是一個便利的rest框架,在Ribbon的基礎上進行了一次改進,採用介面的方式,將需要呼叫的其他服務的方法定義成抽象方法,不需要自己構建http請求,簡化了呼叫。但是最後的原理還是通過ribbon在註冊伺服器中找到服務例項,然後對請求進行分配。

在工作中,我們基本上都是使用Feign來進行服務呼叫,因為Feign使用起來就像是呼叫自身本地的方法一樣,而感覺不到是呼叫遠端方法,相當舒服,它主要有3個優點。

  • 1. feign本身裡面就包含有了ribbon,具有負載均衡的能力
  • 2. fegin是一個採用基於介面的註解的程式設計方式,更加簡便
  • 3. fegin整合了Hystrix,具有熔斷的能力

二、 準備工作:

對於springcloud-eureka-client工程不清楚了,可以參考:一起來學Spring Cloud | 第二章:服務註冊和發現元件 (Eureka)

1. 啟動eureka-server 工程,eureka註冊中心就啟動了。

2. 啟動springcloud-eureka-client工程,springcloud-eureka-client工程的埠為9300。

3. 將springcloud-eureka-client工程的application.properties檔案中埠改成9400,然後再啟動springcloud-eureka-client。

通過上面步驟,我們就啟動了埠9300,9400兩個一樣的springcloud-eureka-client服務模組(為了測試feign的負載均衡能力)。

三、新建一個feign服務:

1. 新建一個spring-boot工程,取名為springcloud-feign-client,修改pox檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.haly</groupId>
		<artifactId>springcloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<groupId>com.haly</groupId>
	<artifactId>springcloud-feign-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud-ribbon-client</name>
	<description>新建一個springcloud專案</description>

	<dependencies>
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
           <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

如果有同學對上面配置pom檔案中的parent標籤引入的父pom有疑問,可以參考一下 一起來學Spring Cloud | 第二章:服務註冊和發現元件 (Eureka) , 講解了自己定義一個父pom的作用 

如果有同學不想從頭搭建模組,也可以在parant標籤中直接引入springboot的配置,但是注意本章節所使用的依賴包相容的版本號為:

springboot :2.1.4.RELEASE ,      springcloud:Finchley.RELEASE

 2. 修改application.properties檔案如下

server.port=9600
spring.application.name=springcloud-feign-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3. 模組啟動類需要增加註解@EnableFeignClients,表示開啟Feign的功能

package com.haly.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class SpringcloudFeignClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudFeignClientApplication.class, args);
	}

}

4. 定義一個feign介面,通過@FeignClient(“服務名”),來指定呼叫哪個服務。本章案例中呼叫了springcloud-eureka-client服務的“/hello”介面

springcloud-eureka-client模組中的hello介面,在第二章已經實現,具體實現可以參考:一起來學Spring Cloud | 第二章:服務註冊和發現元件 (Eureka)

ps:抽象方法的註解、方法名、引數要和服務提供方保持一致(這裡是與springcloud-eureka-client模組中的 /hello方法保持一致)

package com.haly.springcloud.romote;

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

@FeignClient(value = "springcloud-eureka-client")
public interface FeignRemoteService {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(@RequestParam(value = "name") String name);
    
}

5. controller層,對外暴露一個"/getHello"的API介面,給頁面測試,通過上面定義的Feign客戶端FeignRemoteService來消費服務。程式碼如下:

package com.haly.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haly.springcloud.romote.FeignRemoteService;

@RestController
public class FeignController {
	
    @Autowired
    FeignRemoteService feignRemoteService;

    @GetMapping(value = "/getHello")
    public String getHello(@RequestParam String name) {
        return feignRemoteService.hello(name);
    }

}

6. 啟動各個服務模組,服務註冊結果如下

 訪問地址 http://localhost:9600/getHello?name=young碼農  , 多次輪流訪問頁面,出現9300,9400服務介面返回結果,證明feign是整合了負載均衡功能

四、總結:

加上本章節程式碼後,程式碼目錄結構:

&n