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

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

 更新時間:2024年09月30日 10:19:01   作者:Kenny.志  
本文闡述了如何在SpringBoot3.x中集成Feign,以實(shí)現(xiàn)微服務(wù)之間的調(diào)用,主要步驟包括:搭建chain-common服務(wù),創(chuàng)建chain-starter/chain-feign-starter服務(wù),集成Feign到chain-system和chain-iot-channel服務(wù),配置Feign,感興趣的朋友一起看看吧

一、前言

本篇主要是圍繞著兩個點(diǎn),1、集成 Feign,2、分離feign接口層,獨(dú)立服務(wù);
還有一點(diǎn)就是上篇文章的服務(wù) iot-channel、system-server 服務(wù)名稱調(diào)整成為了 chain-iot-channel、chain-system

二、搭建 chain-common 服務(wù)

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 項(xiàng)目暫時只是空項(xiàng)目

二、搭建 chain-starter/chain-feign-starter 服務(wù)

chain-starter

chain-starter 服務(wù)只是一個 pom 項(xiàng)目,主要作用是來包含一些啟動服務(wù),例如 chain-feign-starter 之類

chain-feign-starter

搭建這個服務(wù)的主要是目的是,后續(xù)會有很多服務(wù)會引用到 Feign 框架,如果在每個服務(wù)獨(dú)立引用 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>

四、服務(wù)配置 Feign

1、啟動服務(wù)增加注解

在 chain-system、chain-iot-channel 啟動服務(wù)都增加 @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 服務(wù)增加被調(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 服務(wù)增加調(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();
}

在這里需要注意一點(diǎn)的是,如果在 IotChannelInterface.java 中配置了@RequestMapping(path = "/iot/channel/open/api"),那么在 SystemForIotChannelInterfaceClient.java 中就需要增加 path = "/iot/channel/open/api" 配置
還有另一點(diǎn)就是如果單獨(dú)使用 Feign,沒有集成 Ribbon,那么就需要在 @FeignClient 注解中增加 url 配置項(xiàng),因?yàn)闆]有 Ribbon 框架是無法實(shí)現(xiàn)負(fù)載均衡,那么 name 參數(shù)的配置,不會直接調(diào)用到服務(wù)的,只能增加 url 配置

五、獨(dú)立 Feign 調(diào)用接口

1、增加 chain-open-api/chain-iot-channel-api 服務(wù)

chain-open-api

chain-open-api 和 chain-starter 服務(wù)一樣,只是一個 pom 項(xiàng)目,主要作用是來包含項(xiàng)目中每個服務(wù)對應(yīng)的 open api 項(xiàng)目

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 服務(wù)是否可用
     *
     * @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 {
}

最后附上項(xiàng)目結(jié)構(gòu)圖

到此這篇關(guān)于Spring Boot 3.x 集成 Feign的文章就介紹到這了,更多相關(guān)Spring Boot 3.x 集成 Feign內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java設(shè)計(jì)模式之策略模式示例詳解

    Java設(shè)計(jì)模式之策略模式示例詳解

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

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

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

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

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

    深入解析Apache Kafka實(shí)時流處理平臺

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

    Spring 跨域配置請求詳解

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

    SpringBoot 多Profile使用與切換方式

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

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

    這篇文章主要介紹了Spring里的Async注解實(shí)現(xiàn)異步操作的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法

    關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法

    這篇文章主要介紹了關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下
    2015-11-11
  • IDEA創(chuàng)建maven項(xiàng)目時在tomcat運(yùn)行瀏覽器404的問題

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

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

    IDEA接入Deepseek的圖文教程

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

最新評論