SpringCloud?Feign實現(xiàn)微服務之間相互請求問題
上篇文章說了通過RestTemplate實現(xiàn)微服務之間訪問:http://m.fzitv.net/article/252981.htm,這篇文章將通過Feign實現(xiàn)微服務之間訪問。
代碼基于RestTemplate實現(xiàn)微服務之間訪問基礎上進行修改。
??Feign簡介
Github:https://github.com/OpenFeign/feign
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端, Feign可以幫助我們更快捷、優(yōu)雅地實現(xiàn)微服務之間的調(diào)用。
1.Feign可幫助我們更加便捷,優(yōu)雅的調(diào)用HTTP API。
2.在SpringCloud中,使用Feign非常簡單——創(chuàng)建一個接口,并在接口上添加一些注解,代碼就完成了。
3.Feign支持多種注解,例如Feign自帶的注解或者JAX-RS注解等。
4.SpringCloud對Feign進行了增強,使Feign支持了SpringMVC注解,并整合了Ribbon和Eureka,從而讓Feign的使用更加方便。
??Spring Cloud 組件依賴版本

本文參考使用組件依賴如下

??Feign實現(xiàn)服務之間訪問
創(chuàng)建微服務項目,結(jié)構(gòu)如下圖所示

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.ber</groupId>
<artifactId>SpringCloud-Feign</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.8.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</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-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<modules>
<!-- 注冊中心 -->
<module>ber-nacos</module>
</modules>
</project>
ber-nacos 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>SpringCloud-Feign</artifactId>
<groupId>com.ber</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ber-nacos</artifactId>
<packaging>pom</packaging>
<modules>
<module>nacos-consumer</module>
<module>nacos-provider</module>
<module>nacos-consumer-feign</module>
</modules>
</project>?創(chuàng)建nacos-consumer-feign微服務
nacos-consumer-feign 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>ber-nacos</artifactId>
<groupId>com.ber</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-consumer-feign</artifactId>
<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>
</dependency>
<!-- 負載均衡 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
application.properties
spring.application.name=nacos-consumer-feign spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 server.port=8895 feign.hystrix.enabled=true
啟動類,注意添加注解@EnableFeignClients、@EnableDiscoveryClient
package com.ber.nacos.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author: ber
* @date: 2022/6/25 0025 22:43
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}controller接口,通過/queryMsg/{msgStr}訪問provider微服務接口
package com.ber.nacos.feign.controller;
import com.ber.nacos.feign.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: ber
* @date: 2022/6/25 0025 22:46
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@RestController
public class MsgController {
@Autowired
MsgService msgService;
/**
* 獲取消息
*
* @param msgStr 消息
* @return
*/
@GetMapping("/queryMsg/{msgStr}")
public String getMsg(@PathVariable(value = "msgStr") String msgStr) {
return msgService.getMsg(msgStr);
}
}
創(chuàng)建feign client
FeignClient 默認集成了 Ribbon,使用@FeignClient注解將MsgService 接口作為 FeignClient ,其屬性名稱與服務名稱nacos-provider對應
package com.ber.nacos.feign.service;
import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author: ber
* @date: 2022/6/25 0025 22:54
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {
@GetMapping("/getMsg/{msgStr}")
String getMsg(@PathVariable("msgStr") String msgStr);
}Feign的Fallback機制,在網(wǎng)絡請求時,可能會出現(xiàn)異常請求,如果還想再異常情況下使系統(tǒng)可用,那么就需要容錯處理,執(zhí)行設置的容錯業(yè)務代碼。
當接口請求不成功時,就會調(diào)用MsgServiceFallback類這里的實現(xiàn)
package com.ber.nacos.feign.service.fallback;
import com.ber.nacos.feign.service.MsgService;
import org.springframework.stereotype.Component;
/**
* @author: ber
* @date: 2022/6/25 0025 22:58
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
public class MsgServiceFallback implements MsgService {
@Override
public String getMsg(String msgStr) {
return "請求失敗";
}
}?nacos-provider微服務
代碼參考上篇文章《RestTemplate實現(xiàn)微服務之間訪問》

??Feign微服務之間訪問測試
啟動nacos-consumer-feign和nacos-provider,訪問http://localhost:8848/nacos,使用 nacos/nacos 登陸后,可以發(fā)現(xiàn)服務列表中,兩個微服務已經(jīng)注冊,如下圖所示。

訪問http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下圖所示的界面

?Feign容錯機制
修改feign client代碼如下所示
改為@GetMapping("/getMsg-error/{msgStr}"),即原本的queryMsg會被請求到getMsg-error,而provider并沒有提供getMsg-error,此時feign會進入容錯機制。
package com.ber.nacos.feign.service;
import com.ber.nacos.feign.service.fallback.MsgServiceFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author: ber
* @date: 2022/6/25 0025 22:54
* -------------------------------
* Github:https://github.com/berbai
* Blog:https://blog.csdn.net/Ber_Bai
*/
@Component
@FeignClient(name = "nacos-provider", fallback = MsgServiceFallback.class)
public interface MsgService {
@GetMapping("/getMsg-error/{msgStr}")
String getMsg(@PathVariable("msgStr") String msgStr);
}再次訪問http://localhost:8895/queryMsg/hello,即consumer-feign的接口,可以得到如下圖所示的界面

到此這篇關于SpringCloud Feign實現(xiàn)微服務之間相互請求的文章就介紹到這了,更多相關SpringCloud Feign微服務請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MybatisPlus查詢條件空字符串和NULL問題背景分析
文章詳細分析了MybatisPlus在處理查詢條件時,空字符串和NULL值的問題,MP 3.3.0及以上版本提供了多種解決方法,包括在Bean屬性上使用注解、全局配置等,推薦使用全局配置的方式來解決這個問題,以避免在SQL查詢中出現(xiàn)不必要的空字符串條件,感興趣的朋友跟隨小編一起看看吧2025-03-03
JavaWeb實現(xiàn)學生信息管理系統(tǒng)(3)
這篇文章主要為大家詳細介紹了JavaWeb實現(xiàn)學生信息管理系統(tǒng)第三篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
java 實現(xiàn)單鏈表逆轉(zhuǎn)詳解及實例代碼
這篇文章主要介紹了java 實現(xiàn)單鏈表逆轉(zhuǎn)實例代碼的相關資料,需要的朋友可以參考下2017-02-02
java文件操作代碼片斷實例實現(xiàn)統(tǒng)計文件中字母出現(xiàn)的個數(shù)功能
本文介紹java讀文件實例,實現(xiàn)統(tǒng)計某一目錄下每個文件中出現(xiàn)的字母個數(shù)、數(shù)字個數(shù)、空格個數(shù)及行數(shù),除此之外沒有其他字符,大家參考使用吧2014-01-01
淺談JDK8中的Duration Period和ChronoUnit
在JDK8中,引入了三個非常有用的時間相關的API:Duration,Period和ChronoUnit。他們都是用來對時間進行統(tǒng)計的,本文將會詳細講解一下這三個API的使用2021-06-06

