配置
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(因为里面包含了@Component)扫描到。因为如果被@ComponetScan扫描到

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

被折叠的 条评论
为什么被折叠?



