Spring?Boot?3.x?集成?Feign的詳細(xì)過程
一、前言
本篇主要是圍繞著兩個點(diǎn),1、集成 Feign,2、分離feign接口層,獨(dú)立服務(wù);
還有一點(diǎn)就是上篇文章的服務(wù) iot-channel、system-server 服務(wù)名稱調(diào)整成為了 chain-iot-channel、chain-system
二、搭建 chain-common 服務(wù)
pom.xml
<properties> <!-- lombok --> <lombok.version>1.18.26</lombok.version> </properties> <!-- Dependencies --> <dependencies> <!-- Lombok Dependency --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies>
chain-common 項(xiàng)目暫時只是空項(xiàng)目
二、搭建 chain-starter/chain-feign-starter 服務(wù)
chain-starter
chain-starter 服務(wù)只是一個 pom 項(xiàng)目,主要作用是來包含一些啟動服務(wù),例如 chain-feign-starter 之類
chain-feign-starter
搭建這個服務(wù)的主要是目的是,后續(xù)會有很多服務(wù)會引用到 Feign 框架,如果在每個服務(wù)獨(dú)立引用 Feign,在后續(xù)的升級版本或需要增加 Feign 的配置就會很麻煩,所以現(xiàn)在統(tǒng)一管理起來
<dependencies> <!-- feign 客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
三、chain-system、chain-iot-channel 集成 Feign
pom.xml 增加 Feign 引用
<dependency> <groupId>com.chain</groupId> <artifactId>chain-feign-starter</artifactId> <version>${chain.version}</version> </dependency>
四、服務(wù)配置 Feign
1、啟動服務(wù)增加注解
在 chain-system、chain-iot-channel 啟動服務(wù)都增加 @EnableFeignClients 注解,開發(fā)Feign 客戶端
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class IotChannelServeApp { public static void main(String[] args) { SpringApplication.run(IotChannelServeApp.class, args); } }
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SystemServerApp { public static void main(String[] args) { SpringApplication.run(SystemServerApp.class); } }
2、chain-iot-channel 服務(wù)增加被調(diào)用接口
IotChannelInterface.java
@RestController @RequestMapping(path = "/iot/channel/open/api") public class IotChannelInterface { @Override @GetMapping(path = "/testIotChannelFeign") public String testIotChannelFeign() { return "test iot channel feign open api"; } }
3、chain-system 服務(wù)增加調(diào)用接口
SystemForIotChannelInterfaceClient.java
@FeignClient(name = "chain-iot-channel", url = "http://localhost:10020", path = "/iot/channel/open/api") public interface SystemForIotChannelInterfaceClient { @GetMapping(path = "/testIotChannelFeign") String testIotChannelFeign(); }
在這里需要注意一點(diǎn)的是,如果在 IotChannelInterface.java 中配置了@RequestMapping(path = "/iot/channel/open/api"),那么在 SystemForIotChannelInterfaceClient.java 中就需要增加 path = "/iot/channel/open/api" 配置
還有另一點(diǎn)就是如果單獨(dú)使用 Feign,沒有集成 Ribbon,那么就需要在 @FeignClient 注解中增加 url 配置項(xiàng),因?yàn)闆]有 Ribbon 框架是無法實(shí)現(xiàn)負(fù)載均衡,那么 name 參數(shù)的配置,不會直接調(diào)用到服務(wù)的,只能增加 url 配置
五、獨(dú)立 Feign 調(diào)用接口
1、增加 chain-open-api/chain-iot-channel-api 服務(wù)
chain-open-api
chain-open-api 和 chain-starter 服務(wù)一樣,只是一個 pom 項(xiàng)目,主要作用是來包含項(xiàng)目中每個服務(wù)對應(yīng)的 open api 項(xiàng)目
chain-iot-channel-api pom.xml
<dependencies> <!-- 自定義 Feign --> <dependency> <groupId>com.chain</groupId> <artifactId>chain-feign-starter</artifactId> <version>${chain.version}</version> </dependency> </dependencies>
IotChannelInterfaceApi.java
public interface IotChannelInterfaceApi { /** * 測試 iot channel 服務(wù)是否可用 * * @return String */ @GetMapping(path = "/testIotChannelFeign") String testIotChannelFeign(); }
2、增加對 chain-iot-channel-api 的引用
chain-iot-channel\chain-system
pom.xml
<dependency> <groupId>com.chain</groupId> <artifactId>chain-iot-channel-api</artifactId> <version>${chain.version}</version> </dependency>
3、改造IotChannelInterface.java、SystemForIotChannelInterfaceClient.java
IotChannelInterface.java、
@RestController @RequestMapping(path = "/iot/channel/open/api") public class IotChannelInterface implements IotChannelInterfaceApi { @Override public String testIotChannelFeign() { return "test iot channel feign open api"; } }
SystemForIotChannelInterfaceClient.java
@FeignClient(name = "chain-iot-channel", url = "http://localhost:10020", path = "/iot/channel/open/api") public interface SystemForIotChannelInterfaceClient extends IotChannelInterfaceApi { }
最后附上項(xiàng)目結(jié)構(gòu)圖
到此這篇關(guān)于Spring Boot 3.x 集成 Feign的文章就介紹到這了,更多相關(guān)Spring Boot 3.x 集成 Feign內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中@FeignClient 注解的作用
- SpringBoot動態(tài)Feign服務(wù)調(diào)用詳解
- SpringCloud解決Feign異步回調(diào)問題(SpringBoot+Async+Future實(shí)現(xiàn))
- springBoot使用openfeign來遠(yuǎn)程調(diào)用的實(shí)現(xiàn)
- 使用SpringBoot項(xiàng)目導(dǎo)入openfeign版本的問題
- @FeignClient的使用和Spring?Boot的版本適配方式
- springboot啟動feign項(xiàng)目報(bào)錯:Service id not legal hostnam的解決
相關(guān)文章
Spring里的Async注解實(shí)現(xiàn)異步操作的方法步驟
這篇文章主要介紹了Spring里的Async注解實(shí)現(xiàn)異步操作的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法
這篇文章主要介紹了關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2015-11-11IDEA創(chuàng)建maven項(xiàng)目時在tomcat運(yùn)行瀏覽器404的問題
這篇文章主要介紹了IDEA創(chuàng)建maven項(xiàng)目時在tomcat運(yùn)行瀏覽器404的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11