current position:Home>Redis' contribution in the field of microservices
Redis' contribution in the field of microservices
2022-06-24 12:46:55【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}/providers
Next , 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}/providers
by key callpublish
Order 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 herekeys
command ), 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}
performpsubscribe
Command 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
keys
command , It blocks single threads redis,keys During execution , Other orders have to line up - There's no heartbeat detection , I tested provider By
kill -9
After 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 .
copyright notice
author[Guixian old man],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/175/20210526103538428g.html
The sidebar is recommended
- [Tencent cloud 618 countdown!] Promotion strategy of the promotion activities
- Simple and flexible permission design?
- Making daily menu applet with micro build low code
- 99% of the students can't write good code because of this problem!
- Istio practical skills: using prism to construct multi version test services
- Kubernetes practical technique: setting kernel parameters for pod
- A scheme for crawlers to collect public opinion data
- Essential key steps in the construction of e-commerce live broadcast source code
- How do websites and we media tap user needs? Deeply expose the secrets behind the keywords!
- From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
guess what you like
Continuous testing | key to efficient testing in Devops Era
It's settled! Bank retail credit risk control just does it!
Encapsulate the method of converting a picture file object to Base64
The pod is evicted due to insufficient disk space of tke node
Post processing - deep camera deformation effects
Tencent released credit risk control results safely: it has helped banks lend more than 100 billion yuan
Interesting erasure code
Five minutes to develop your own code generator
How to make secruecrt more productive
About me, a 19 line programmer
Random recommended
- Kubernetes practical skill: entering container netns
- [programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
- Design and implementation of high performance go log library zap
- Ghost, a synonym for blog system
- Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
- IOMMU (VII) -vfio and mdev
- [highlights] summary of award-winning activities of Tencent cloud documents
- The programmer's graduation project is still bald after a year
- How to solve the problem that MBR does not support partitions over 2T, and lossless transfer to GPT
- Another prize! Tencent Youtu won the leading scientific and technological achievement award of the 2021 digital Expo
- Use txvideoeditor to add watermark and export video card at 99%? No successful failed callback?
- Reset the password, and the automatic login of the website saved by chrome Google browser is lost. What is the underlying reason?
- [log service CLS] Tencent cloud log service CLS accesses CDN
- [live broadcast of celebrities] elastic observability workshop
- Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
- SMS SMS
- WPF from zero to 1 tutorial details, suitable for novices on the road
- Dingding, Feishu, and enterprise wechat: different business approaches
- [2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
- How to do research on plant endophytes? Special topic on Microbiology
- Mlife forum | microbiome and data mining
- 105. simple chat room 8: use socket to transfer pictures
- Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
- Reading notes of returning to hometown
- Generate the NC file of 4-D air pressure and temperature, and then read the code (provide the code)
- Codereview tool chain for micro medicine
- Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer
- On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"
- Use the open source tool k8tz to gracefully set the kubernetes pod time zone
- How does Argo family bucket make Devops easier?
- A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore
- MySQL foreign key impact
- Common special characters in JS and TS
- The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out
- Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
- what the fuck! I'm flattered. He actually wrote down the answers to the redis interview questions that big companies often ask!
- Concept + formula (excluding parameter estimation)
- [database] final review (planning Edition)
- Troubleshooting and optimization of files that cannot be globally searched by ordinary users in easydss video platform customization project
- How can ffmpeg streaming to the server save video as a file through easydss video platform?