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

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

??Feign實(shí)現(xiàn)服務(wù)之間訪問
創(chuàng)建微服務(wù)項(xià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>
<!-- 注冊(cè)中心 -->
<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微服務(wù)
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>
<!-- 負(fù)載均衡 -->
<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
啟動(dòng)類,注意添加注解@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微服務(wù)接口
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 默認(rèn)集成了 Ribbon,使用@FeignClient注解將MsgService 接口作為 FeignClient ,其屬性名稱與服務(wù)名稱nacos-provider對(duì)應(yīng)
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機(jī)制,在網(wǎng)絡(luò)請(qǐng)求時(shí),可能會(huì)出現(xiàn)異常請(qǐng)求,如果還想再異常情況下使系統(tǒng)可用,那么就需要容錯(cuò)處理,執(zhí)行設(shè)置的容錯(cuò)業(yè)務(wù)代碼。
當(dāng)接口請(qǐng)求不成功時(shí),就會(huì)調(diào)用MsgServiceFallback類這里的實(shí)現(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 "請(qǐng)求失敗";
}
}?nacos-provider微服務(wù)
代碼參考上篇文章《RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問》

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

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

?Feign容錯(cuò)機(jī)制
修改feign client代碼如下所示
改為@GetMapping("/getMsg-error/{msgStr}"),即原本的queryMsg會(huì)被請(qǐng)求到getMsg-error,而provider并沒有提供getMsg-error,此時(shí)feign會(huì)進(jìn)入容錯(cuò)機(jī)制。
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的接口,可以得到如下圖所示的界面

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

