news 2026/6/3 18:22:07

Spring Cloud OpenFeign.D.1:详述及示例

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring Cloud OpenFeign.D.1:详述及示例

Spring Cloud OpenFeign

1、概述

文档:https://spring.io/projects/spring-cloud-openfeign

该项目通过自动配置以及与 Spring 环境及其他 Spring 编程模型惯用方法的绑定,为 Spring Boot 应用程序提供了 https://github.com/OpenFeign/feign[OpenFeign] 的集成支持。

特征

  • 声明式 REST 客户端:Feign 会为带有 JAX-RS 或 Spring MVC 注解修饰的接口创建一个动态实现。

开始

@SpringBootApplication @EnableFeignClients public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } @FeignClient("name") static interface NameService { @RequestMapping("/") public String getName(); } }

2、介绍

文档:https://docs.spring.io/spring-cloud-openfeign/reference/index.html

本项目通过自动配置以及与 Spring 环境的绑定以及遵循其他 Spring 编程模型的惯用方法,为 Spring Boot 应用程序提供了 OpenFeign 的集成功能。

正如在《Spring Cloud 2022.0.0 发布博客文章》中所宣布的那样,我们现在已将 Spring Cloud OpenFeign 项目视为功能完备。我们将仅添加错误修复内容,并可能合并一些来自社区的小型功能提案。我们建议转而使用 Spring HTTP Service Clients.。

一、Features(特征)

文档:https://docs.spring.io/spring-cloud-openfeign/reference/spring-cloud-openfeign.html

声明式 REST 客户端:Feign

Feign 是一种声明式的网络服务客户端。它使得编写网络服务客户端变得更加容易。要使用 Feign,需创建一个interface(接口)并对其进行标注。它具有可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解。Feign 还支持可插拔的编码器和解码器。Spring Cloud 增加了对 Spring MVC 注解的支持,并支持使用 Spring Web 中默认使用的相同的HttpMessageConverters。Spring Cloud 将 Eureka、Spring Cloud CircuitBreaker 以及 Spring Cloud LoadBalancer 集成在一起,以便在使用 Feign 时提供一个负载均衡的 HTTP 客户端。

1、如何引入 Feign

要在您的项目中引入 Feign,请使用具有org.springframework.cloudgroup 和spring-cloud-starter-openfeignartifactId的启动器。有关使用当前 Spring Cloud 发布版设置构建系统的详细信息,请参阅 Spring Cloud Project。

示例 Spring Boot 应用程序

@SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

StoreClient.java

@FeignClient("stores") public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); @GetMapping("/stores") Page<Store> getStores(Pageable pageable); @PostMapping(value = "/stores/{storeId}", consumes = "application/json", params = "mode=upsert") Store update(@PathVariable("storeId") Long storeId, Store store); @DeleteMapping("/stores/{storeId:\\d+}") void delete(@PathVariable Long storeId); }

@FeignClient注解中,字符串值(如上述的“stores”)是一个任意的客户端名称,用于创建一个 Spring Cloud LoadBalancer client(负载均衡)。您还可以使用 url 属性(绝对值或仅主机名)来指定一个 URL。应用程序上下文中的 bean 名称是接口的完全限定名称。如果您要指定自己的别名值,可以使用 @FeignClient 注解的qualifiers值。

上述的load-balancer(负载均衡器)客户端将需要获取"stores"服务的物理地址。如果您的应用程序是 Eureka 客户端,那么它会从 Eureka 服务注册表中解析该服务。如果您不想使用 Eureka,也可以通过使用SimpleDiscoveryClient在外部配置中配置服务器列表。

Spring Cloud OpenFeign 支持 Spring Cloud LoadBalancer(负载均衡)的blocking mode(阻塞模式)所具备的所有功能。您可以在project documentation中了解更多相关内容。

若要在带有@Configuration注解的类中使用@EnableFeignClients注解,请务必明确指定客户端所在的包路径,例如:

@EnableFeignClients(basePackages = "com.example.clients") 或者明确列出它们 @EnableFeignClients(clients = InventoryServiceFeignClient.class)

在多模块设置中加载 Spring Feign 客户端 bean 时,您需要直接指定相关包。

由于 FactoryBean 对象可能在初始上下文刷新之前就被实例化,而 Spring Cloud OpenFeign 客户端的实例化会触发上下文刷新,因此这些对象不应在 FactoryBean 类中进行声明。

属性解析模式

在创建 Feign 客户端 bean 时,我们会解析通过 @FeignClient 注解传递的值。自 4.x 版本起,这些值是被立即解析的。对于大多数用例来说,这是一个很好的解决方案,并且还支持 AOT(提前编译)。

如果您希望属性的解析是延迟进行的,请将 spring.cloud.openfeign.lazy-attributes-resolution 属性的值设置为 true 。

对于 Spring Cloud Contract 测试集成,应使用延迟属性解析功能。

2、覆盖Feign默认设置

Spring Cloud 的 Feign 支持中的一个核心概念是“命名客户端”。每个 Feign 客户端都是一个组件集合的一部分,这些组件协同工作以根据需要联系远程服务器,而这个集合有一个名称,作为应用程序开发人员使用 @FeignClient 注解时所赋予的。Spring Cloud 会根据每个命名客户端的需求在ApplicationContext中创建一个新的集合,使用FeignClientsConfiguration进行处理。这包含(以及其他内容)feign.Decoder,feign.Encoderfeign.Contract.。可以通过使用@FeignClient注解的contextId属性来覆盖这个集合的名称。

Spring Cloud 允许您通过使用@FeignClient注解来声明额外的配置(在FeignClientsConfiguration的基础上),从而完全掌控 Feign 客户端。示例:

@FeignClient(name = "stores", configuration = FooConfiguration.class) public interface StoreClient { //.. }

在这种情况下,客户端是由FeignClientsConfiguration中已有的组件以及FooConfiguration中的任何组件组合而成的(其中后者会覆盖前者)。

FooConfiguration无需使用@Configuration进行标注。但如果使用了该注解,则需注意将其排除在任何会将此配置包含在内的@ComponentScan中之外,因为这样一来,它将成为 feign 的默认数据源,包括feign.Decoder,feign.Encoder,feign.Contract等相关组件。为避免这种情况,可以将其放在与任何@ComponentScan@SpringBootApplication不重叠的单独包中,或者在@ComponentScan中明确排除它。
通过在@FeignClient注解中使用contextId属性,并同时更改ApplicationContext集合的名称,将能够覆盖客户端名称的别名,并将其用作为该客户端所创建的配置 bean 名称的一部分。

S.1、Spring Cloud OpenFeign在Nacos中集成示例

1、父POM

(示例:openfeign-nacos-b3-example)

<?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> <groupId>org.example.springcloud.openfeign</groupId> <artifactId>openfeign-nacos-b3-example</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>openfeign-nacos-provider-example</module> <module>openfeign-nacos-consumer-example</module> </modules> <properties> <!-- Spring Cloud Alibaba --> <spring.cloud.alibaba.version>2025.0.0.0</spring.cloud.alibaba.version> <!-- Spring Cloud --> <spring.cloud.version>2025.0.0</spring.cloud.version> <!-- Spring Boot --> <spring.boot.version>3.5.8</spring.boot.version> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot BOM --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud BOM --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Cloud Alibaba BOM --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </project>

2、服务提供者(注册到Nacos)

(示例:openfeign-nacos-provider-example),提供一个最简单的服务,并把服务注册到Nacos中。

1.pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>openfeign-nacos-b3-example</artifactId> <groupId>org.example.springcloud.openfeign</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>openfeign-nacos-provider-example</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringCloud Alibaba Nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>

2.application.yaml

server: port: 9001 spring: application: name: openfeign-nacos-provider-example cloud: nacos: discovery: server-addr: 192.168.0.227:8848

3.启动类(OpenFeignNacosProviderApplication)

package org.example.openfeign.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class OpenFeignNacosProviderApplication { public static void main(String[] args) { SpringApplication.run(OpenFeignNacosProviderApplication.class, args); } }

4.提供一个服务(HelloController)

package org.example.openfeign.provider.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "hello world"; } }

5.结果

注册到Nacos中

访问结果

3、服务消费者

(示例:openfeign-nacos-consumer-example),既是服务消费者也是服务提供者。

1.pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>openfeign-nacos-b3-example</artifactId> <groupId>org.example.springcloud.openfeign</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>openfeign-nacos-consumer-example</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringCloud Alibaba Nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--OpenFeign为HTTP形式的Rest API提供了非常简洁高效的RPC调用方式--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--loadbalancer是Spring Cloud官方自己提供的客户端负载均衡器,抽象和实现,用来替代Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency> </dependencies> </project>

2.application.yaml

server: port: 9002 spring: application: name: openfeign-nacos-consumer-example cloud: nacos: discovery: server-addr: 192.168.0.227:8848

3.启动类(OpenFeignNacosConsumerApplication)

package org.example.openfeign.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class OpenFeignNacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(OpenFeignNacosConsumerApplication.class, args); } }

4.Feign Client(HelloClient)

package org.example.openfeign.consumer.openfeignclients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; // 括号中为目标服务名 @FeignClient("openfeign-nacos-provider-example") public interface HelloClient { // 目标方法的url @GetMapping("hello") String hello(); }

5.消费者(ConsumerController)

package org.example.openfeign.consumer.controller; import org.example.openfeign.consumer.openfeignclients.HelloClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController public class ConsumerController { @Autowired HelloClient helloClient; @GetMapping("/feign") public String test() { return helloClient.hello()+" from Feign at "+new Date(); } }

6.结果

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/22 20:43:18

大模型AI产品经理学习资源,价值2万的资料免费共享_大模型多个岗位详解,非常详细收藏我这一篇就够了

本文详细介绍了9个大模型相关岗位的职责与要求&#xff0c;并提供了价值2万元的大模型&AI产品经理学习资源包&#xff0c;包括7阶段学习路线图、300集视频教程、200本技术书籍及面试题合集&#xff0c;覆盖从入门到实战的全流程&#xff0c;适合小白和程序员系统学习大模型…

作者头像 李华
网站建设 2026/6/1 10:04:40

fpga lvds接口显示屏驱动

驱动源码: //------------------------------------文件信息--------------------------------------- // 文件名称: lcd2lvds_convert.v // 最后修改日期: 2018-5-3 // 最新版本: 1.0 // 功能描述: LCD数据格式转LVDS数据格式 // /…

作者头像 李华
网站建设 2026/6/3 1:32:43

Depth-Wise Emergence of Prediction-Centric Geometry in Large Language Models

Depth-Wise Emergence of Prediction-Centric Geometry in Large Language Models Authors: Shahar Haim, Daniel C McNamee Deep-Dive Summary: 论文总结&#xff1a;ControlNet - 为文本到图像扩散模型添加条件控制 这篇文章介绍了一种名为 ControlNet 的神经网络架构&am…

作者头像 李华
网站建设 2026/5/30 19:43:46

Flutter for OpenHarmony 实战_吃豆人游戏幽灵AI与绘制技术

Flutter for OpenHarmony 实战&#xff1a;吃豆人游戏幽灵AI与绘制技术 欢迎加入开源鸿蒙跨平台社区&#xff1a;开源鸿蒙跨平台开发者社区 幽灵是吃豆人游戏中最具挑战性的元素&#xff0c;它们的AI行为和视觉效果直接影响游戏的难度和吸引力。本文将详细介绍幽灵的数据结构…

作者头像 李华
网站建设 2026/5/29 12:32:33

基于8086计算器系统仿真设计

一 概要基于8086计算器系统仿真设计是一个结合了硬件与软件技术的综合性项目&#xff0c;旨在通过仿真技术模拟实现一个能够执行基本算术运算的计算器系统。以下是对该设计概要的详细阐述&#xff1a; 一、设计目标 该设计的主要目标是利用8086微处理器为核心&#xff0c;结合适…

作者头像 李华
网站建设 2026/5/26 9:06:22

QGIS应用教学——降雨量的空间插值与等值线绘制

前期准备1.QGIS(安装教程见本账号文章) 2.气象数据&#xff08;请到地球资源数据云中学术社区的同名文章下载&#xff09;一、从孤立的观测站到连续的雨量图当我们打开附件中从 NOAA&#xff08;美国国家海洋和大气管理局&#xff09;下载的气象数据时&#xff0c;映入眼帘的往…

作者头像 李华