RestConfig.java 761 字节
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springcloud.config;

Q
qinyingjie 已提交
3
import com.netflix.loadbalancer.IRule;
Q
qinyingjie 已提交
4
import com.netflix.loadbalancer.ZoneAvoidanceRule;
Q
qinyingjie 已提交
5
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
Q
qinyingjie 已提交
6 7 8 9 10 11 12
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

Q
qinyingjie 已提交
13
    @LoadBalanced
Q
qinyingjie 已提交
14 15 16 17
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
Q
qinyingjie 已提交
18 19 20 21 22 23

    /**
     * 自定义负载均衡
     */
    @Bean
    public IRule microIrule() {
Q
qinyingjie 已提交
24 25 26
        //return new RandomRule(); //轮训
        //return new BestAvailableRule();
        return new ZoneAvoidanceRule(); //新版本默认的策略
Q
qinyingjie 已提交
27 28
    }
}