当前位置:网站首页>Redis' contribution in the field of microservices
Redis' contribution in the field of microservices
2022-06-24 12:46:00 【Guixian old man】
This article has been included https://github.com/lkxiaolou/lkxiaolou welcome star.
Preface
Speaking of redis, Maybe the key words that pop up in your mind are :NoSQL、KV、 High performance 、 Cache, etc . But today's article is from another angle —— Microservices .
The reason for this article is also from an interview experience , In an interview with a candidate from Momo ( It's the one who makes friends ) when , He mentioned a point that made me feel very interesting , He said redis It is widely used in Momo , In addition to regular caching , In some scenarios, it's also NoSQL Database to use , Also use redis As a registry of microservices , Even RPC All of the calling protocols used redis agreement .
Registry Center
The first to learn redis Can be used as a registry from dubbo In the source code of , But I don't know much about it , Because I've never heard of a company using redis To do service discovery .
stay dubbo Use in redis It's very easy to do service discovery , introduce jedis rely on , Change the registry address to redis address :
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
dubbo.registry.address=redis://127.0.0.1:6379
The registration data is like this , The type is hash
/dubbo/${service}/${category}Such as
/dubbo/com.newboo.sample.api.DemoService/consumers /dubbo/com.newboo.sample.api.DemoService/providers
hash Data structure key It's registered url,value Is the expiration time
127.0.0.1:6379> hgetall /dubbo/com.newboo.sample.api.DemoService/providers 1) "dubbo://172.23.233.142:20881/com.newboo.sample.api.DemoService?anyhost=true&application=boot-samples-dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.newboo.sample.api.DemoService&metadata-type=remote&methods=sayHello&pid=19807&release=2.7.8&side=provider×tamp=1621857955355" 2) "1621858734778"
In theory , As long as the registry conforms to the data store 、 Monitor push changes 、 The basic functions of heartbeat detection are OK .
With dubbo For example redis How to use its own characteristics to complete the function of the registry ( With dubbo 2.7.8 Version as an example ):
- Service registration
- provider At the time of service registration , Will the service provider's url write in
/dubbo/${service}/providersNext , The data type is hash,key For the provider url,value by key The expiration time of , The default is 60s, Configurable - After the writing is completed, write with
/dubbo/${service}/providersby key callpublishOrder to issue a register event - provider Start a separate thread at initialization time every
1/2 Expiration time( Default 30s) When the provider Re register and publish register event - Service discovery
- Get a match
/dubbo/${service}/*Of key( Used herekeyscommand ), I got these kinds of :/dubbo/${service}/providers、/dubbo/${service}/routers、/dubbo/${service}/configuators - Yes
/dubbo/${service}/*To get the key Conducthgetall, Get the real provider List and configuration data , Carry out assembly 、 matching - At the same time for each subscribe There is a separate thread for the service , Yes
/dubbo/${service}performpsubscribeCommand blocking, waiting for an event to happen
In terms of source code and testing ,dubbo Of redis Registries cannot be used directly in production environments , There are two reasons for this :
- Used
keyscommand , It blocks single threads redis,keys During execution , Other orders have to line up - There's no heartbeat detection , I tested provider By
kill -9After killing ,consumer Is imperceptible . But from the implementation point of view, we want to judge whether the service is available by the expiration time of the storage , That is, we need to compare url Corresponding value With the current time , If it is overdue, it should be eliminated , But this part doesn't seem to be complete
although dubbo Of redis Registry production is not available , But it doesn't affect that he can build a production usable registry , Momo is a good example .
RPC Call Protocol
redis Act of agreement RPC Calling protocol is also what Momo told me , I asked him two questions :
- Why choose redis Act of agreement RPC Call Protocol
- redis How the protocol is transmitted is similar to header Of
Implicit parameter
The answer to the first question is quite unexpected , He said it's for cross language calls , I thought it was just http、gRPC And so on ,redis It's the first time I've heard the agreement across languages . But think about it , There's nothing wrong with it , Now which back-end language is not implemented redis The client of ?
The reason redis Protocols can be cross language , It all depends on its simple design , Easy to implement , Please refer to this link for details of the agreement :
http://redisdoc.com/topic/protocol.html
Let me give you an example redis How concise is the agreement , This is a project that I focused on a long time ago
https://github.com/jdp/redisent
It's a php Realized redis client , only one php file , common 196 That's ok , this 196 Line contains comments , Variable definitions , Link establishment, etc , Very little code really parses the protocol , The encoding and sending of the request only takes 17 Line code , The only code returned by parsing is 58 That's ok ! Just like the introduction of the project :simple, no-nonsense
The answer to the second question is in line with my expectation , from redis For the time being, the level of the agreement can't support similar header The implicit parameter of , But Momo's RPC The framework is self-developed , So they solved this problem at the framework level , They chose json, If you want to pass it through header Parameters , The framework assembles the parameters into the transporter .
Unfortunately dubbo Medium redis The protocol implementation is incomplete , Cannot be exposed redis agreement , Can only call , So testing can only test client Connect to redis Server run get、set call , It doesn't make much sense .
summary
redis Now it's a very versatile storage component , Although it's not mainstream in the field of microservices , But it also gives us an idea , At least this road is accessible .
WeChat official account " Master bug catcher ", Back end technology sharing , Architecture design 、 performance optimization 、 Source code reading 、 Troubleshoot problems 、 Step on the pit practice .
边栏推荐
- How does easygbs, a national standard platform, solve the problem that information cannot be carried across domains?
- Jupyter notebook service installation and startup
- Discussion on redis communication protocol
- 微医CodeReview工具链
- Flink snapshot analysis: operators for locating large states and data skew
- mLife Forum | 微生物组和数据挖掘
- 巴比特 | 元宇宙每日必读:618成绩已然揭晓,在这份还算满意的答卷背后,数字藏品做出了多少贡献?...
- A scheme for crawlers to collect public opinion data
- WPF from zero to 1 tutorial details, suitable for novices on the road
- Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
猜你喜欢

Who said that "programmers are useless without computers? The big brother around me disagrees! It's true

mLife Forum | 微生物组和数据挖掘

On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"

线程同步的基石AbstractQueuedSynchronizer详解
![[database] final review (planning Edition)](/img/94/cd2df3a011c4e466df5aaa0f3bd0f2.png)
[database] final review (planning Edition)

文本转语音功能上线,可以体验专业播音员的服务,诚邀试用

【2022国赛模拟】摆(bigben)——行列式、杜教筛

从《梦华录》的争议性,谈谈数字版权作品的价值泡沫

Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)

微医CodeReview工具链
随机推荐
[programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
How to make Baidu quickly include its own personal website?
How to evaluate software development projects reasonably?
Leetcode 1218. 最长定差子序列
What if the WordPress website forgets its password
Argo 全家桶如何让 DevOps 变的更容易?
Metamask项目方给Solidity程序员的16个安全建议
【数据挖掘】期末复习(样卷题目+少量知识点)
[tke] GPU node NVIDIA Tesla driver reinstallation
Continuous testing | making testing more free: practicing automated execution of use cases in coding
[live broadcast of celebrities] elastic observability workshop
pipeline groovy
Attack popular science: DDoS
The operation and maintenance boss laughed at me. Don't you know that?
【概率论期末抱佛脚】概念+公式(不含参数估计)
使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
RTMP streaming platform easydss video on demand interface search bar development label fuzzy query process introduction
IOMMU (VII) -vfio and mdev
[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
It's settled! Bank retail credit risk control just does it!