Spring Boot3.0.0+Spring Cloud2021.0.5 Feign负载均衡缺失问题求助
问题:Feign客户端负载均衡缺失导致启动错误
使用环境:Spring Boot 3.0.0 + Spring Cloud 2021.0.5
现有BillingServiceApplication.java代码:
package org.sid.billingservice; import org.sid.billingservice.entities.Bill; import org.sid.billingservice.entities.ProductItem; import org.sid.billingservice.model.Customer; import org.sid.billingservice.model.Product; import org.sid.billingservice.repository.BillRepository; import org.sid.billingservice.repository.ProductItemRepository; import org.sid.billingservice.services.CustomerRestClient; import org.sid.billingservice.services.ProductRestClient; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.FeignAutoConfiguration; import org.springframework.context.annotation.Bean; import java.util.Collection; import java.util.Date; import java.util.Random; @SpringBootApplication @EnableFeignClients @ImportAutoConfiguration({FeignAutoConfiguration.class}) public class BillingServiceApplication { public static void main(String[] args) { SpringApplication.run(BillingServiceApplication.class, args); } @Bean CommandLineRunner start(BillRepository billRepository, ProductItemRepository productItemRepository, CustomerRestClient customerRestClient, ProductRestClient productRestClient) { return args -> { Collection<Product> products = productRestClient.allProducts().getContent(); Long customerId = 1L; Customer customer = customerRestClient.findCustomerById(customerId); if (customer == null) throw new RuntimeException("Customer not found"); Bill bill = new Bill(); bill.setBillDate(new Date()); bill.setCustomerId(customerId); Bill savedBill = billRepository.save(bill); products.forEach(product -> { ProductItem productItem = new ProductItem(); productItem.setBill(savedBill); productItem.setProductId(product.getId()); productItem.setQuantity(1 + new Random().nextInt(10)); productItem.setPrice(product.getPrice()); productItem.setDiscount(Math.random()); productItemRepository.save(productItem); }); }; } }
运行时抛出错误:
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
完整错误日志包含该错误及相关调用栈信息。
解决方案
1. 添加Spring Cloud LoadBalancer依赖
Spring Cloud 2021.0.x版本开始,Feign不再默认集成Ribbon,需要手动引入Spring Cloud LoadBalancer作为负载均衡组件。
- Maven(pom.xml):
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
- Gradle(build.gradle):
implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer'
2. 移除冗余配置
删除类上的@ImportAutoConfiguration({FeignAutoConfiguration.class})注解,因为@EnableFeignClients已经自动完成Feign相关配置的导入,手动导入会导致配置冲突。
修改后的类注解部分:
@SpringBootApplication @EnableFeignClients public class BillingServiceApplication { // ... 原有代码 }
3. 确认Feign客户端接口配置
确保你的CustomerRestClient和ProductRestClient接口正确使用@FeignClient注解指定服务名称,示例:
@FeignClient(name = "customer-service") // 对应注册中心的客户服务名称 public interface CustomerRestClient { @GetMapping("/customers/{id}") Customer findCustomerById(@PathVariable Long id); }
@FeignClient(name = "product-service") // 对应注册中心的产品服务名称 public interface ProductRestClient { @GetMapping("/products") Page<Product> allProducts(); }
完成以上步骤后,重新启动服务即可解决该错误。
内容的提问来源于stack exchange,提问作者Đỗ Như Vỹ
相关产品推荐
相关产品推荐

