欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Boot?3.x?集成?Feign的詳細過程

 更新時間:2024年09月30日 10:19:01   作者:Kenny.志  
本文闡述了如何在SpringBoot3.x中集成Feign,以實現(xiàn)微服務之間的調(diào)用,主要步驟包括:搭建chain-common服務,創(chuàng)建chain-starter/chain-feign-starter服務,集成Feign到chain-system和chain-iot-channel服務,配置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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java設計模式之策略模式示例詳解

    Java設計模式之策略模式示例詳解

    這篇文章主要為大家詳細介紹了Java的策略模式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • java使用IO流對數(shù)組排序?qū)嵗v解

    java使用IO流對數(shù)組排序?qū)嵗v解

    在本篇文章里小編給大家整理的是一篇關于java使用IO流對數(shù)組排序?qū)嵗v解內(nèi)容,有興趣的朋友們可以學習下。
    2021-02-02
  • 詳解Spring-Boot中如何使用多線程處理任務

    詳解Spring-Boot中如何使用多線程處理任務

    本篇文章主要介紹了詳解Spring-Boot中如何使用多線程處理任務,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • 深入解析Apache Kafka實時流處理平臺

    深入解析Apache Kafka實時流處理平臺

    這篇文章主要為大家介紹了Apache Kafka實時流處理平臺深入解析,從基本概念到實戰(zhàn)操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Spring 跨域配置請求詳解

    Spring 跨域配置請求詳解

    這篇文章主要介紹了Spring 跨域配置請求詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-01-01
  • SpringBoot 多Profile使用與切換方式

    SpringBoot 多Profile使用與切換方式

    這篇文章主要介紹了SpringBoot 多Profile使用與切換方式,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Spring里的Async注解實現(xiàn)異步操作的方法步驟

    Spring里的Async注解實現(xiàn)異步操作的方法步驟

    這篇文章主要介紹了Spring里的Async注解實現(xiàn)異步操作的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    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創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題

    IDEA創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題

    這篇文章主要介紹了IDEA創(chuàng)建maven項目時在tomcat運行瀏覽器404的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • IDEA接入Deepseek的圖文教程

    IDEA接入Deepseek的圖文教程

    在本篇文章中,我們將詳細介紹如何在 JetBrains IDEA 中使用 Continue 插件接入 DeepSeek,讓你的 AI 編程助手更智能,提高開發(fā)效率,感興趣的小伙伴跟著小編一起來看看吧
    2025-03-03

最新評論