2、Spring Boot 3.x 集成 Feign

一、前言

复制代码
本篇主要是围绕着两个点,1、集成 Feign,2、分离feign接口层,独立服务;
还有一点就是上篇文章的服务 iot-channel、system-server 服务名称调整成为了 chain-iot-channel、chain-system

二、搭建 chain-common 服务

复制代码
pom.xml 
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 的配置就会很麻烦,所以现在统一管理起来
xml 复制代码
    <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 引用
xml 复制代码
        <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 服务增加被调用接口
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 服务增加调用接口
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
xml 复制代码
    <dependencies>
        <!-- 自定义 Feign -->
        <dependency>
            <groupId>com.chain</groupId>
            <artifactId>chain-feign-starter</artifactId>
            <version>${chain.version}</version>
        </dependency>
    </dependencies>
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
xml 复制代码
        <dependency>
            <groupId>com.chain</groupId>
            <artifactId>chain-iot-channel-api</artifactId>
            <version>${chain.version}</version>
        </dependency>
3、改造IotChannelInterface.java、SystemForIotChannelInterfaceClient.java
IotChannelInterface.java、
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
java 复制代码
@FeignClient(name = "chain-iot-channel", url = "http://localhost:10020", path = "/iot/channel/open/api")
public interface SystemForIotChannelInterfaceClient extends IotChannelInterfaceApi {
}

最后附上项目结构图

相关推荐
天河归来6 分钟前
springboot框架redis开启管道批量写入数据
java·spring boot·redis
合作小小程序员小小店11 分钟前
web网页,在线%食谱推荐系统%分析系统demo,基于vscode,uniapp,vue,java,jdk,springboot,mysql数据库
vue.js·spring boot·vscode·spring·uni-app
张先shen15 分钟前
Elasticsearch RESTful API入门:全文搜索实战
java·大数据·elasticsearch·搜索引擎·全文检索·restful
codervibe15 分钟前
如何用 Spring Security 构建无状态权限控制系统(含角色菜单控制)
java·后端
codervibe19 分钟前
项目中如何用策略模式实现多角色登录解耦?(附实战代码)
java·后端
TCChzp21 分钟前
synchronized全链路解析:从字节码到JVM内核的锁实现与升级策略
java·jvm
大葱白菜22 分钟前
🧩 Java 枚举详解:从基础到实战,掌握类型安全与优雅设计
java·程序员
expect7g24 分钟前
Flink-Checkpoint-2.OperatorChain
后端·flink
笑衬人心。24 分钟前
在 Mac 上安装 Java 和 IntelliJ IDEA(完整笔记)
java·macos·intellij-idea
大葱白菜24 分钟前
🧱 Java 抽象类详解:从基础到实战,掌握面向对象设计的核心基石
后端·程序员