springCloud之nacos之负载均衡Ribbon

1负载均衡的两种方式

1.服务器端负载均衡(请求通过nginx,再分发到不同的服务器)
2.客户端侧负载均衡(比如订单中心拿到请求(要到用户中心),再通过负载均衡分发到不同的用户中心实例中)

2手写负载均衡器

用到的bean对象DiscoveryClient 服务发现

  @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/order/create")
    public String createOrder(Integer productId,Integer userId){
      // String result = restTemplate.getForObject("http://msb-stock/stock/reduce/"+ productId,String.class);
       // log.info("进行减库存:{}",result);
        //根据服务名称查询服务列表ServiceInstance实例对象
        List<ServiceInstance> serviceInstanceList= discoveryClient.getInstances("msb-stock");
         List<String> toUrls =  serviceInstanceList.stream().map(a->a.getUri().toString()+"/stock/reduce/").collect(Collectors.toList());
        int i = ThreadLocalRandom.current().nextInt(toUrls.size());
        String targetUrl = toUrls.get(i);
        System.err.println(i);
    String result = restTemplate.getForObject(targetUrl+productId,String.class);
        return "下单成功";
    }

3Ribbon实现负载均衡

集成nacos,因为nacos集成了Ribbon,如果其他没有集成nacos的注册中心,比如Eureka需要引入Ribbon

//@LoadBalanced 就可以使用了
	 @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
4Ribbon重要接口
接口作用默认值
IClientConfig读取配置DefaultclientConfigImpl
IRule负载均衡规则,选择实例ZoneAvoidanceRule
IPing筛选掉ping不通的实例默认采用DummyPing实现,该检查策略是一个特殊的实现,<br />实际上它并不会检查实例是否可用,而是始终返回true,默认认为所<br />有服务实例都是可用的.
ServerList<Server>交给Ribbon的实例列表Ribbon: ConfigurationBasedServerList<br /> Spring Cloud Alibaba: NacosServerList
ServerListFilter过滤掉不符合条件的实例ZonePreferenceServerListFilter
ILoadBalancerRibbon的入口ZoneAwareLoadBalancer
ServerListUpdater更新交给Ribbon的List的策略PollingServerListUpdater
5Ribbon负载均衡规则
规则名称特点
RandomRule随机选择一个Server
NacosRule同集群优先调用
RetryRule对选定的负责均衡策略机上充值机制,在一个配置时间段内当选择Server不成功,则一直尝试使用subRule的方式选择一个可用的Server
RoundRobinRule轮询选择,轮询index,选择index对应位置Server
WeightedResponseTimeRule根据相应时间加权,相应时间越长,权重越小,被选中的可能性越低
ZoneAvoidanceRule(默认是这个)该策略能够在多区域环境下选出最佳区域的实例进行访问。在没有Zone的环境下,类似于轮询(RoundRobinRule)
Ribbon的细粒度配置

当我们需要通过随机负载均衡条用用户中心,但是需要通过轮询的负载均衡策略调用账户中心,这个时候就用到了Ribbon的细粒度配置,满足不同的轮询需求
1.通过java类的配置
2.通过配置属性配置
3全局配置
java类实现优先属性配置

1.java类实现

在这里插入图片描述

1.新建一个懒加载的 IRule Bean

//不被spring管理
public class RibbonConfiguration {

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

2.新建一个一个配置类

@Configuration //被spring管理
@RibbonClient(name = "msb-stock",configuration = RibbonConfiguration.class)//新的负载均衡策略
public class StockRibbonConfiguration {

}

再次重启调用,则msb-stock 实例都是新的负载均衡策略

2配置属性的方式

注释掉刚刚的java类配置,在配置文件中添加以下属性
在这里插入图片描述

#单词需要写对,不然会有错误,NFLoadBalancerRuleClassName 
msb-stock:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
3.全局配置

在这里插入图片描述

Ribbon饿加载

正常项目启动后,Ribbon不会立刻去注册中心加载服务,当我们第一次请求的时候,才会去加载服务,这样第一次请求的时间都会偏长。

},Server stats: [[Server:192.168.0.104:11001;	Zone:UNKNOWN;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
, [Server:192.168.0.104:11002;	Zone:UNKNOWN;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
, [Server:192.168.0.104:11003;	Zone:UNKNOWN;	Total Requests:0;	Successive connection failure:0;	Total blackout seconds:0;	Last connection made:Thu Jan 01 08:00:00 CST 1970;	First connection made: Thu Jan 01 08:00:00 CST 1970;	Active Connections:0;	total failure count in last (1000) msecs:0;	average resp time:0.0;	90 percentile resp time:0.0;	95 percentile resp time:0.0;	min resp time:0.0;	max resp time:0.0;	stddev resp time:0.0]
]}ServerList:com.alibaba.cloud.nacos.ribbon.NacosServerList@218f0c

在配置文件中添加属性,可以在项目启动的时候加载重要信息

ribbon:
  eager-load:
    enabled: true
    clients: msb-stock
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值