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


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

一、Feign 簡介:

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

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

  • 1. feign本身里面就包含有了ribbon,具有負載均衡的能力
  • 2. fegin是一個采用基於接口的注解的編程方式,更加簡便
  • 3. fegin整合了Hystrix,具有熔斷的能力

二、 准備工作:

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文件如下:

<parent>標簽就是引入我們第一章節新建的父工程的pom.xml文件,具體可參考:一起來學Spring Cloud | 第一章 :如何搭建一個多模塊的springcloud項目

<?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>

 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;

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.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.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.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是整合了負載均衡功能

四、總結:

加上本章節代碼后,代碼目錄結構:


免責聲明!

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



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