SpringCloud---SpringCloud Ribbon(二)

SpringCloud Ribbon默认使用的是轮询算法(RoundRobinRule),Ribbon负载均衡的算法都实现了IRule接口。IRule是Ribbon的核心组件。

使用Ribbon的负载均衡算法:在module配置类中添加想要使用的负载均衡算法对象的Bean实例。

@Configuration
public class RibbonConfig {
	
	@Bean
	@LoadBalanced   //Ribbon负载均衡
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}
	
	@Bean
	public IRule myRule() {
		return new RandomRule();	//RandomRule随机算法,若要其他算法,粘贴算法类名即可。
	}
}
发布了71 篇原创文章 · 获赞 31 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39115469/article/details/104679203