SpringCloud:使用load balancer替代ribbon作为客户端负载均衡器

1.在配置文件里进行配置:

spring.cloud.loadbalancer.ribbon.enabled=false

2.在pom.xml中增加load balancer依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

3.配置RestTemplate

package cn.edu.tju.config;

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 RestConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();

    }
}

你可能感兴趣的:(SpringCloud,spring,cloud,ribbon,负载均衡)