当前位置:网站首页>The graphic details Eureka's caching mechanism/level 3 cache
The graphic details Eureka's caching mechanism/level 3 cache
2022-08-01 19:33:00 【Bald loves fitness】
前言
1、为什么说Eureka是CAP理论中的AP?
从CAP理论看,Eureka是一个AP系统,Its priority to ensure availability(A)和分区容错性§,不保证强一致性,but can achieve eventual consistency.
- Because only in the cluster
Any instance does not出现问题,Eureka服务就是可用的;即Eureka Client 在向某个 Eureka Server 注册时,如果发现连接失败,则会自动切换至其它节点; - 另外EurekaThere is no concept of master-slave in the cluster,各个节点都是平等的,节点间采用ReplicateAsynchronous way to synchronize data;
也正因为Eureka It does not guarantee strong data consistency by itself,So a lot of caches are designed in the architecture.
2、Why design so many caches(三级缓存)?
这个和MySQLIt is very similar to use master-slave for read-write separation,The read and write separation at the database level is to share the read pressure of the main database;而EurekaThe L3 cache is also for read-write separation,Make write operations not block read operations.
- 因为在
写的时候,线程会持有ConcurrentHashmap相应HashBarrels of the node object lock,block the sameHashother reading threads for the bucket. - This can effectively reduce read and write concurrency,Avoid the pressure of reading, writing, reading and writing for resources.
Then add so much cache,How to ensure the eventual consistency of cached data?
Let's talk about it in detail belowEureka是如何做的.
缓存机制
Eureka ServerThere are three variables used to save the service registration information,分别是:registry、readWriteCacheMap、readOnlyCacheMap;默认情况下定时任务每30s将二级缓存readWriteCacheMapSync to L3 cachereadOnlyCacheMap;
1、Eureka Server每60s清理超过90s * 2(官方彩蛋)未续约的节点;
2、Eureka Client每30s从readOnlyCacheMap更新服务注册信息;
3、UIinterface fromregistryGet the latest service registration information.
1、三级缓存分别是什么?
| 缓存 | 缓存类型 | 所处类 | 概述 |
|---|---|---|---|
| registry 一级缓存 | ConcurrentHashMap | AbstractInstanceRegistry | 实时更新,又名注册表,UI界面从这里获取服务注册信息; |
| readWriteCacheMap 二级缓存 | Guava Cache(LoadingCache) | ResponseCacheImpl | 实时更新,缓存时间180秒; |
| readOnlyCacheMap 三级缓存 | ConcurrentHashMap | ResponseCacheImpl | 周期更新,默认每30s从二级缓存readWriteCacheMap中同步数据更新; Eureka Client默认从这里获取服务注册信息,可配为直接从readWriteCacheMap获取 |
L2 cache, also known as read-write cache、L3 cache, also known as read-only cache;
1)Eureka Server缓存相关配置
| 配置名 | 默认值 | 概述 |
|---|---|---|
| eureka.server.useReadOnlyResponseCache | true | Client从readOnlyCacheMap更新数据,false则跳过readOnlyCacheMap直接从readWriteCacheMap更新 |
| eureka.server.responsecCacheUpdateIntervalMs | 30000 | readWriteCacheMap更新至readOnlyCacheMap的周期,默认30s |
2、Data synchronization between caches
AbstractInstanceRegistry类中的registryfield is registry、and save the first level cache with(Real-time latest data);ResponseCacheImpl类中的readWriteCacheMap字段和readOnlyCacheMapFields represent L2 cache and L3 cache respectively;Below we will focus on these two classes、Data synchronization of three fields is discussed.
1)注册一个服务实例
Eureka Server中做的操作:
向注册表registryWrite service instance information in,并使得二级缓存失效;

Eureka client第一次向Eureka ServerWhen registering a service or sending a heartbeat to renew,会进去到Eureka Serve中ApplicationResource#addInstance()方法中:
最终进入到AbstractInstanceRegistry#register()方法中,往其registryAdd service registration information to the field:
Here is an example of service registration,我们知道了registry的数据来源;
注意,At the end of the service registration, the second level cache will be emptied/失效;
Let's take a look nextEurekaHow the L2 and L3 caches work?
2)二级/When is the L3 cache initialized??
Eureka Server启动的时候会根据SpringBoot自动装配的特性,初始化EurekaServerContext接口的实现类DefaultEurekaServerContext;在DefaultEurekaServerContextConstructor post logic is executed during initializationinitialize(),which initializes the registry;
接着进入到PeerAwareInstanceRegistry接口的实现类PeerAwareInstanceRegistryImpl#init()方法中,其中会通过调用initializedResponseCache()方法Initialize secondary/三级缓存;
3)二级/What did the L3 cache initialization do??
这里可以看到initializedResponseCache()方法中直接new了一个ResponseCacheImpl类;
我们接着进入到ResponseCacheImpl类中,看一下它的构造函数:
总的来说,在初始化ResponseCacheImpl类时:
- Will set to initialize the second level cache
readWriteCacheMap(过期时间180s),Set L2 cache to L3 cachereadOnlyCacheMap同步的时间间隔(默认30s). - Set whether to use L3 cache(默认使用),If used, start a cron task,默认每隔30sSync data from L2 cache to L3 cache(Update only existing in the triple cachekey);
4、发现/寻找一个服务
针对Eureka Client和UI界面,The way they read service registration information is slightly different:
- 针对Eureka Client:
1、如果使用只读缓存(三级缓存)<默认使用>,then get it from the read-only cache first;如果获取不到,from the read-write cache(二级缓存)中获取,and cache the data to a read-only cache;
2、Do not use read-only cache,directly from the read-write cache;Triggered if not availableguavathe callback function from the registryregistry中同步数据(i.e. from L1 cache – 注册表registry中取)- 而对于UI界面,is real-time from the first level cache(注册表registry)中取.
对于Eureka Clienteither get aApplication的信息(入口为ApplicationResource#getApplication())or get allApplication的信息(入口为ApplicationsResource#getContainers())都会进入到ResponseCacheImple#get(Key key)方法,then go through this path《get(key, shouldUseReadOnlyResponseCache) --> getValue(final Key key, boolean useReadOnlyCache)》Finally came to the logic of actually reading the data from the cache:

总结
1、Eureka Server 在接收Eureka Client注册的时候,read-write cache(二级缓存)清空;
2、Eureka Server启动时会做两件事:
- Will initialize read and write cache(
二级缓存),从注册表registry(一级缓存)中实时加载数据,默认180s过期;- Determine whether to use read-only cache(三级缓存),默认开启;Enable one if used定时任务,默认每30sDo a data synchronization from read-write cache to read-only cache;
3、Eureka ClientWhen obtaining service information,The default from a read-only cache first;If it cannot be obtained, it will be obtained from the read-write cache.,and cache the data to a read-only cache;获取不到,再触发guavathe callback function from the registry中同步(i.e. from L1 cache – 注册表registry中取).
1、The benefits of multi-level caching?
- Avoid frequent read and write conflicts in service registration as much as possible,写阻塞读;
- 提高Eureka Server服务的读写性能.
In the face of frequent competition for read and write resources、Write blocking read, etc.,I can consider borrowingEurekaThe multi-level cache scheme does read-write separation.
边栏推荐
- Tencent Cloud Hosting Security x Lightweight Application Server | Powerful Joint Hosting Security Pratt & Whitney Version Released
- Gradle系列——Gradle文件操作,Gradle依赖(基于Gradle文档7.5)day3-1
- ExcelPatternTool: Excel form-database mutual import tool
- DAO开发教程【WEB3.0】
- 【综述专栏】IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
- mysql自增ID跳跃增长解决方案
- 力扣刷题之合并两个有序数组
- Heavy cover special | intercept 99% malicious traffic, reveal WAF offensive and defensive drills best practices
- Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
- Ha ha!A print function, quite good at playing!
猜你喜欢

Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法

Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结

58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)

18、分布式配置中心nacos

从普通进阶成优秀的测试/开发程序员,一路过关斩将

From ordinary advanced to excellent test/development programmer, all the way through

Library website construction source code sharing

百度无人驾驶商业化已“上路”

Gradle系列——Gradle文件操作,Gradle依赖(基于Gradle文档7.5)day3-1

MySQL你到底都加了什么锁?
随机推荐
不要再使用MySQL online DDL了
1个小时!从零制作一个! AI图片识别WEB应用!
【Redis】缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存击穿、缓存降级
Redis启动时提示Creating Server TCP listening socket *:6379: bind: No error
Screenshot of Selenium in Remote
Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
【周赛复盘】LeetCode第304场单周赛
手撸代码,Redis发布订阅机制实现
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
Choosing the right DevOps tool starts with understanding DevOps
突破边界,华为存储的破壁之旅
【服务器数据恢复】服务器Raid5阵列mdisk组中多块磁盘离线的数据恢复案例
Win10, the middle mouse button cannot zoom in and out in proe/creo
SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
Win11如何删除升级包?Win11删除升级包的方法
安装win32gui失败,解决问题
ssh & scp
Every calculation, & say what mean
How to query database configuration parameters in GBase 8c, such as datestyle.What function or syntax to use?
随时随地写代码--基于Code-server部署自己的云开发环境