This article has been included github.com/lkxiaolou/l… Welcome to star.

preface

When it comes to Redis, the key words that may come to mind are: NoSQL, KV, high performance, cache, etc. But today’s article takes a different Angle — microservices.

The origin of this article also comes from an interview experience. In an interview with a candidate from Momo (the same momo that makes friends), he mentioned something interesting to me. He said that Redis is widely used on Momo, in addition to regular caching, it can also be used as NoSQL database in some situations. Redis is also used as the registry of microservices, and even the call protocol of RPC is redis.

The registry

The first known use of Redis as a registry came from Dubbo’s source code, but I never knew much about it because I never heard of any companies using Redis for service discovery.

In dubbo, it is easy to use Redis to do service discovery by introducing jedis dependencies and changing the registry address to redis address:

Clients </groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>Copy the code
dubbo.registry.address=redis:/ / 127.0.0.1:6379
Copy the code

The data that’s registered is going to look like this, and it’s going to be of type hash

/dubbo/${service}/${category}
Copy the code

Such as

/dubbo/com.newboo.sample.api.DemoService/consumers
/dubbo/com.newboo.sample.api.DemoService/providers
Copy the code

The key stored in the hash data structure is the registered URL and the value is the expiration time

127.0.0.1:6379 > hgetall dubbo/com. Newboo. Sample. API. DemoService/will 1) "Dubbo: / / 172.23.233.142:20881 / com. Newboo. Sample. The API. DemoService? Anyhost = true&application = the 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 of = = provider&timestamp = 1621857955 355 "2)" 1621858734778"Copy the code

In theory, the registry only needs to meet the basic functions of data storage, listening to push changes, and heartbeat detection.

Take dubbo as an example to see how Redis uses its features to complete the registry function (dubbo version 2.7.8 as an example) :

  • The service registry

    • When registering the service, the provider writes the URL of the service provider/dubbo/${service}/providersIn the following command, the data type is hash, key is the url of the provider, and value is the expiration time of the key. The default value is 60 seconds
    • After the write is complete/dubbo/${service}/providersAs key callspublishCommand to publish a register event
    • The provider is initialized with a separate thread every other time1/2 Expiration time(default 30s) re-registers the Provider and issues register events
  • Service discovery

    • For matching/dubbo/${service}/*Key is used herekeysCommand), get the following:/dubbo/${service}/providers,/dubbo/${service}/routers,/dubbo/${service}/configuators
    • right/dubbo/${service}/*Get the key and proceedhgetallGet the real provider list and configuration data to assemble and match
    • Open a separate thread for each subscribe service, yes/dubbo/${service}performpsubscribeThe command blocks until an event occurs

From source code and testing, Dubbo’s Redis registry cannot be used directly in a production environment for two reasons:

  • Using thekeysThe redis and keys command blocks a single thread while all other commands are queued
  • There is no heartbeat detection function, I tested the provider bykill -9After killing, the consumer can’t sense it. However, from the perspective of implementation, it wants to judge whether the service is available by the expiration time of the storage, that is, it needs to compare the value corresponding to the URL with the current time, if the expiration time should be removed, but this part seems not complete

Although Dubbo’s Redis registry is not productive, that doesn’t prevent him from building a registry that is productive. Momo is a good example.

RPC calling protocol

Redis protocol as RPC call protocol was also told to me by Momo. I asked him two questions at that time:

  • Why choose Redis protocol as RPC call protocol
  • How does redis protocol pass through similar to headerImplicit parameter

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 could achieve cross-language invocation. But on second thought, it’s fine. What back-end language doesn’t have a redis client now?

The reason why redis protocol can achieve cross-language, this also depends on its design is very simple, easy to implement, detailed protocol content can refer to this link:

http://redisdoc.com/topic/protocol.html

Let me just give you an example of how simple the Redis protocol is, which is a project I’ve been looking at for a long time, right

https://github.com/jdp/redisent

It is a PHP implementation of redis client, there is only one PHP file, a total of 196 lines, these 196 lines contain comments, variable definition, link establishment, etc., the actual parsing protocol code is very little, the request encoding and sending only 17 lines of code, parsing only 58 lines of code back! As the project’s introduction says: Simple, no-nonsense

The answer to the second question is consistent with my expectation. From the level of Redis protocol, the implicit parameters like header cannot be supported temporarily, but Momo’s RPC framework is self-developed, so they solve this problem at the framework level. They choose JSON for serialization.

Unfortunately, the implementation of the Redis protocol in Dubbo is not complete, which cannot expose the Redis protocol and can only be called. Therefore, the test can only test the client connection to the Redis server for GET and SET calls, which is of little significance.

conclusion

Redis is a very versatile storage component right now, and while it’s not mainstream in the microservices space, it gives us a way to at least follow through.


Search attention wechat public number “bug catching master”, back-end technology sharing, architecture design, performance optimization, source code reading, problem solving, practice.