Spring Boot 3.x 集成 Feign的详细过程

 更新时间:2024年09月30日 10:19:01   作者:Kenny.志  
本文阐述了如何在SpringBoot3.x中集成Feign,以实现微服务之间的调用,主要步骤包括:搭建chain-common服务,创建chain-starter/chain-feign-starter服务,集成Feign到chain-system和chain-iot-channel服务,配置Feign,感兴趣的朋友一起看看吧

一、前言

本篇主要是围绕着两个点,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 客户端

@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 服务增加被调用接口

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 服务增加调用接口

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 框架是无法实现负载均衡,那么 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>

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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java设计模式之策略模式示例详解

    Java设计模式之策略模式示例详解

    这篇文章主要为大家详细介绍了Java的策略模式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • java使用IO流对数组排序实例讲解

    java使用IO流对数组排序实例讲解

    在本篇文章里小编给大家整理的是一篇关于java使用IO流对数组排序实例讲解内容,有兴趣的朋友们可以学习下。
    2021-02-02
  • 详解Spring-Boot中如何使用多线程处理任务

    详解Spring-Boot中如何使用多线程处理任务

    本篇文章主要介绍了详解Spring-Boot中如何使用多线程处理任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 深入解析Apache Kafka实时流处理平台

    深入解析Apache Kafka实时流处理平台

    这篇文章主要为大家介绍了Apache Kafka实时流处理平台深入解析,从基本概念到实战操作详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Spring 跨域配置请求详解

    Spring 跨域配置请求详解

    这篇文章主要介绍了Spring 跨域配置请求详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • SpringBoot 多Profile使用与切换方式

    SpringBoot 多Profile使用与切换方式

    这篇文章主要介绍了SpringBoot 多Profile使用与切换方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Spring里的Async注解实现异步操作的方法步骤

    Spring里的Async注解实现异步操作的方法步骤

    这篇文章主要介绍了Spring里的Async注解实现异步操作的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 关于Unsupported major.minor version 49.0的错误解决办法

    关于Unsupported major.minor version 49.0的错误解决办法

    这篇文章主要介绍了关于Unsupported major.minor version 49.0的错误解决办法的相关资料,需要的朋友可以参考下
    2015-11-11
  • IDEA创建maven项目时在tomcat运行浏览器404的问题

    IDEA创建maven项目时在tomcat运行浏览器404的问题

    这篇文章主要介绍了IDEA创建maven项目时在tomcat运行浏览器404的问题及解决方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • IDEA接入Deepseek的图文教程

    IDEA接入Deepseek的图文教程

    在本篇文章中,我们将详细介绍如何在 JetBrains IDEA 中使用 Continue 插件接入 DeepSeek,让你的 AI 编程助手更智能,提高开发效率,感兴趣的小伙伴跟着小编一起来看看吧
    2025-03-03

最新评论