配置
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