ribbon学习
spring cloud 中的负载均衡有ribbon和feign
引入ribbon依赖
<!--ribbon相关--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
在客户端加入loadblance
@Bean @LoadBalanced //ribbon负载均衡, public RestTemplate getRestaTemplate(){ return new RestTemplate(); }
ribbon的负载均衡算法有七种,默认轮询,要更换:
@Bean @LoadBalanced //ribbon负载均衡, public RestTemplate getRestaTemplate(){ return new RestTemplate(); } //更换算法
@Bean public IRule defRule(){ return new RandomRule(); }
自定义Rule,不能建在与主类及子包下面:
@SpringBootApplication @EnableEurekaClient @RibbonClient(name = "MICROSERVICECLOUD",configuration = "自定义的rule") public class Consumer80 { public static void main(String[] args) { SpringApplication.run(Consumer80.class,args); } }
...