2、Spring Boot 3.x 集成 Feign

news/2024/10/4 9:18:22/

一、前言

本篇主要是围绕着两个点,1、集成 Feign,2、分离feign接口层,独立服务;
还有一点就是上篇文章的服务 iot-channel、system-server 服务名称调整成为了 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
搭建这个服务的主要是目的是,后续会有很多服务会引用到 Feign 框架,如果在每个服务独立引用 Feign,在后续的升级版本或需要增加 Feign 的配置就会很麻烦,所以现在统一管理起来
    <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 注解,开发Feign 客户端
java">@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class IotChannelServeApp {public static void main(String[] args) {SpringApplication.run(IotChannelServeApp.class, args);}
}
java">@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SystemServerApp {public static void main(String[] args) {SpringApplication.run(SystemServerApp.class);}
}
2、chain-iot-channel 服务增加被调用接口
java_85">IotChannelInterface.java
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 服务增加调用接口
java_103">SystemForIotChannelInterfaceClient.java
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 框架是无法实现负载均衡,那么 name 参数的配置,不会直接调用到服务的,只能增加 url 配置

五、独立 Feign 调用接口

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>
java_136">IotChannelInterfaceApi.java
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>
javaSystemForIotChannelInterfaceClientjava_163">3、改造IotChannelInterface.java、SystemForIotChannelInterfaceClient.java
java_165">IotChannelInterface.java
java">@RestController
@RequestMapping(path = "/iot/channel/open/api")
public class IotChannelInterface implements IotChannelInterfaceApi {@Overridepublic String testIotChannelFeign() {return "test iot channel feign open api";}
}
java_179">SystemForIotChannelInterfaceClient.java
java">@FeignClient(name = "chain-iot-channel", url = "http://localhost:10020", path = "/iot/channel/open/api")
public interface SystemForIotChannelInterfaceClient extends IotChannelInterfaceApi {
}

最后附上项目结构图
在这里插入图片描述


http://www.ppmy.cn/news/1534331.html

相关文章

C# (.net6)实现Redis发布和订阅简单案例

概念&#xff1a; 在 .NET 6 中使用 Redis 的/订发布阅模式。发布/订阅&#xff08;Pub/Sub&#xff09;是 Redis 支持的一种消息传递模式&#xff0c;其中一个或多个发布者向一个或多个订阅者发送消息,Redis 客户端可以订阅任意数量的频道。 多个客户端可以订阅一个相同的频道…

MongoDB伪分布式部署(mac M2)

1. 序言 本博客是上一博客的进阶版&#xff1a;mac M2安装单机版 MongoDB 7.x&#xff0c;上一博客可以看做是单机、单节点部署MongoDB本博客将介绍单机、多服务部署MongoDB&#xff0c;实际就是伪分布式部署 2. 副本集(Replica Set)方式部署 2.1 什么是副本集&#xff1f; …

Netgear-WN604 downloadFile.php 信息泄露复现(CVE-2024-6646)

0x01 产品描述&#xff1a; NETGEAR WN604是一款功能强大的双频AC1200无线路由器,非常适合中大型家庭和企业使用。它支持最新的802.11ac无线标准,能提供高达1200Mbps的无线传输速度。路由器具备千兆有线网口和3个100Mbps有线网口,可满足有线和无线设备的接入需求。此外,它还内置…

element-ui 通过按钮式触发日期选择器

element ui 写在前面1. 自定义的日期时间组件CustomDatePicker.vue2. 页面效果总结写在最后 写在前面 需求&#xff1a;elementui中日期时间选择器&#xff0c;目前只能通过点击input输入框触发日期选择器&#xff0c;我希望能通过其他方式触发日期选择器同时把input输入框去掉…

git如何将多个提交合并为一个提交

目录 第一种&#xff1a;使用git rebase命令 第二种&#xff1a;使用git reset命令 重新提交 第一种&#xff1a;使用git rebase命令 使用以下命令的其中一种启动交互式 rebase git rebase -i 你想要合并提交的父提交的哈希值git rebase -i <commit-hash>^ &#…

实战4、爬取淘宝商品数据-selenium模拟

1、登录 在页面中找到登录按键&#xff0c;使用selenium模拟点击 button_login wd.find_element(By.XPATH,"//li[idJ_SiteNavMytaobao]/div[classsite-nav-menu-hd]/a[target_top]")button_login.click()找到用户名密码所在位置模拟输入 # 模拟输入账密button_us…

正向代理与反向代理:原理、区别以及应用(Nginx 和 Tomcat)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言1. 实现原理正向代理工作流程&#xff1a; 反向代理工作流程&#xff1a; 区别 2. 使用案例Nginx作为正向代理Nginx作为反向代理Tomcat作为反向代理 3. 适用场景…

C#绘制动态曲线

前言 用于实时显示数据动态曲线&#xff0c;比如&#xff1a;SOC。 //用于绘制动态曲线&#xff0c;可置于定时函数中&#xff0c;定时更新数据曲线 void DrawSocGraph() {double f (double)MainForm.readData[12]; //display datachart1.Series[0].Points.Add(f);if (ch…