文章目錄
1. 簡介
Feign 是一個聲明式的僞Http客戶端,使用時只需要創建一個接口並註解。
Feign 默認集成了 Ribbon,並和 Eureka 結合,默認實現了負載均衡的效果。
2. 工程實例
在這篇文章: SpringBoot2 整合 SpringCloud Ribbon 實現負載均衡實例 中我們使用RestTemplate+Ribbon去消費服務,本篇文章主要講述如何通過Feign去消費服務。
工程目錄如下圖所示:
root 工程 spring-study,root 工程的 pom.xml 如下所示:
<?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.syrdbt</groupId>
<artifactId>study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springcloud-study-eureka-server</module>
<module>springcloud-study-feign</module>
<module>springcloud-study-hello-service</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/>
</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.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
2.1 註冊中心 springcloud-study-eureka-server
工程目錄如下所示:
2.1.1 依賴 pom.xml
pom.xml 的依賴如下所示:
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eurkea-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
2.1.2 配置文件 application.properties
配置文件如下所示:
# 服務註冊中心端口號
server.port=8761
# 服務註冊中心主機名
eureka.instance.hostname=127.0.0.1
# 不向註冊中心註冊自己
eureka.client.register-with-eureka=false
# 不需要檢索服務
eureka.client.fetch-registry=false
# 註冊中心地址,會自動填充 主機名 和 端口號
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.1.3 啓動類 EurekaServerApplication.java
package com.syrdbt.study.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author syrdbt
* @date 2019-10-12
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2.2 服務提供者 springcloud-study-hello-service
工程目錄如下所示:
2.2.1 依賴 pom.xml
pom.xml 如下所示:
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hello-service</artifactId>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.2 配置文件 application.yml
eureka:
client:
serviceUrl:
#註冊中心的地址
defaultZone: http://localhost:8761/eureka/
server:
#當前服務端口號
port: 8762
spring:
application:
#當前應用名稱,當前應用的名稱
name: service-hi
2.2.3 控制器 HelloController.java
package com.syrdbt.study.hello.service.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Value("${server.port}")
private String port;
@RequestMapping(value = "hello", method = RequestMethod.GET)
public String index() {
return "eurekaClient:Hello World " + this.port;
}
}
2.2.4 啓動類 HelloServiceApplication.java
package com.syrdbt.study.hello.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
2.3 springcloud-study-feign
2.3.1 依賴 pom.xml
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>com.syrdbt</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>feign</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
</project>
2.3.2 配置文件 application.yml
application.yml:
server:
port: 8765
spring:
application:
# 該服務的名字
name: server-feign
eureka:
client:
serviceUrl:
# eureka服務的地址
defaultZone: http://localhost:8761/eureka/
2.3.3 Fegin 接口 HelloFeign
通過@ FeignClient(“服務名”),來指定調用哪個服務。下面的示例中調用了service-hi服務的“/hello”接口,HelloFeign.java 代碼如下:
package com.syrdbt.study.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author syrdbt
* @date 2019-10-12
*/
@FeignClient(name = "service-hi")
public interface HelloFeign {
@RequestMapping(value="/hello")
String hello();
}
2.3.4 調用 feign 接口的控制器
HelloController.java,通過上面定義的Feign客戶端 HelloFeign 來消費服務。
package com.syrdbt.study.feign.controller;
import com.syrdbt.study.feign.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author syrdbt
* @date 2019-10-12
*/
@RestController
public class HelloController {
@Autowired
private HelloFeign helloFeign;
@GetMapping(value="/hello")
public String getHi() {
return helloFeign.hello();
}
}
2.3.5 啓動類 FeignApplication
package com.syrdbt.study.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author syrdbt
* @date 2019-10-12
*/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3. 啓動並測試
需要啓動4個實例。
⚠️注意:HelloServiceApplication 需要啓動兩個,兩個的端口號分別爲 8762 和 8763 。
IDEA 啓動多實例教程:https://www.cnblogs.com/wuxun1997/p/11208519.html。
3.1 註冊中心的情況
訪問 http://localhost:8761/ 。
註冊中心註冊的 實例如下所示:
3.2 測試訪問
訪問:http://localhost:8765/hello 。
運行截圖如下所示:
參考文獻
- SpringCloud 微服務實戰 - 作者 :崔永超老師
- https://www.fangzhipeng.com/springcloud/2018/08/03/sc-f3-feign.html