一、場景描述
我們可以通過ribbon來實(shí)現(xiàn)服務(wù)間的調(diào)用,但是當(dāng)服務(wù)很多的時(shí)候調(diào)用方法寫起來比較麻煩赂摆,而且也不容易管理。feign整合了ribbon钟些,以接口的方式來調(diào)用服務(wù)烟号,更加的方便
二、feign實(shí)現(xiàn)
- Idea-->file-->new-->project-->spring initializr-->next-->修改必要信息-->next
- 分別選擇Cloud Routing-->Feign還有Cloud Discovery-->Eureka Discovery
- 點(diǎn)擊next-->finish
- 我們查看生成項(xiàng)目里面的pom文件內(nèi)容如下:
<?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>
<groupId>com.example</groupId>
<artifactId>eurekafeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eurekafeign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 我們可以看到其中兩個(gè)重要的依賴政恍,我們再現(xiàn)有項(xiàng)目添加feign調(diào)用功能的時(shí)候只需要把下面兩個(gè)依賴添加即可
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 在我們的啟動(dòng)類中添加@EnableFeignClients汪拥。用于啟用feign組建
- 在application.properties文件中添加eureka的配置,使注冊中心可用篙耗。配置請參照注冊中心實(shí)戰(zhàn)篇迫筑,內(nèi)容如下:
spring.application.name=eurekafeign
server.port=8084
eureka.client.serviceUrl.defaultZone=http://eureka1:8001/eureka/ ,http://eureka1:8002/eureka/,http://eureka1:8003/eureka/
- 我們做一個(gè)簡單的調(diào)用實(shí)例代碼如下:
==DemoServiceInterface== 這是核心內(nèi)容,是Feign的Client接口宗弯,
@FeignClient("DEMO")指定我們這個(gè)接口會(huì)使用eureka注冊中心的DEMO服務(wù)中的服務(wù)
@RequestMapping("/hello")表示我們調(diào)用DEMO服務(wù)中的hello服務(wù)
package com.example.eurekafeign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@FeignClient("DEMO")
public interface DemoServiceInterface {
@RequestMapping("/hello")
String hello();
}
- 在我們需要調(diào)用demo的hello服務(wù)的地方注入DemoServiceInterface脯燃,直接調(diào)用hello方法即可完成調(diào)用。
package com.example.eurekafeign;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class HelloWord {
@Resource
private DemoServiceInterface demoServiceInterface;
@RequestMapping(value="hello")
@ResponseBody
public String helloWord(){
return demoServiceInterface.hello();
}
}
++敲黑板++
feign內(nèi)部集成了Ribbon所以通過serviceid找到服務(wù)后罕伯,會(huì)通過ribbon自動(dòng)實(shí)現(xiàn)服務(wù)訪問的負(fù)載均衡曲伊,可以通過定義ribbon的負(fù)載均衡方法來實(shí)現(xiàn)自己想要的
在接口的方法前面需要定義該方法訪問路徑和訪問方法和編碼格式等信息
對于請求參數(shù)也可以通過@requestBody或者@RequestParameter的value方法進(jìn)行設(shè)置,這里就使用和入?yún)⒚膮?shù)設(shè)定