惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
I
InfoQ
H
Hacker News: Front Page
D
Docker
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
博客园 - 【当耐特】
T
Tor Project blog
U
Unit 42
H
Heimdal Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy & Cybersecurity Law Blog
PCI Perspectives
PCI Perspectives
美团技术团队
O
OpenAI News
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MyScale Blog
MyScale Blog

博客园 - Doyourself!

rocketmq 启动后 在mq console界面的consumer的Quantity数量显示为0 问题记录 python 切换版本后 提示 无法在python 3.11(.venv)(D:/my_rag_bot/.venv/Scripts/python.exe)设置 python sdk,该sdk似乎无效 记录一次日志告警随着nacos文件动态刷新而失效的问题 多个WebMvcConfigurer配置Jackson2ObjectMapperBuilder不生效问题记录 自定义拦截器不生效问题记录 记录一次nginx能通但是请求一直不了的问题 idea远程连接并本地打包到远程服务器 记一次生产环境内存溢出记录 凤凰架构总结 sentinel接入记录 JVM虚拟机总结 记录一次首页优化的经历 使用sharding-jdbc做分库分表记录 使用druid自定义拦截器 记录一次 maven 子模块相互依赖导致的父模块无法动态升级的问题 'parent.relativePath' points at wrong local POM 雪花算法snowflakeIdWorker使用记录 全局调用链路traceId网关到业务层、feign调用统一问题记录 打印mq异常消息记录 根据druid将慢sql通过钉钉的方式进行告警功能记录
Spring Cloud 的ribbon的饥饿加载机制
Doyourself! · 2023-08-25 · via 博客园 - Doyourself!

 我们在使用Spring Cloud的Ribbon或Feign来实现服务调用的时候,如果我们的机器或网络环境等原因不是很好的话,有时候会发现这样一个问题:我们服务消费方调用服务提供方接口的时候,第一次请求经常会超时,而之后的调用就没有问题了。下面我们就来说说造成这个问题的原因,以及如何解决的方法。

问题原因

造成第一次服务调用出现失败的原因主要是Ribbon进行客户端负载均衡的Client并不是在服务启动的时候就初始化好的,而是在调用的时候才会去创建相应的Client,所以第一次调用的耗时不仅仅包含发送HTTP请求的时间,还包含了创建RibbonClient的时间,这样一来如果创建时间速度较慢,同时设置的超时时间又比较短的话,很容易就会出现上面所描述的显现。

从日志中我们也能知道这一点细节,在第一次发起调用的时候我们可以从日志中看到如下信息:


2017-09-25 08:29:54,201 INFO  [main] com.netflix.loadbalancer.DynamicServerListLoadBalancer - DynamicServerListLoadBalancer for client hello-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=hello-service,current list of Servers=[192.168.99.176:9901],Load balancer stats=Zone stats: {unknown=[Zone:unknown;    Instance count:1;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;] 
},Server stats: [[Server:192.168.99.176:9901;   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:ConsulServerList{serviceId='hello-service', tag=null} 

而Feign的实现基于Ribbon,所以它也有一样的问题,下面就来看看如何解决这个问题。

解决方法

解决的方法很简单,既然第一次调用时候产生RibbonClient耗时,那么就让它提前创建,而不是在第一次调用的时候创建。

在Spring Cloud的Dlaston版本中提供了几个新的参数,它们可以很方便的帮我们实现这样的功能。

ribbon:
	eager-load:
    	enabled:true
	clients:kong-auth,kong-uc-service

参数说明:

  • ribbon.eager-load.enabled:开启Ribbon的饥饿加载模式
  • ribbon.eager-load.clients:指定需要饥饿加载的客户端名称、服务名

        通过上面的配置完成之后,我们尝试重启一下服务消费者,这个时候我们会发现,我们没有开始调用服务接口,但是上面初始化负载均衡的日志就已经打印出来了。这就说明我们对ribbon的饥饿加载模块设置已经生效了。

要解决Ribbon的饥饿加载问题,您可以通过以下方式进行配置:

1. 在Ribbon客户端的配置文件中,添加以下配置项:

或者在application.properties文件中添加以下配置项:


2. 在Spring Cloud的配置文件中,添加以下配置项:

或者在application.properties文件中添加以下配置项:

通过设置eager-load.enabled为true,Ribbon将在应用启动时立即加载所有服务实例的信息,而不是按需加载。这样可以避免在第一次请求时出现延迟,从而解决饥饿加载的问题。

请注意,以上配置适用于使用Spring Cloud Netflix Ribbon作为负载均衡器的情况。如果您使用的是Spring Cloud LoadBalancer作为负载均衡器,可以参考LoadBalancer的文档来解决饥饿加载问题。

另外,饥饿加载可能会增加应用启动时间和内存消耗,因此在配置饥饿加载时需要权衡考虑。 

 

参考:https://blog.csdn.net/qq_28089993/article/details/80600032