当前位置:网站首页>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
类中的registry
field is registry、and save the first level cache with(Real-time latest data);ResponseCacheImpl
类中的readWriteCacheMap
字段和readOnlyCacheMap
Fields 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()
方法中,往其registry
Add 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
;在DefaultEurekaServerContext
Constructor 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.
边栏推荐
- MLX90640 红外热成像仪测温模块开发笔记(完整篇)
- PHP 安全最佳实践
- When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
- 58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
- 把 Oracle 数据库从 RAC 集群迁移到单机环境
- 突破边界,华为存储的破壁之旅
- 【1374. 生成每种字符都是奇数个的字符串】
- 腾讯云主机安全 x 轻量应用服务器|强强联合主机安全普惠版重磅发布
- TestNG多个xml进行自动化测试
- Website construction process
猜你喜欢
How PROE/Croe edits a completed sketch and brings it back to sketching state
10 个 PHP 代码安全漏洞扫描程序
#yyds dry goods inventory# Interview must brush TOP101: the last k nodes in the linked list
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
Ha ha!A print function, quite good at playing!
Win11怎么安装语音包?Win11语音包安装教程
17、负载均衡
Redis启动时提示Creating Server TCP listening socket *:6379: bind: No error
BN BatchNorm + BatchNorm的替代新方法KNConvNets
网络不通?服务丢包?这篇 TCP 连接状态详解及故障排查,收好了~
随机推荐
经验共享|在线文档协作:企业文档处理的最佳选择
How PROE/Croe edits a completed sketch and brings it back to sketching state
Website construction process
What should I do if the Win11 campus network cannot be connected?Win11 can't connect to campus network solution
不要再使用MySQL online DDL了
面试必问的HashCode技术内幕
datax - 艰难debug路
Every calculation, & say what mean
How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
Win11怎么安装语音包?Win11语音包安装教程
Mobile Zero of Likou Brush Questions
在表格数据上,为什么基于树的模型仍然优于深度学习?
57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
【服务器数据恢复】服务器Raid5阵列mdisk组中多块磁盘离线的数据恢复案例
【Redis】缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存击穿、缓存降级
Tencent Cloud Hosting Security x Lightweight Application Server | Powerful Joint Hosting Security Pratt & Whitney Version Released
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
MySQL开发技巧——存储过程
18. Distributed configuration center nacos
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度