第一步
在客户端主启动类上添加@RibbonClient注解;
name属性:是客户端要访问的微服务名字
configuration属性:是自定义负载均衡算法类。
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "MICROSERVICECLOUD-DEPT",configuration = MyIRule.class)//在MICROSERVICECLOUD-DEPT服务上使用MyIRule自定义负载均衡算法
public class DeptConsumer80_App {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer80_App.class, args);
}
}
注意: 自定义的算法类不能放在主启动类的包及其子包下。
因为主启动类使用了@SpringBootApplication注解,而这个注解又默认添加了@ComponentScan注解【只要使用了@SpringBootApplication注解就使用了@ComponentScan注解】。下面是@SpringBootApplication的@ComponentScan注解源码:
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
可以看到,这个注解会把添加了注