3. Analysis of Dubbo registration and subscription principles

Course Outline:

  1. Distributed project development and joint debugging
  2. Control the use of management background
  3. Detailed Explanation of Dubbo Registration Center

1. Distributed project development and joint debugging


Interface exposure and reference

In an RPC scenario, the caller calls the server through the interface, passes in parameters and obtains the returned result. In this way, the interface and model of the server must be exposed to the calling project. How is the server exposed? How does the client reference it?

interface information

, model information

,abnormal

picture

The usual way to expose an interface is to separate the interface from the implementation. The server will place the interface, model, exception, etc. in one module, and the implementation in another module. The caller is referenced through Maven.

picture

Automate builds and collaborate

When there are more and more projects and the service dependencies become more and more complex, in order to improve the efficiency of collaboration, automated tools must be used to complete the entire process from writing interfaces to building JAR packages and finally to citing them.

picture

Process description:

  1. The service provider project developer writes the Client interface
  2. push to remote warehouse
  3. jenkins build specified version
  4. Jenkins Deploye to private server warehouse nexus
  5. Service consumer project developers download from private service warehouse based on maven

Interface smooth upgrade:

In the process of project iteration, there are often multiple projects relying on the same interface. As shown in the figure below, projects B and C both rely on interface 1 in project A. At this time, the business needs of project B require an additional parameter for interface 1. After the upgrade is complete. Project B can be built and launched correctly, but project C cannot.

picture

Solutions and principles:

  1. The interface should be backward compatible: interface parameters should be encapsulated in the form of objects as much as possible. The Model attribute can only be added but not deleted. If it needs to be invalidated, you can add the @Deprecated mark.
  2. If there is an incompatible change, the caller must be notified to rectify and make a launch plan.

Development joint debugging:

During the project development process, the registration center of a development or test environment is likely to carry multiple services at the same time. If two sets of services are being jointly debugged, how to ensure that the target service is called?

1. Joint debugging based on temporary grouping

group grouping

Use the same temporary group in reference and server, and set it by group

2. Direct connection provider:

Specify the provider's url in the reference to achieve direct connection

<dubbo:reference  url="dubbo://127.0.0.1:20880" id="demoService"
                  timeout="2000"
                  interface="com.tuling.teach.service.DemoService" check="false"/>

3. Register only:

A project may be a service provider and a consumer at the same time. When testing, it needs to call a certain service and does not want the service being developed to affect other subscribers. How to implement it?

It can be realized by modifying register=false

<dubbo:registry address="multicast://224.5.6.7:1234" register="false"/>

## 2. Dubbo control and management background use


Dubbo control background version description:

2.5.8 background-admin

2.6 dubbo-admin

Dubbo control background installation:

#从github 中下载dubbo 项目
git clone https://github.com/apache/incubator-dubbo.git
#更新项目
git fetch
#临时切换至 dubbo-2.5.8 版本
git checkout dubbo-2.5.8
#进入 dubbo-admin 目录
cd dubbo-admin
#mvn 构建admin war 包
mvn clean pakcage -DskipTests
#得到 dubbo-admin-2.5.8.war 即可直接部署至Tomcat
#修改 dubbo.properties 配置文件
dubbo.registry.address=zookeeper://127.0.0.1:2181

Note: If you are really lazy to build, you can directly download the built one:

Link: https://pan.baidu.com/s/1zJFNPgwNVgZZ-xobAfi5eQ Extraction code: gjtv

Introduction to the basic functions of the control background:

  • Service Lookup:
  • View service relationship:
  • Service weight adjustment:
  • Service route:
  • 服务禁用
    

3. Detailed Explanation of Dubbo Registration Center


The role of the registry

In order to achieve the purpose of dynamic expansion of the service cluster, the registration center stores the address information and availability status information of the service, and pushes it to the clients who have subscribed to the relevant service in real time.

picture

A complete registration center needs to implement the following functions:

  1. Receive the registration of the server and the reference of the client, that is, associate the reference with the consumption, and support many-to-many.
  2. Instantly clear the state of a service when it shuts down abnormally
  3. When the registration center is restarted, the registration data and subscription requests can be automatically restored
  4. The cluster of the registry itself

Registration centers supported by Dubbo

  1. Multicast Registry
  2. Based on the network broadcast technology, it can only be used in the local area network, and is generally used for simple test services
  3. Zookeeper registry ( recommended )
  4. Zookeeper is a sub-project of Apacahe Hadoop. It is a tree-type directory service that supports change push. It is suitable as a registration center for Dubbo services. It has high industrial strength and can be used in production environments. It is recommended to use
  5. Redis Registry
  6. Redis-based registry
  7. Simple Registry
  8. Based on its own Dubbo service implementation (SimpleRegistryService), it does not support clusters and can be used as a reference for a custom registry, but it is not suitable for direct use in a production environment.

Redis Registry

We need to know two things about the Redis registry,

  1. How to store service registration and subscription relationships
  2. is how to update immediately when the service status changes

Dubbo uses the publish-subscribe feature of Redis to realize real-time data synchronization between the provider and the consumer. The principle is as follows:

Redis publish subscribe

Redis publish-subscribe (pub/sub) is a message communication mode: the sender (pub) sends the message, and the subscriber (sub) receives the message.

A Redis client can subscribe to any number of channels.

The following figure shows the channel channel1, and the relationship between the three clients subscribed to this channel - client2, client5 and client1:

picture

When a new message is sent to the channel channel1 through the PUBLISH command, this message will be sent to the three clients subscribed to it:

Demonstrates the use of Redis as a registry.

  • Start the Redis service
  • Server configuration registration center
  • Start two servers
  • Observe the data in Redis through the RedisClient client

Redis registry configuration:

<dubbo:registry protocol="redis" address="192.168.0.147:6379"/>

When we started the two servers, we found that a record of Hash type was added in Reids, and its key was /dubbo/tuling.dubbo.server.UserService/providers. The URLs and validity periods of the two service providers are stored in Value respectively.
picture

The same consumer is also similar to its overall structure as follows:

//服务提供者注册信息 
/dubbbo/com.tuling.teach.service.DemoService/providers
  dubbo://192.168.246.1:20880/XXX.DemoService=1542619052964 
  dubbo://192.168.246.2:20880/XXX.DemoService=1542619052964 
//服务消费订阅信息
/dubbbo/com.tuling.teach.service.DemoService/consumers
  dubbo://192.168.246.1:20880/XXX.DemoService=1542619788641
  • The main key is the service name and type
  • The Key in the Map is the URL address
  • The Value in the Map is the expiration time, which is used to judge dirty data, and the dirty data will be deleted by the monitoring center

Next, answer the second question. Can the status of the provider be changed immediately when the provider suddenly goes down ?

Here Dubbo uses a regular heartbeat mechanism to maintain the validity period of the service URL. By default, the validity period is updated every 30 seconds. That is, the millisecond value corresponding to the URL. For specific codes, see: com.alibaba.dubbo.registry.redis.RedisRegistry#expireExecutor

picture

com.alibaba.dubbo.registry.redis.RedisRegistry#deferExpired

com.alibaba.dubbo.registry.integration.RegistryDirectory

com.alibaba.dubbo.registry.support.ProviderConsumerRegTable

Zookeeper Registry

Regarding the Zookeeper registration center, it is also necessary to understand its storage structure and update mechanism.

Zookeper is a tree-type directory service that supports change push and is more stable than redis's Publish/Subscribe function.

structure:

picture

Failed to reconnect

com.alibaba.dubbo.registry.support.FailbackRegistry

Provider disconnected abruptly:

Based on the Zookeeper temporary node mechanism, Zookeeper will automatically delete all temporary nodes after the client session times out. The default is 40 seconds.

// Create a temporary node

com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient#createEphemeral

Question:
If a client joins within 40 seconds of zookeeper disconnection, will the invalid provider connection be called?

Answer: No, after the provider goes down, its connection with the client will be disconnected immediately, and the client will detect the long connection status before calling.

// 检测连接是否有效
com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker#isAvailable

Creating configurators and routers will create persistent nodes

// create persistent node

com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient#createPersistent

Implementation of the service subscription mechanism:

// 注册目录
com.alibaba.dubbo.registry.integration.RegistryDirectory

Source code analysis:
picture

com.alibaba.dubbo.registry.integration.RegistryDirectory

The composition diagram is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_39513430/article/details/108301812