feign/openfeign和dubbo是常用的微服務RPC框架,由于feigin內部已經集成ribbon,自帶了負載均衡的功能,當有多個同名的服務注冊到注冊中心時,會根據ribbon默認的負載均衡算法將請求分配到不同的服務。這篇文章就簡單介紹一下怎么使用feign來調用遠程的服務。
首先,需要有一個微服務注冊中心來提供服務注冊與發現,本章就使用nacos作為注冊中心。
Spring Boot整合Nacoshttps://blog.csdn.net/2501_92713943/article/details/150595053
目錄
一、服務提供者
1、創建項目
2、添加依賴
3、修改配置
4、創建接口
二、服務消費者
1、創建項目
2、添加依賴
3、修改配置
4、使用feign
創建接口
使用注解
調用接口
三、開啟Hystrix實現服務降級
1、開啟hystrix
bootstrap.yml
2、創建實現類
FeignServiceImpl.java
3、指定降級類
FeignService.java
4、測試降級功能
創建接口
UserController.java
正常訪問
服務降級
首先,要實現服務間的調用,需要有服務提供者和服務消費者,創建兩個項目,分別用于服務提供者和服務消費者。
一、服務提供者
1、創建項目
在IntelliJ IDEA中創建一個springboot項目provider
?
2、添加依賴
修改pom.xml,添加nacos注冊中心的依賴。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><groupId>cn.edu.sgu.www</groupId><artifactId>provider</artifactId><version>0.0.1-SNAPSHOT</version><description>Spring Boot整合Feign服務提供者項目</description><developers><developer><name>沐雨橙風ιε</name><roles><role>developer</role></roles><timezone>Asia/Shanghai</timezone></developer></developers><scm><url>https://gitee.com/muyu-chengfeng/provider.git</url></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.4.RELEASE</version></plugin></plugins></build>
</project>
3、修改配置
將配置文件application.properties重命名為bootstrap.yml,修改配置文件的內容。
server:port: 8088spring:application:name: providercloud:nacos:discovery:register-enabled: trueserver-addr: localhost:8848namespace: 030a5699-7f2f-4107-92a9-752655bda84e
4、創建接口
在項目根包下創建controller包,在controller包下創建一個UserController類,創建一個控制器接口/user/getUserInfo
package cn.edu.sgu.www.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)public Map<String, String> getUserInfo() {Map<String, String> resultMap = new HashMap<>();resultMap.put("age", "18");resultMap.put("name", "沐雨橙風ιε");resultMap.put("phone", "18888888888");resultMap.put("email", "h*****@163.com");return resultMap;}}
二、服務消費者
1、創建項目
在IntelliJ IDEA中創建一個springboot項目consumer
?
2、添加依賴
修改pom.xml,添加feign、nacos注冊中心的依賴。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><groupId>cn.edu.sgu.www</groupId><artifactId>consumer</artifactId><version>0.0.1-SNAPSHOT</version><description>Spring Boot整合Feign服務消費者項目</description><developers><developer><name>沐雨橙風ιε</name><roles><role>developer</role></roles><timezone>Asia/Shanghai</timezone></developer></developers><scm><url>https://gitee.com/muyu-chengfeng/consumer.git</url></scm><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.9.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.6.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.4.RELEASE</version></plugin></plugins></build>
</project>
3、修改配置
將配置文件application.properties重命名為bootstrap.yml,修改配置文件的內容。
server:port: 8089feign:hystrix:enabled: falsespring:application:name: consumercloud:nacos:discovery:register-enabled: trueserver-addr: localhost:8848namespace: 030a5699-7f2f-4107-92a9-752655bda84e
4、使用feign
創建接口
在項目根包下創建feign包,在feign包下創建一個接口FeignService
package cn.edu.sgu.www.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@FeignClient("provider")
public interface FeignService {@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)Map<String, String> getUserInfo();
}
@FeignClient("provider")指定注冊到nacos的服務名,需要調用哪個服務的接口,就寫哪個。
直接把要調用的控制器接口的方法簽名連同@RequestMapping注解復制過來,然后修改一下請求路徑,在前面添加控制器類上指定的路徑/user
使用注解
這時候@Autowired會報錯,找不到FeignService的bean,因為沒有在啟動類上面添加@EnableFeignClients注解
package cn.edu.sgu.www.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @author 沐雨橙風ιε* @version 1.0*/
@EnableFeignClients
@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
調用接口
依次啟動nacos服務器和provider項目,在測試類上調用FeignService的RPC接口方法。
package cn.edu.sgu.www.consumer;import cn.edu.sgu.www.consumer.feign.FeignService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@SpringBootTest
class ConsumerTests {@Autowiredprivate FeignService feignService;@Testvoid test() {Map<String, String> userInfo = feignService.getUserInfo();System.out.println(userInfo);}}
查看測試類運行結果,成功獲取并打印出了provider服務的/user/getUserInfo接口的返回值。
?
三、開啟Hystrix實現服務降級
1、開啟hystrix
bootstrap.yml
在comsumer項目的bootstrap.yml文件中添加以下配置
feign:hystrix:enabled: true
?完整的bootstrap.yml文件內容
server:port: 8089feign:hystrix:enabled: falsespring:application:name: consumercloud:nacos:discovery:register-enabled: trueserver-addr: localhost:8848namespace: 030a5699-7f2f-4107-92a9-752655bda84e
2、創建實現類
然后創建一個FeignService的實現類,交給spring管理。
FeignServiceImpl.java
package cn.edu.sgu.www.consumer.feign;import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@Component
public class FeignServiceImpl implements FeignService {@Overridepublic Map<String, String> getUserInfo() {Map<String, String> resultMap = new HashMap<>();resultMap.put("code", "404");resultMap.put("message", "服務請求失敗,已經執行降級方法!");return resultMap;}}
3、指定降級類
最后,在FeiginService接口的的@FeiginCilent注解上指定fallback=FeignServiceImpl.class
@FeignClient(value = "provider", fallback = FeignServiceImpl.class)
FeignService.java
package cn.edu.sgu.www.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@FeignClient(value = "provider", fallback = FeignServiceImpl.class)
public interface FeignService {@RequestMapping(value = "/user/getUserInfo", method = RequestMethod.GET)Map<String, String> getUserInfo();
}
4、測試降級功能
創建接口
UserController.java
把UserController.java從provider項目中連同controller包復制過來,修改接口的代碼,調用FeignService的方法。
package cn.edu.sgu.www.consumer.controller;import cn.edu.sgu.www.consumer.feign.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;/*** @author 沐雨橙風ιε* @version 1.0*/
@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {private final FeignService feignService;@Autowiredpublic UserController(FeignService feignService) {this.feignService = feignService;}@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)public Map<String, String> getUserInfo() {return feignService.getUserInfo();}}
正常訪問
啟動consumer項目,在瀏覽器地址欄輸入以下網址。
http://localhost:8089/user/getUserInfo
可以看到正常返回了數據。
?
服務降級
接著關掉provider項目,刷新頁面,成功執行降級方法,返回了錯誤提示。
?
好了,springboot整合feign的介紹到這里了,
文章代碼已上傳到Gitee,可按需獲取~
服務提供者
Spring Boot整合Feign服務提供者項目https://gitee.com/muyu-chengfeng/provider.git服務消費者
Spring Boot整合Feign服務消費者項目https://gitee.com/muyu-chengfeng/consumer.git