当前位置:网站首页>Monitor the cache update of Eureka client
Monitor the cache update of Eureka client
2022-06-23 13:38:00 【51CTO】
Welcome to visit mine GitHub
Here we classify and summarize all the original works of Xinchen ( Including supporting source code ): https://github.com/zq2599/blog_demos
from Eureka server Get a list of services
Spring cloud Application in environment , If you register to Eureka server, It will start from Eureka server Get the registration information of all apps ( Also called service list ), And save it locally , This operation is periodic , Once every 30 seconds by default ;
Here's the official structure , You can see Application Service towards Eureka Server Yes Get Registry Request :

Reference article
- If you are interested , Want to know more about spring Broadcast mechanism perhaps Eureka client Update service list , I recommend you to refer to the following two articles :
- 《spring4.1.8 The third expansion of actual combat : Broadcasting and monitoring 》
- 《Spring Cloud Source code analysis Eureka Chapter five : Update service list 》
Actual combat content
- This article is a practical article , The actual combat content is as follows :
- start-up Eureka server;
- Develop an application springcloudcustomizelistener, After startup, it will be registered to Eureka server;
- At this time, the application identity is Eureka client, Will periodically from Eureka server Get a list of services ( There is logic );
- Each time the successfully obtained service list is successfully stored in the local cache ( There is logic );
- After being stored in the cache , Will be in spring Send broadcast in container ( There is logic );
- The focus of this actual battle is to customize a listener to receive the above broadcast , After receiving the broadcast, print out the details in the log ;
- Start another application springclouddeepprovider, Will also register with Eureka server;
- And then observe springcloudcustomizelistener Broadcast monitoring log of , Will find springclouddeepprovider Registration information ;
Eureka client Introduction to the source code of cache service list
Before actual combat , First pair Eureka client The implementation source code of cache service list is introduced , In this way, a matching listener can be made ;
Application as Eureka Client Start time of , stay com.netflix.discovery.DiscoveryClient Class initScheduledTasks In the method , Will start a recurring task , every other 30 Seconds from Eureka server Get service list information , Here's the picture , The red box TimedSupervisorTask Responsible for periodic execution , In the green box CacheRefreshThread Responsible for specific update logic :

- stay CacheRefreshThread Class through layers of calls , The logic to get the service list and update the local cache is in fetchRegistry Method implementation , Here's the picture , The red box getAndStoreFullRegistry Be responsible for updating the whole method , In the green box getAndUpdateDelta Method is responsible for incremental updates , In the Yellow frame onCacheRefreshed Method is the focus of today : Send a broadcast , The broadcast type is The local cache of the service list has been updated

- onCacheRefreshed Method in subclass CloudEurekaClient It's rewritten in , It can be seen that a normal spring In vessel broadcast , The type is HeartbeatEvent, We can customize the listening class to receive broadcasts , And only... Is accepted by generic specification HeartbeatEvent type :
Actual application settings
- This actual battle will build a small Spring Cloud Environmental Science , The following applications are included :
| apply name | identity | effect | port | Startup sequence |
|---|---|---|---|---|
| springclouddeepeureka | Eureka server | Registry Center | 8081 | First of all |
| springcloudcustomizelistener | Eureka client | There is a custom broadcast listener , Listen to the broadcast of service list cache updates | 8085 | second |
| springclouddeepprovider | Eureka client | Ordinary web application | 8082 | Third |
Source download
- springclouddeepeureka and springclouddeepprovider These two applications , In the article 《Spring Cloud Source code analysis Eureka Chapter one : preparation 》 This is described in detail in , I won't say much in this article , You can refer to the article , It can also be in github Download the source code of these two applications , The address and link information is shown in the following table :
| name | link | remarks |
|---|---|---|
| Project home page | https://github.com/zq2599/blog_demos | The project is in progress. GitHub Home page on |
| git Warehouse address (https) | https://github.com/zq2599/blog_demos.git | The warehouse address of the source code of the project ,https agreement |
| git Warehouse address (ssh) | [email protected]:zq2599/blog_demos.git | The warehouse address of the source code of the project ,ssh agreement |
- This git Multiple folders in project , The source code of this chapter is in springclouddeepeureka、springclouddeepprovider Under these two folders , As shown in the red box below :

start-up springclouddeepeureka
- application springclouddeepeureka Start immediately after development , Access the address in the browser : http://localhost:8081, so Eureka server Has been launched , But no apps have been registered yet , Here's the picture :

Development springcloudcustomizelistener
- Next, we will develop applications together springcloudcustomizelistener, Add custom... In this app spring Radio monitor , If you don't want to type the code , You can also get it from github Download the source code directly from , The address and link information is shown in the following table :
| name | link | remarks |
|---|---|---|
| Project home page | https://github.com/zq2599/blog_demos | The project is in progress. GitHub Home page on |
| git Warehouse address (https) | https://github.com/zq2599/blog_demos.git | The warehouse address of the source code of the project ,https agreement |
| git Warehouse address (ssh) | [email protected]:zq2599/blog_demos.git | The warehouse address of the source code of the project ,ssh agreement |
- This git Multiple folders in project , The source code of this chapter is in springcloudcustomizelistener Under the folder , As shown in the red box below :

Let's develop together :
Create a springboot Of web application ,pom.xml The contents are as follows , Note that in order to show more details in the log , Rely on fastjson library :
- Application profile application.yml Is as follows :
- Create a listener EurekaCacheRefreshListener.java, I've analyzed HeartbeatEvent Two member variables of the instance , The values are the number of cache flushes and CloudEurekaClient example , On receiving the broadcast , Print both member variables in the log :
- Start the application , Wait for about 30 About seconds ,EurekaCacheRefreshListener The log in will be output on the console , Rich content , It is recommended that you copy these contents to format them online JSON Format your website to see it again , Some key information is listed below , Other contents have been omitted :
- As shown above , The instance information of the current application has been cached locally ;
start-up springclouddeepprovider
- application springclouddeepprovider Is the last application to launch , After starting the application , And then observe springcloudcustomizelistener Log , Discover applications springclouddeepprovider The registration information of has been obtained :
- thus , This actual battle has been completed , By developing a custom broadcast listener , We are right. Eureka We have a better understanding of the registration discovery mechanism of , In your Spring Cloud Learning process , I hope this article can help you ;
Welcome to your attention 51CTO Blog : Xinchen, programmer
On the way to study , You are not alone , Xinchen's original works are accompanied all the way …
边栏推荐
- 从类、API、框架三个层面学习如何设计可复用软件的学习心得
- Hanyuan hi tech 8-way telephone +1-way 100M Ethernet RJ11 telephone optical transceiver 8-way PCM telephone optical transceiver
- ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
- Generics, generic defects and application scenarios that 90% of people do not understand
- Esp32-c3 introductory tutorial problem ⑦ - fatal error: ESP_ Bt.h: no such file or directory ESP not found_ bt.h
- Digraph D and e
- sed -i命令怎么使用
- What is the principle of live CDN in the process of building the source code of live streaming apps with goods?
- #云原生征文#深入了解Ingress
- 1 channel 100m optical fiber transceiver 1 100m optical 1 100m electric desktop Ethernet optical fiber transceiver built-in power supply
猜你喜欢

618的省钱技术攻略 来啦 -体验场景 领取10元猫超卡!

Go写文件的权限 WriteFile(filename, data, 0644)?

64 channel telephone +2-channel Gigabit Ethernet 64 channel PCM telephone optical transceiver voice telephone to optical fiber

栈和队列的基本使用

AAIG看全球6月刊(上)发布|AI人格真的觉醒了吗?NLP哪个细分方向最具社会价值?Get新观点新启发~

Architecture design methods in technical practice

逆向调试入门-了解PE结构文件

Broadcast level E1 to aes-ebu audio codec E1 to stereo audio XLR codec

2022软科大学专业排名出炉!西电AI专业排名超清北,南大蝉联全国第一 !
![[Yunzhou said live room] - digital security special session will be officially launched tomorrow afternoon](/img/56/a6a9fbba0a9fc212883b469bb857c5.png)
[Yunzhou said live room] - digital security special session will be officially launched tomorrow afternoon
随机推荐
理财产品长期是几年?新手最好买长期还是短期?
中断和轮询
Hanyuan high tech USB2.0 optical transceiver USB2.0 optical fiber extender USB2.0 optical fiber transmitter USB2.0 interface to optical fiber
Tuikit audio and video low code solution navigation page
You call this shit MQ?
Hanyuan high tech new generation green energy-saving Ethernet access industrial switch high efficiency energy-saving Gigabit Industrial Ethernet switch
How to enable the SMS function of alicloud for crmeb knowledge payment
Runtime application self-protection (rasp): self-cultivation of application security
quartus調用&設計D觸發器——仿真&時序波驗證
OS的常见用法(图片示例)
实战监听Eureka client的缓存更新
Wallys/DR6018-S/ 802.11AX MU-MIMO OFDMA / 2* GE PORTS/WIFI 6e / BAND DUAL CONCURRENT
Online text filter less than specified length tool
Quartus II 13.1 detailed installation steps
The redis keys command should be used with caution in the production environment. It is best to shield it
618's money saving technology strategy is coming - experience the scene and get a 10 yuan cat super card!
串口、COM、UART、TTL、RS232(485)区别详解
2022 年以后,对AI开发人员意味着什么
栈和队列的基本使用
深入剖析MobileNet和它的变种