Ribbon饥饿加载

默认情况下Ribbon是懒加载的。当服务启动好之后,第一次请求是非常慢的,第二次之后就快很多。

解决方式:开启饥饿加载

ribbon:
eager-load:
enabled: true #开启饥饿加载
clients: server-1,server-2,server-3 #为哪些服务的名称开启饥饿加载,多个用逗号分隔

Ribbon负载均衡的八种算法,其中ResponseTimeWeightedRule已废除

规则名称特点
AvailabilityFilteringRule 过滤掉一直连接失败的被标记为circuit tripped(电路跳闸)的后端Service,并过滤掉那些高并发的后端Server或者使用一个AvailabilityPredicate来包含过滤Server的逻辑,其实就是检查status的记录的各个Server的运行状态
BestAvailableRule 选择一个最小的并发请求的Server,逐个考察Server,如果Server被tripped了,则跳过
RandomRule 随机选择一个Server
ResponseTimeWeightedRule 已废弃,作用同WeightedResponseTimeRule
RetryRule 对选定的负责均衡策略机上充值机制,在一个配置时间段内当选择Server不成功,则一直尝试使用subRule的方式选择一个可用的Server
RoundRobinRule 轮询选择,轮询index,选择index对应位置Server
WeightedResponseTimeRule 根据相应时间加权,相应时间越长,权重越小,被选中的可能性越低
ZoneAvoidanceRule (默认是这个)负责判断Server所Zone的性能和Server的可用性选择Server,在没有Zone的环境下,类似于轮询(RoundRobinRule

 

java文件配置Ribbon负载均衡规则:

package com.springcloud.demo.client.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: yzp
 * @Date: 2020-6-5 9:33
 * @description
 */
@Configuration
public class RibbonConfig {

    /**
     * @return 负载均衡规则
     */
    @Bean
    public IRule iRule() {
        return new RandomRule();
    }

}

配置文件配置Ribbon负载均衡规则:

# 服务名称 Service-ID
demo-user:
ribbon:
# 属性配置方式【推荐】配置文件配置负载均衡算法-这里使用的是自定义的Ribbon的负载均衡算法,默认
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

 

配置属性方式

<clientName>:
ribbon:
NFLoadBalancerClassName: #ILoadBalancer该接口实现类
NFLoadBalancerRuleClassName: #IRule该接口实现类
NFLoadBalancerPingClassName: #Iping该接口实现类
NIWSServerListClassName: #ServerList该接口实现类
NIWSServerListFilterClassName: #ServiceListFilter该接口实现类

在这些属性中定义的类优先于使用@RibbonClient(configuration=RibbonConfig.class)Spring 定义的bean 以及由Spring Cloud Netflix提供的默认值。描述:配置文件中定义ribbon优先代码定义

 

配置方式优点缺点
代码配置 基于代码,更加灵活 有坑(父子上下文)
线上修改得重新打包,发布
属性配置 易上手 配置更加直观 
线上修改无需重新打包,发布 
优先级更高
极端场景下没有配置配置方式灵活

 

实现负载均衡<全局>配置-随机

方式一:Ribbon的配置类定义在主类下

让ComponentScan上下文重叠(强烈不建议使用

方式二:

@Configuration
@RibbonClients(defaultConfiguration = RibbonConfig.class)//Ribbon负载均衡全局粒度配置(所有服务都按照这个配置)
public class RibbonConfig {
}

 

Ribbon核心功能之IPing

1、IPing是Ribbon保证服务可用的基石

2、IPing常见实现:NIWSDiscoveryPing、PingUrl

 

 

IPing算法算法描述
NIWSDiscoveryPing 不执行Ping操作,根据Eureka Clien反馈判断存活
PingUrl 使用HttpClient操作对服务进行Ping操作

DummyPing “人性本善”流算法,默认返回true
NoOpPing 永远返回true

 

Ribbon参数配置:

# Max number of retries on the same server (excluding the first try)
ribbon.MaxAutoRetries=1

# Max number of next servers to retry (excluding the first server)
ribbon.MaxAutoRetriesNextServer=1

# Whether all operations can be retried for this client
ribbon.OkToRetryOnAllOperations=true

# Interval to refresh the server list from the source
ribbon.ServerListRefreshInterval=2000

# Connect timeout used by Apache HttpClient
ribbon.ConnectTimeout=3000

# Read timeout used by Apache HttpClient
ribbon.ReadTimeout=3000

# Initial list of servers, can be changed via Archaius dynamic property at runtime
ribbon.listOfServers=www.baidu.com:80,www.jd.com:80

Ribbon参数可用在DefaultClientConfigImpl、CommonClientConfigKey文件查看。

 

posted on 2020-06-05 10:01  袁子弹  阅读(667)  评论(0编辑  收藏  举报