一、前言
本篇主要是围绕着两个点,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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
 
             我要评论
我要评论 
                                             
                                             
                                            
发表评论