Spring Boot Ribbon负载均衡配置的坑(避免被ComponentScan扫描)

Spring Boot Ribbon的配置需要注意避免被@ComponentScan扫描,以免导致所有RibbonClient共享配置,引起负载均衡问题。当配置被扫描后,可能导致访问服务时出现404错误,因为负载均衡器被错误地复用。解决办法是确保Ribbon配置类不被主应用上下文扫描,例如将其放在单独包下或明确指定扫描包路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置

Spring Boot Ribbon负载均衡配置很简单,网上一搜类似下面:

@Configuration
public class LoadBalanceConfiguration {

    @Bean
    public IRule rule() {
        return new WeightedResponseTimeRule();
    }
}

在Ribbon的文档中有这样一段话:

The FooConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication) you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan).

大体意思是对于Ribbon的配置必须用@Configuration注解标识,并且不能被@Component注解或者@SpringBootApplication(因为里面包含了@Compon

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值