Cpte-Boot/cpte-wms-service/FEIGN_STARTUP_FIX.md

189 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Feign 启动错误修复说明
## 问题描述
启动微服务时报错:
```
Field basicServiceClient in org.cpte.example.FeignServiceExample required a bean of type
'org.cpte.feign.client.BasicServiceClient' that could not be found.
```
## 原因分析
这个错误是因为 Spring 容器无法找到 Feign Client 的 Bean。虽然我们创建了 Feign Client 接口,但是**没有在启动类上添加 `@EnableFeignClients` 注解**来启用 Feign 客户端扫描。
## 解决方案
在所有微服务的启动类上添加 `@EnableFeignClients` 注解:
### 1. 入库服务 (WmsInboundApplication)
```java
@SpringBootApplication(scanBasePackages = {"org.jeecg", "org.cpte", "org.cpte.modules"})
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@ImportAutoConfiguration(JustAuthAutoConfiguration.class)
@EnableFeignClients(basePackages = {"org.cpte.feign.client"}) // 新增这行
public class WmsInboundApplication extends SpringBootServletInitializer {
// ...
}
```
### 2. 基础服务 (WmsBasicApplication)
```java
@SpringBootApplication(scanBasePackages = {"org.jeecg","org.cpte","org.cpte.modules"})
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@ImportAutoConfiguration(JustAuthAutoConfiguration.class)
@EnableFeignClients(basePackages = {"org.cpte.feign.client"}) // 新增这行
public class WmsBasicApplication extends SpringBootServletInitializer {
// ...
}
```
### 3. 出库服务 (WmsOutboundApplication)
```java
@SpringBootApplication(scanBasePackages = {"org.jeecg", "org.cpte", "org.cpte.modules"})
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@ImportAutoConfiguration(JustAuthAutoConfiguration.class)
@EnableFeignClients(basePackages = {"org.cpte.feign.client"}) // 新增这行
public class WmsOutboundApplication extends SpringBootServletInitializer {
// ...
}
```
### 4. 库存服务 (WmsInventoryApplication)
```java
@SpringBootApplication(scanBasePackages = {"org.jeecg", "org.cpte", "org.cpte.modules"})
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@ImportAutoConfiguration(JustAuthAutoConfiguration.class)
@EnableFeignClients(basePackages = {"org.cpte.feign.client"}) // 新增这行
public class WmsInventoryApplication extends SpringBootServletInitializer {
// ...
}
```
### 5. 调度服务 (WmsScheduleApplication)
```java
@SpringBootApplication(scanBasePackages = {"org.jeecg", "org.cpte", "org.cpte.modules"})
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class})
@ImportAutoConfiguration(JustAuthAutoConfiguration.class)
@EnableFeignClients(basePackages = {"org.cpte.feign.client"}) // 新增这行
public class WmsScheduleApplication extends SpringBootServletInitializer {
// ...
}
```
## @EnableFeignClients 说明
### 作用
`@EnableFeignClients` 是 Spring Cloud OpenFeign 提供的注解,用于启用 Feign 客户端扫描。它会自动扫描指定包下的所有 Feign Client 接口,并为它们创建代理 Bean。
### 常用配置
```java
@EnableFeignClients(
basePackages = {"org.cpte.feign.client"}, // 指定 Feign Client 所在的包
defaultConfiguration = FeignClientConfiguration.class, // 默认配置类(可选)
clients = { // 指定具体的 Feign Client可选不指定则扫描所有
BasicServiceClient.class,
InventoryServiceClient.class,
ScheduleServiceClient.class
}
)
```
### 配置位置
- **启动类上**:全局启用 Feign推荐
- **配置类上**:局部启用 Feign
## 验证方法
启动服务后,查看日志确认 Feign Client 已成功注册:
```bash
# 查看启动日志
kubectl logs deployment/wms-inbound-service -n wms-system | grep -i feign
# 或者本地启动查看控制台输出
```
如果看到类似以下日志,说明 Feign Client 已成功注册:
```
INFO: Created Feign client for interface: org.cpte.feign.client.BasicServiceClient
INFO: Created Feign client for interface: org.cpte.feign.client.InventoryServiceClient
INFO: Created Feign client for interface: org.cpte.feign.client.ScheduleServiceClient
```
## 注意事项
### 1. 包扫描路径
确保 `basePackages` 指定的包路径包含所有的 Feign Client 接口:
- `org.cpte.feign.client.BasicServiceClient`
- `org.cpte.feign.client.InventoryServiceClient`
- `org.cpte.feign.client.ScheduleServiceClient`
### 2. 依赖配置
确保 `pom.xml` 中已添加 OpenFeign 依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.1.3</version>
</dependency>
```
### 3. 配置类
建议创建 Feign 配置类来统一管理 Feign 的配置:
```java
@Configuration
public class FeignClientConfiguration {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.BASIC;
}
@Bean
public Request.Options options() {
return new Request.Options(5000, TimeUnit.MILLISECONDS,
10000, TimeUnit.MILLISECONDS,
true);
}
}
```
### 4. Fallback 降级
为每个 Feign Client 配置 FallbackFactory提高系统可用性
```java
@FeignClient(
name = "wms-basic-service",
url = "${feign.client.wms-basic.url:http://wms-basic-service:80}",
fallbackFactory = BasicServiceFallbackFactory.class
)
public interface BasicServiceClient {
// ...
}
```
## 相关文件
- 入库服务启动类:`cpte-wms-service/cpte-wms-inbound-service/src/main/java/org/cpte/WmsInboundApplication.java`
- 基础服务启动类:`cpte-wms-service/cpte-wms-basic-service/src/main/java/org/cpte/WmsBasicApplication.java`
- 出库服务启动类:`cpte-wms-service/cpte-wms-outbound-service/src/main/java/org/cpte/WmsOutboundApplication.java`
- 库存服务启动类:`cpte-wms-service/cpte-wms-inventory-service/src/main/java/org/cpte/WmsInventoryApplication.java`
- 调度服务启动类:`cpte-wms-service/cpte-wms-schedule-service/src/main/java/org/cpte/WmsScheduleApplication.java`
## 参考文档
- [Feign 使用指南](cpte-wms-service/FEIGN_USAGE.md)
- [K8s 部署文档](K8S_DEPLOYMENT.md)
- [架构总览](ARCHITECTURE_OVERVIEW.md)