一、OpenFeign介绍
1. 官网
Spring Cloud OpenFeign - Spring官网
2. 特性:声明式REST客户端
声明式REST客户端
:Feign创建了一个用JAX-RS或SpringMVC注释装饰的接口的动态实现
二、OpenFeign入门示例
本示例,基于
Spring Boot 3.3.3
1. 添加依赖
在 Maven 项目中,需要在 pom.xml
文件中添加 OpenFeign 的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.1.3</version>
</dependency>
截止 2024-08-24
,OpenFeign 最新版本为:4.1.3
。
在 SpringBoot3
中,OpenFeign 必须指明依赖的版本号
,否则会报错。具体原因请参考后面章节《三、OpenFeign 指明依赖版本》
2. 启用 OpenFeign
在 Spring Boot 应用程序中启用 OpenFeign,可以通过在启动类上添加 @EnableFeignClients
注解来实现:
package com.example.hello_feign_client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class HelloFeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(HelloFeignClientApplication.class, args);
}
}
3. 定义 Feign Client 接口
定义一个接口来代表客户端,并使用 @FeignClient
注解来配置它。例如,假设有一个名为 hello-world
的微服务,你可以这样定义 Feign Client:
package com.example.hello_feign_client.feign;
import com.example.hello_common.model.vo.UserVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "hello-world", url = "http://localhost:8080", path = "/users")
public interface UserFeignClient {
@GetMapping("/{id}")
UserVo getUserById(@PathVariable String id);
}
在这个例子中,@FeignClient
注解中的 name
属性指定了要调用的 服务名
。在微服务架构中,通过注册中心(Eureka、Nacos等)和服务名,调用远程服务。
上例中使用 url
属性(绝对值或仅主机名)指定 URL,此时不再通过服务名来调用远程服务,而是直接访问 url 对应的地址。
UserClient
接口中定义的方法用于发起 HTTP GET 请求到 /users/{id}
路径,并且通过 @PathVariable
注解将方法参数绑定到 URL 中的 {id}
。
4. 使用 Feign Client
在你的应用程序中,你可以通过注入 Feign Client 接口来使用它。例如:
package com.example.hello_feign_client.web.user.controller;
import com.example.hello_common.model.vo.UserVo;
import com.example.hello_feign_client.feign.UserFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
private final UserFeignClient userFeignClient;
@GetMapping("/{id}")
public UserVo getUserById(@PathVariable String id) {
return userFeignClient.getUserById(id);
}
}
在这个示例中,UserController
控制器通过构造函数注入了 UserFeignClient
,并使用它来调用远程服务。
5. 配置选项
通过属性文件来配置 OpenFeign 的一些行为,例如日志级别、超时时间等。
以上就是使用 OpenFeign 的基本步骤。随着项目的深入,你还可以探索更多的高级功能,如服务降级、错误处理、请求拦截器等。
三、OpenFeign指明依赖版本
1. 原因
SpringBoot3
中,已经不再管理 OpenFeign 的依赖版本了,OpenFeign 必须指明依赖的版本号
,否则会报错。
2. 报错信息示例
3. 补充说明
spring-boot-dependencies
是 Spring Boot 提供的一种依赖管理机制,它主要用于简化 Maven 或 Gradle 项目中的依赖版本配置。通过这种方式引入依赖,可以避免在项目中显式声明各个依赖库的版本号,从而减少版本冲突的风险。
Spring Boot 3.3.3
,spring-boot-dependencies
已经不再包含 OpenFeign
了。