当前位置: 首页 > article >正文

springboot-starter 整合feignClient

项目结构图

在这里插入图片描述

引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>feignClient-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feignClient-starter</name>
    <description>feignClient-starter</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

feignClient接口

package org.example.feignclientstarter.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "user-server")
public interface TestFeign {
    @GetMapping("/getUser")
    Object getUser(String str);
}

自动装配配置类

package org.example.feignclientstarter.config;
import org.example.feignclientstarter.feign.TestFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
@Configurable
@EnableFeignClients(value = "org.example.feignclientstarter.config.feign", clients = TestFeign.class)
public class AutoConfiguration {
    AutoConfiguration(ApplicationContext context) {
        try {
            loadFeignClients(context);
        } catch (Exception e) {
        }
    }

    public void loadFeignClients(ApplicationContext context) throws Exception {
        AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
        Class<?> candidateClass = Class.forName("org.springframework.cloud.openfeign.FeignClientsRegistrar");
        ImportBeanDefinitionRegistrar registrar =
                BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);

        BeanDefinitionRegistry registry = null;
        if (beanFactory instanceof BeanDefinitionRegistry) {
            registry = (BeanDefinitionRegistry) beanFactory;
        }
        if (registrar instanceof EnvironmentAware) {
            EnvironmentAware envAware = (EnvironmentAware) registrar;
            envAware.setEnvironment(new StandardEnvironment());
        }
        final AnnotationMetadata metadata = new StandardAnnotationMetadata(FeignAutoConfiguration.class);
        registrar.registerBeanDefinitions(metadata, registry);
    }
}

业务方法Service

package org.example.feignclientstarter.service;
import lombok.experimental.Accessors;
import org.example.feignclientstarter.feign.TestFeign;
import org.springframework.stereotype.Service;
@Service
public class TestService {
    @Accessors
    private TestFeign testFeign;

    public Object getUser(String str) {
        return testFeign.getUser(str);
    }
}

resource下建META-INF.spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.feignclientstarter.config.AutoConfiguration

使用方法

在消费者服务中引入

<dependency>
    <groupId>org.example</groupId>
    <artifactId>feignClient-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

http://www.kler.cn/a/376310.html

相关文章:

  • Rust 力扣 - 2841. 几乎唯一子数组的最大和
  • 对于图像的关键点数据提取openpose
  • 一文了解Linux内核I2C子系统,驱动苹果MFI加密芯片
  • SQL实战训练之,力扣:1990. 统计实验的数量
  • BME680模块简介
  • Cpp多态机制的深入理解(20)
  • 方差与协方差
  • 【销帮帮-注册/登录安全分析报告-试用页面存在安全隐患】
  • C++学习大纲
  • vue和django接口联调
  • Go Modules和 雅典项目
  • C++ ----------- 栈和队列
  • 【小白学机器学习25 】 用交叉表去理解自由度的计算
  • Markdown自学第二课之记笔记——HTML第一弹
  • go语言gin框架平滑关闭——思悟项目技术2
  • 【Spark中创建RDD的两种方式】Spark中如何获取sc对象、以及创建RDD的两种方式
  • 异步编程的利之Future模式深入解析(In Depth Analysis of Future Patterns)
  • 数据可视化组件DataV在Vue3中的基本使用
  • 【GIN】go-gin 中 validator 验证功能
  • 【element el-date-picker限制时间选择范围】
  • 鸿蒙生态:机遇与挑战
  • 一对一直播源码搭建部署环境说明
  • 【性能测试】jmeter如何写入数据到文件,做持久化保存
  • linux之网络子系统-用户层接收数据包之多路复用方案(epoll)
  • jenkins 构建报错 mvn: command not found
  • 高压线路覆冰厚度测量,输电线路微波覆冰监测装置守护电网安全