2.服务治理-Ribbon-配置负载均衡策略

2020-05-02  本文已影响0人  溅十三
image.png

1.Ribbon的默认负载均衡策略是?

RoundRobinRule :每次轮询节点

2.设置负载均衡策略为RandomRule

IRule 是所有负载均衡策略的顶级类
return new RandomRule();修改默认均衡策略
@Bean 代表的是什么?

package com.imooc.springcloud;

import com.imooc.springcloud.rules.MyRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by 半仙.
 */
@Configuration
public class RibbonConfiguration {

    @Bean
    public IRule defaultLBStrategy() {
        return new RandomRule();
    }

}

2.重启

为什么可以?原因是什么?

package com.imooc.springcloud;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonConsumerApplication {

//    精华所在
    @Bean
    @LoadBalanced
    public RestTemplate template() {
        return new RestTemplate();
    }

    @Bean
    public IRule defaultLBStrategy() {
        return new RandomRule();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(RibbonConsumerApplication.class)
                .web(WebApplicationType.SERVLET)
                .run(args);
    }
}

3.如何针对一个服务来指定一个负载均衡策略

优先级:针对一个服务来指定一个负载均衡策略>全局负载均衡策略

1.先注释掉之前配置的全局负载均衡策略
2.配置一条针对eureka-client的负载均衡策略
3.配置好重启即可
在application.properties配置文件中加入下面的属性,即可配置策略

application.properties配置说明:eureka-client代表的是什么?服务的Id,serviceName
com.netflix.loadbalancer.RoundRobinRule的路径怎么借鉴?

加入这行代码即可:eureka-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

spring.application.name=ribbon-consumer

server.port=31000

eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/

eureka-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule

1.怎么看一个配置类有什么属性?
2.注解的配置比配置文件的优先级更高
3.SpringBoot的.properties和yaml的加载顺序哪个更高?很多问题都和加载顺序有关系
在RibbonConfiguration加入注解@RibbonClient(name = "eureka-client",configuration = com.netflix.loadbalancer.RoundRobinRule.class)

package com.imooc.springcloud;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by 半仙.
 */
@Configuration
@RibbonClient(name = "eureka-client",configuration = com.netflix.loadbalancer.RoundRobinRule.class)
public class RibbonConfiguration {

//    @Bean
//    public IRule defaultLBStrategy() {
//        return new RandomRule();
//    }

}

4.如何针对一个方法来指定一个负载均衡策略?

fen组件里面

千里马常有,而伯乐不常有,需要自己走到聚光灯下

上一篇下一篇

猜你喜欢

热点阅读