Spring?Boot?3.x?集成?Feign的詳細過程
一、前言
本篇主要是圍繞著兩個點,1、集成 Feign,2、分離feign接口層,獨立服務;
還有一點就是上篇文章的服務 iot-channel、system-server 服務名稱調(diào)整成為了 chain-iot-channel、chain-system
二、搭建 chain-common 服務
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 項目暫時只是空項目
二、搭建 chain-starter/chain-feign-starter 服務
chain-starter
chain-starter 服務只是一個 pom 項目,主要作用是來包含一些啟動服務,例如 chain-feign-starter 之類
chain-feign-starter
搭建這個服務的主要是目的是,后續(xù)會有很多服務會引用到 Feign 框架,如果在每個服務獨立引用 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>
四、服務配置 Feign
1、啟動服務增加注解
在 chain-system、chain-iot-channel 啟動服務都增加 @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 服務增加被調(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 服務增加調(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(); }
在這里需要注意一點的是,如果在 IotChannelInterface.java 中配置了@RequestMapping(path = "/iot/channel/open/api"),那么在 SystemForIotChannelInterfaceClient.java 中就需要增加 path = "/iot/channel/open/api" 配置
還有另一點就是如果單獨使用 Feign,沒有集成 Ribbon,那么就需要在 @FeignClient 注解中增加 url 配置項,因為沒有 Ribbon 框架是無法實現(xiàn)負載均衡,那么 name 參數(shù)的配置,不會直接調(diào)用到服務的,只能增加 url 配置
五、獨立 Feign 調(diào)用接口
1、增加 chain-open-api/chain-iot-channel-api 服務
chain-open-api
chain-open-api 和 chain-starter 服務一樣,只是一個 pom 項目,主要作用是來包含項目中每個服務對應的 open api 項目
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 服務是否可用 * * @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 { }
最后附上項目結構圖
到此這篇關于Spring Boot 3.x 集成 Feign的文章就介紹到這了,更多相關Spring Boot 3.x 集成 Feign內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring里的Async注解實現(xiàn)異步操作的方法步驟
這篇文章主要介紹了Spring里的Async注解實現(xiàn)異步操作的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04關于Unsupported major.minor version 49.0的錯誤解決辦法
這篇文章主要介紹了關于Unsupported major.minor version 49.0的錯誤解決辦法的相關資料,需要的朋友可以參考下2015-11-11IDEA創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題
這篇文章主要介紹了IDEA創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11