6.1 KiB
6.1 KiB
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)
@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)
@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)
@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)
@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)
@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。
常用配置
@EnableFeignClients(
basePackages = {"org.cpte.feign.client"}, // 指定 Feign Client 所在的包
defaultConfiguration = FeignClientConfiguration.class, // 默认配置类(可选)
clients = { // 指定具体的 Feign Client(可选,不指定则扫描所有)
BasicServiceClient.class,
InventoryServiceClient.class,
ScheduleServiceClient.class
}
)
配置位置
- 启动类上:全局启用 Feign(推荐)
- 配置类上:局部启用 Feign
验证方法
启动服务后,查看日志确认 Feign Client 已成功注册:
# 查看启动日志
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.BasicServiceClientorg.cpte.feign.client.InventoryServiceClientorg.cpte.feign.client.ScheduleServiceClient
2. 依赖配置
确保 pom.xml 中已添加 OpenFeign 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>4.1.3</version>
</dependency>
3. 配置类
建议创建 Feign 配置类来统一管理 Feign 的配置:
@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,提高系统可用性:
@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