current position:Home>Redis' contribution in the field of microservices

Redis' contribution in the field of microservices

2022-06-24 12:46:55Guixian 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&timestamp=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 call publish 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 here keys command ), I got these kinds of :/dubbo/${service}/providers/dubbo/${service}/routers/dubbo/${service}/configuators
  • Yes /dubbo/${service}/* To get the key Conduct hgetall, 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} perform psubscribe Command blocking, waiting for an event to happen
img1.png

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 .

img2.png

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

Random recommended