springboot-starter 整合feignClient

项目结构图

引入依赖

java 复制代码
<?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接口

java 复制代码
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);
}

自动装配配置类

java 复制代码
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

java 复制代码
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文件

yaml 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.feignclientstarter.config.AutoConfiguration

使用方法

在消费者服务中引入

xml 复制代码
<dependency>
    <groupId>org.example</groupId>
    <artifactId>feignClient-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
相关推荐
Seven9720 小时前
SPI机制:服务扩展的核心技术
java
NE_STOP20 小时前
shiro_实现分布式会话SessionManager、限制密码重试次数和并发登录控制
java
Seven9720 小时前
剑指offer-63、数据流中的中位数
java
毕设源码-钟学长20 小时前
【开题答辩全过程】以 基于Spring Boot的社区养老服务管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
mjhcsp20 小时前
C++ Manacher 算法:原理、实现与应用全解析
java·c++·算法·manacher 算法
Coder_Boy_20 小时前
基于SpringAI的在线考试系统-企业级软件研发工程应用规范案例
java·运维·spring boot·软件工程·devops
indexsunny20 小时前
互联网大厂Java面试实战:微服务、Spring Boot与Kafka在电商场景中的应用
java·spring boot·微服务·面试·kafka·电商
SUDO-120 小时前
Spring Boot + Vue 2 的企业级 SaaS 多租户招聘管理系统
java·spring boot·求职招聘·sass
sheji341620 小时前
【开题答辩全过程】以 基于spring boot的停车管理系统为例,包含答辩的问题和答案
java·spring boot·后端
重生之后端学习21 小时前
21. 合并两个有序链表
java·算法·leetcode·链表·职场和发展