1.Ribbon 是: 负载均衡+RestTemplate,默认是轮询机制
2.引入新版eureka包中自带ribbon
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
或者单独引入,如下
<!-- https://mvnrepository.com/artifact/com.netflix.ribbon/ribbon -->
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
<version>2.7.18</version>
<scope>runtime</scope>
</dependency>
3.config类编写,与RestTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4.rule
ribbon7种负载机制
5.替换ribbon负载机制
例如替换为随机
1.见类,此类不能在@ComponentScan扫描的包下,如果在此扫描内,这个配置就会被所有的ribbon客户端共享,达不到特殊化定制。
@Configuration
public class MyRule {
@Bean
public IRule getRule(){
return new RandomRule(); //随机机制
}
}
类目录结构如下
2.主启动类加配置
@RibbonClient(name = "PAY-SERVICE",configuration = MyRule.class)