In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what redis has contributed to the field of micro-services, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Preface
When it comes to redis, the keywords that pop up in your mind are: NoSQL, KV, high performance, caching and so on. But today's article starts from another perspective-- micro-services.
The cause of this article also comes from an interview experience, when interviewing a candidate from Momo (the Momo who made friends), he mentioned that it was very interesting for me to say that redis is widely used in Momo, in addition to regular caching, it is also used as a NoSQL database in some scenarios, redis is used as a micro-service registry, and even the RPC call protocol uses the redis protocol.
Registration center
I first learned that redis can be used as a registry from the source code of dubbo, but I don't know much about it, because I've never heard of any company using redis for service discovery.
It is quite easy to use redis for service discovery in dubbo. Introduce jedis dependency and change the registry address to redis address:
Redis.clients jedis 2.9.0
Dubbo.registry.address=redis://127.0.0.1:6379
The data registered is like this, and the type is hash
/ dubbo/$ {service} / ${category}
Such as
/ dubbo/com.newboo.sample.api.DemoService/consumers/dubbo/com.newboo.sample.api.DemoService/providers
The key saved under the hash data structure is registered and the url,value is expired.
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, the registry only needs to comply with the basic functions of data storage, monitoring push changes and heartbeat detection.
Take dubbo as an example to see how redis uses its own features to complete the functions of the registry (take dubbo version 2.7.8 as an example):
Service registration
When registering the service, provider writes the url of the service provider to / dubbo/$ {service} / providers. The data type is hash,key, the expiration time of provider url,value is key, and the default is 60s, which is configurable.
Issue a register event provider with / dubbo/$ {service} / providers as the publish command for key invocation after the write is completed. Provider is used for every 1x2 expiration time (default 30s) from initialization.
Provider re-registers and publishes register events
Service discovery
Get the key that matches / dubbo/$ {service} / * (the keys command is used here), and get the following types: / dubbo/$ {service} / providers, / dubbo/$ {service} / routers, / dubbo/$ {service} / configuators
Hgetall the key obtained by / dubbo/$ {service} / *, get the real provider list and configuration data, assemble and match
At the same time, open a separate thread for each subscribe service, and execute the psubscribe command on / dubbo/$ {service} to block waiting for an event to occur.
From the point of view of source code and testing, dubbo's redis registry cannot be used directly in a production environment for two reasons:
The use of the keys command blocks the execution of a single-threaded redis,keys, and other commands have to be queued
Without heartbeat detection, I tested that after provider was killed by kill-9, consumer was imperceptible. However, from the point of view of implementation, we want to judge whether the service is available by the stored expiration time, that is, we need to compare the value corresponding to url with the current time. If it expires, it should be removed, but this part does not seem to be complete.
Although dubbo's redis registry is not available, it does not prevent him from building a production registry. Momo is a good example.
RPC call protocol
Redis protocol as a RPC calling protocol was also told by Momo. At that time, I asked him two questions:
Why choose redis protocol as the RPC calling protocol
How to pass through implicit parameters similar to header in redis Protocol
The answer to the first question was also quite unexpected. He said it was for cross-language invocation. At that time, he felt that only http, gRPC and other protocols had achieved cross-language, and it was the first time that redis protocol had been heard of. But when you think about it, there's nothing wrong with it. which back-end language doesn't implement redis clients now?
The reason why redis protocol can be cross-language is that its design is very simple and easy to implement. For more information on the protocol, please see this link:
Http://redisdoc.com/topic/protocol.html
Let me give you an example of how concise the redis protocol is, which is a project that I focused on a long time ago.
Https://github.com/jdp/redisent
It is a php implementation of the redis client, only a php file, a total of 196 lines, these 196 lines contain comments, variable definition, link establishment, etc., the real parsing protocol code is very little, the request coding and sending only 17 lines of code, parsing returned code only 58 lines! As introduced in the project: simple, no-nonsense
The answer to the second question is consistent with my expectations. Implicit parameters like header can not be supported at the redis protocol level, but Momo's RPC framework is self-developed, so they solved this problem at the framework layer, serializing them to choose json. If you want to transparently transmit header parameters, the framework assembles the parameters into the transport body.
Unfortunately, the implementation of the redis protocol in dubbo is incomplete, so it can not expose the redis protocol and can only be called, so testing can only test that client connects to the redis server for get and set calls, which is of little significance.
The above is all the content of the article "what is the contribution of redis in the field of micro-services"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 281
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.