Spring Cloud (3)B Ribbon 负载均衡 IRule

时间:2023-03-09 13:22:51
Spring Cloud (3)B  Ribbon 负载均衡  IRule
package com.service.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate; @Configuration
public class Dept_Config { @Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
} @Bean
public IRule myRule(){ // RoundRobinRule 轮询
// RandomRule 随机
return new RetryRule();
}
} 自定义Ribbon
1.加载注解

Spring Cloud (3)B  Ribbon 负载均衡  IRule

2.配置mySelfRule.class

Spring Cloud (3)B  Ribbon 负载均衡  IRule

Spring Cloud (3)B  Ribbon 负载均衡  IRule

3.自定义时要实现

Spring Cloud (3)B  Ribbon 负载均衡  IRule