9.2、OpenFeign项目搭建
分类: Spring Cloud OpenFeign
OpenFeign 项目搭建
搭建 OpenFeign 项目是使用 OpenFeign 的第一步。本节将学习 OpenFeign 项目搭建。
本节将学习:添加 OpenFeign 依赖、启动类注解、基础配置,以及项目结构。
添加 OpenFeign 依赖
Maven 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
Nacos Discovery 依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
LoadBalancer 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
完整依赖配置
<dependencies> <!-- OpenFeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Nacos Discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- LoadBalancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency> </dependencies>
启动类注解
@EnableFeignClients
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class OrderServiceApplication { public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } }
指定扫描包
@EnableFeignClients(basePackages = "com.example.feign") public class OrderServiceApplication { // ... }
指定客户端
@EnableFeignClients(clients = {UserServiceClient.class, ProductServiceClient.class}) public class OrderServiceApplication { // ... }
基础配置
配置文件
spring: application: name: order-service cloud: nacos: discovery: server-addr: localhost:8848 feign: client: config: default: connectTimeout: 5000 readTimeout: 10000 user-service: connectTimeout: 3000 readTimeout: 5000
日志配置
logging: level: com.example.feign: DEBUG
超时配置
feign: client: config: default: connectTimeout: 5000 readTimeout: 10000 compression: request: enabled: true response: enabled: true
项目结构
目录结构
src/main/java/com/example/orderservice/
├── OrderServiceApplication.java
├── controller/
│ └── OrderController.java
├── service/
│ └── OrderService.java
├── feign/
│ ├── UserServiceClient.java
│ └── ProductServiceClient.java
└── dto/
└── OrderDTO.java
Feign 客户端接口
@FeignClient(name = "user-service") public interface UserServiceClient { @GetMapping("/api/users/{id}") User getUser(@PathVariable Long id); }
使用 Feign 客户端
@Service public class OrderService { @Autowired private UserServiceClient userServiceClient; public Order createOrder(OrderDTO orderDTO) { User user = userServiceClient.getUser(orderDTO.getUserId()); // 创建订单逻辑 } }
验证配置
验证步骤
验证步骤:
- 检查依赖是否正确添加
- 验证启动类注解
- 检查配置文件
- 启动应用验证
测试代码
@RestController public class TestController { @Autowired private UserServiceClient userServiceClient; @GetMapping("/test") public User test() { return userServiceClient.getUser(1L); } }
官方资源
- Spring Cloud OpenFeign 官方文档:https://spring.io/projects/spring-cloud-openfeign
本节小结
在本节中,我们学习了:
第一个是添加 OpenFeign 依赖。 添加 OpenFeign 和相关依赖。
第二个是启动类注解。 使用 @EnableFeignClients 启用 OpenFeign。
第三个是基础配置。 配置 OpenFeign 的超时、日志等。
第四个是项目结构。 OpenFeign 项目的目录结构。
这就是 OpenFeign 项目搭建。完成搭建后,就可以定义和使用 Feign 客户端了。
在下一节,我们将学习 Feign 客户端定义。