Spring-cloud-feign的学习总结

本文详细解析了FeignClient在Spring Cloud微服务架构中的应用技巧,包括配置版本、Java版本、Spring Cloud版本等环境搭建,以及@FeignClient注解使用要点,如value属性与application.yml中application.name的对应关系,方法参数注解的重要性,RestController与FeignClient接口映射的一致性等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码git地址:github地址
首先版本号:

<parent>  
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
		<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

一共几个记忆点:
1、提供feign的项目的interface,注解@FeignClient的value 需要与该项目的application.yml中application.name相同,否则会报错:com.netflix.client.ClientException: Load balancer does not have available server for client: USER-SERVER3
正确样式如图:在这里插入图片描述
在这里插入图片描述

2、在feignClient注解的方法中,如上图中的TestService中的方法sayHello,参数必须要加注解,如果不加注解,在其他项目中会无法传参到RestController的方法中去。否则会报错:feign.FeignException: status 405 reading TestService#sayHello(String); content: {"timestamp":"2019-07-05T06:05:45.679+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/hello2"}

3、对于feign的理解,这个@RestController更像是feignClient注解下interface的实现类。切记@RestController方法中mapping地址需要和@FeignClient下void的mapping地址一样。

同时网上很多说@GetMapping和PostMapping不能用的问题,我觉得也不存在,可能是我现在的版本支持吧

`spring-cloud-starter-feign` 是早期版本中使用的依赖名称,但在 Spring Cloud 的后续版本中已经被废弃。取而代之的是 `spring-cloud-starter-openfeign`[^3]。 如果需要在 Maven 项目中引入 OpenFeign 支持,则应在项目的 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 需要注意的是,OpenFeign 已经成为 Spring Cloud 的一部分,并且默认集成了与 Spring Boot 和 Spring MVC 的良好兼容性。因此无需再单独寻找 `spring-cloud-starter-feign` 这一旧版依赖[^4]。 此外,为了确保项目的正常运行,还需要确认所使用的 Spring Cloud 版本号是否匹配当前的 Spring Boot 版本。可以在 `pom.xml` 中定义 Spring Cloud BOM(Bill of Materials),以便管理所有相关依赖的版本一致性。例如: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 替换为适合您项目的版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 最后,当使用 Feign 或 OpenFeign 时,通常会结合 Eureka 或 Consul 等服务注册中心来实现动态的服务发现功能。这可以通过启用相应的注解完成,比如 `@EnableDiscoveryClient` 或者 `@EnableEurekaClient`。 ### 示例代码 以下是完整的 Maven 配置示例: ```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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <!-- 根据实际需求调整版本 --> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>11</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 使用合适的 Spring Cloud 版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> </dependencies> </project> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值