RPC framework --Dubbo

Dubbo Introduction

Dubbo is a framework used to implement distributed RPC calls.

Dubbo is a distributed service framework, providing high performance and transparency of RPC remote service call programs, services and SOA governance program.
Dubbo support multiple protocols, including protocols Dubbo, Hession protocols, transport protocols bottom Dubbo protocol is TCP.

Dubbo uses a very simple model, either the provider of service, either consumer consumer services, so this can be based on abstract service provider (Provider) and consumer services (Consumer) two roles. As well as its registry, protocol support, service monitoring, etc., see below for details.

Dubbo framework

Dubbo frame design divided a total of 10 layers, the top layer is left to the Service actually want to use the developer Dubbo development of distributed services implement business logic interface layer. The figure for the consumer to use the service interface, the right light green left light blue background interface for service providers to use the background, located in central axis used for both interfaces.
Here Insert Picture Description
Overall framework is divided into ten layers:

  1. Interface Layer Service (Service): This layer is associated with the actual business logic, according to a service provider and the service consumer interface design and implementation of services corresponding.
  2. Layer configuration (Config): External configuration interface to ServiceConfig ReferenceConfig center and can be directly new configuration class, the configuration may be arranged to generate class by parsing spring.
  3. Service proxy layer (Proxy): transparent proxy service interfaces, generating a service customer terminal and the server Skeleton Stub, in center ServiceProxy expansion interface ProxyFactory.
  4. Service registration layer (Registry): registration and discovery package service address, to serve as the center URL, extended interface is RegistryFactory, Registry and RegistryService. It may not be registered service center, the Service Provider directly exposed services.
  5. Cluster layer (Cluster): encapsulating a plurality of providers routing and load balancing, and bridging the registration center to center Invoker expansion interface Cluster, Directory, Router and LoadBalance. The combination of multiple service providers as a service provider, to achieve transparency of service consumers, providers only need to interact with a service.
  6. Monitoring layer (Monitor): RPC number of calls and call time monitoring to Statistics as the center, the expansion interface MonitorFactory, Monitor and MonitorService.
  7. Remote call level (Protocol): The RPC call letters to Invocation and Result for the center, the expansion interface Protocol, Invoker and Exporter. Protocol is a service domain, which is the main entrance features Invoker exposure and reference, which is responsible for the life cycle management of Invoker. Invoker is the entity domain, it is Dubbo's core model, other models are scrambling to rely on it, or convert it, it represents an executable body, can invoke call to initiate it, it is possible that a local implementation, it may is a remote implementation is also possible to achieve a cluster.
  8. Information exchange layer (Exchange): encapsulation request response mode, synchronous asynchronous transfer, to the center Request and Response, extended interface Exchanger, ExchangeChannel, ExchangeClient and ExchangeServer.
  9. Network transport layer (Transport): abstract mina and netty a unified interface for Message-centric, expansion interfaces for the Channel, Transporter, Client, Server and Codec.
  10. Data sequence layer (Serialize): some tools may be multiplexed, expansion interface Serialization, ObjectInput, ObjectOutput and ThreadPool.

Agreement details:
https://blog.csdn.net/yuer2008200820008/article/details/80208025

Dubbo features

1. transparent remote method invocation, the same as calling a local method call remote methods, only a simple configuration, without any intrusion API.
2. Soft load balancing and fault tolerance mechanisms, including the network can replace F5 hardware load balancer, lower costs, reduce single point.
3. Auto Service registration and discovery, no longer need to write the dead service provider address, IP address registry based query interface name service provider, and can be smoothly add or remove services provider.

Dubbo all-spring arrangement, transparent access applications, application no intrusion API, the configuration can be simply loaded with Spring Dubbo, Dubbo Spring-loaded extension Schema.

Dubbo operation

Dubbo application only concerned about service providers and service consumers.
Here Insert Picture Description

Service call process is as follows:

  1. Service Provider (Provider) services to publish service registry;
  2. Service consumer (Consumer) from the service registry (Registry) subscription service;
  3. The service consumer calls the services available has been registered;
  4. Monitoring center (Monitor) recording the calling procedure.

During the description of a role:

  1. Provider: Service Provider exposure.
  2. Consumer: the service consumer calls the remote service.
  3. Registry: registration and registry services found, you can choose Zookeeper as a registration center, remote storage service Provider registration information. Sign up and remain long connection between the center and the provider and the caller can get the service Provider change of release, and the latest list of services pushed to Consumer
  4. Monitor: statistical services of call times to reconcile the monitoring center call time.
  5. Container: run container services.

Specific process is:

  1. Responsible for starting the service container, load, run service provider.
  2. Service provider when you start, registration services they provided to the registry.
  3. Consumer services at startup, you need to subscribe to the service registry.
  4. Registry return address list service providers to consumers, if there is a change, the registry will be based on long push to change the data connection to the consumer.
  5. Service consumers, providers from the list of addresses, soft load balancing algorithm, choose a provider call, if the call fails, then select another call.
  6. Service consumers and providers, in memory of the cumulative number of calls and call time, time sent once per minute statistical data to the monitoring center.

Why we need to monitor the center of it?

  1. Error when you can discover.
  2. When the service is not enough (such as big promotion), you can see the number of calls and other services, estimated the number of machines needed to support the service? When the machine plus?

Original link: https: //blog.csdn.net/baoyu_G/article/details/82314435

Dubbo registry

When using Dubbo, generally in the registry on the Zookeeper (of course there Redis, Multicast, Simple do registry), and is used to manage the cluster Zookeeper, Zookeeper not separate a single server, but the entire cluster each machines together to form Zookeeper, which have a leader to coordinate server to other servers.

Service provider, the caller is in Zookeeper management cluster, as shown below:
Here Insert Picture Description

Process is as follows:

  1. Service Provider startup: URL address to write their own directory under /dubbo/com.foo.BarService/providers
  2. When consumers start service: subscription provider directory at /dubbo/com.foo.BarService/providers URL address. And write their own URL address directory under /dubbo/com.foo.BarService/consumers
  3. Monitoring Center on startup: subscribe to all providers and consumers URL addresses under /dubbo/com.foo.BarService directory.

Note: Registration and subscription are URL address information.

Zookeeper (registry), you get:

  1. When the provider of a power failure or other abnormal shutdown, the registry can automatically delete information provider
  2. When the registry to restart, can automatically restore the registration data, as well as subscription request
  3. When the session expires, it can automatically restore the registration data, as well as subscription request

Zookeeper subscription

zk + clients using Event Notification way to pull subscription.

  1. The first time a client linked to the registry, will get all the data in the corresponding directory. And on the subscription node registered a watcher, the client will maintain a long link with tcp registry.
  2. When follow-up data each node has changed, the registry will take the initiative to inform the client according watcher callback, this is the event notification.
  3. When the client receiving the notice, the total amount will automatically pull data at a corresponding node, which is the client pull.

Zk Each node has a version number, when the data changes node, the node corresponding version number will change, and this time will trigger watcher notify the corresponding client.

Zookeeper cache

Basically each scene, the cache of the role is to use space for time. Imagine if every time remote calls to be pulled from the full amount registry again the list of services, then the registration centers to shore up the main so much traffic.

So dubbo the registry will caching mechanism, when the consumer or a treatment center to get the registration information will be cached locally. This is the reason why the registry hung parties can also call

Why choose Zookeeper?

Zookeeper data model is very simple, there are a series of data nodes are called ZNode composition, with traditional disk file system is different, zk the full amount of data stored in memory, can be described as high performance, and support for clustering, high availability can be described, In addition to support event listeners. These characteristics determine zk particularly suitable as a registration center (data publish / subscribe).

Transfer: https: //www.cnblogs.com/qtlq/p/11296578.html

Dubbo service exposure process

Probably process:

  1. Exposure to local services
  2. Exposure to remote services
  3. Start netty
  4. Connection zookeeper
  5. Registration to the zookeeper
  6. Listening zookeeper

What is the local and remote exposed exposure, the difference between them?

In dubbo we may be both a service Provider, it is Consumer, therefore the presence of his own calling service. If we go to access the network, it is naturally far away, it is exposed to the local design services to solve this problem.

Local exposure: exposure is in the JVM, no network communication.
Remote exposure: exposure is the information ip, port, etc. to a remote client, network communication needs when you call.

Exposure process:

  1. I began to call the export () method to determine whether there is a delay exposure.
  2. Not then start calling doExport method, exposure.
  3. doExport method to perform a series of inspection method, and then call doExportUrls method.
  4. Call proxyFactory.getInvoker () method to get Invoker, and services will be implemented as a package Invoker, Invoker encapsulates service implementation class.
  5. Invoker packaged into the Exporter, and cached, the cache URL Invoker used as key. This is the middle of Dubbo need to use protocol (Protocol), is the need to use, if the agreement is Dubbo, is DubboProtocol, it will open socket listening service, and receives various requests sent by the client, the details of the communication by the dubbo achieve their own. That exposure process, will start Netty.
  6. Start Netty, the server Server starts listening port.
  7. When the request came, upon request, information generated key, to cache lookup Exporter, we found the Invoker, you can complete the call.

Here Insert Picture Description

Links: HTTPS: //www.jianshu.com/p/292fcdcfe41e
https://www.jianshu.com/p/dcfe426e9cd7

What is netty

Netty is the role of network communications, it is the NIO.

Netty is a high performance, asynchronous event-driven NIO framework, API JAVA NIO based implementation provided. It provides support for TCP, UDP and file transfer. As a NIO asynchronous frame, all IO operations are asynchronous non-blocking Netty, Future-Listener through the mechanism, the user can easily acquire or active operation results obtained IO notification mechanism.
Netty binding Selector (provided by the java NIO) and Reactor pattern design efficient threading model.

Start Netty is equivalent to communicate with other network server, the server itself is also Server Client. As Server, the server may receive the connection request of the Client, and the manner in NIO IO requests connection.

For details, see: https: //www.cnblogs.com/technologykai/articles/10910878.html

Dubbo service referral process

The first is the process of subscription services:

  1. Depending on the configuration at the time of subscription, ready to generate ReferenceBean (generation service instance needs to be called).
  2. Create a service agent, call ReferenceConfig.createProxy () method.
  3. createProxy () method call will refer RegistryProtocol's () method
  4. refer () methods need to use RegistryDirectory. RegistryDirectory by RegistryDirectory.subscribeUrl () subscribe to the service node information and watch Zookeeper change, thus achieving automatic service discovery.
  5. After you create a successful service agent, will produce a list of invoker

Then the referral process services:

  1. Get and return invoker list from the memory cache when the service reference.
  2. Router brush selected by a subset invoker invoker comply with routing rules from the list.
  3. LoadBalance by selecting the ultimate goal invoker, initiate a service call.
  4. If the call fails, when each retry, through LoadBalance, soft load algorithm to find the right service, select a Invoker call.
  5. Call succeeds, the execution method.

Dubbo load balancing strategy

Dubbo load balancing There are four strategies:

  1. Random LoadBalance (default): Random policy. Set the weight according to the probability, uniform, and can dynamically adjust the weights of the weight provider, in accordance with the load balancing weights, the greater the weight the higher the flow rate distribution, generally use this default on it.
  2. RoundRobin LoadBalance: polling policy. The default is uniformly hit the respective flow up machine, according to convention right after re-set the polling rate. But if the performance of each machine is different, easily lead to poor performance of the machine load is too high, there is a relatively slow implementation of the service provider piled request. So in this case need to adjust the weights so that the machine carrying heavy weights poor performance of smaller, less traffic number. .
  3. LeastActive LoadBalance: Minimum number of active calls. This is the automatic perception that, if a worse performance machine, so the slower the response, the less active, inactive at this time will give the poor performance of the machine fewer requests. If the number of active each provider are the same, a random selection. An active counter in which each service provider defenders, so if this value is smaller the velocity of the current service provider processed quickly, so the routing on the choice of the minimum activity machines.
  4. ConsistentHash LoadBalance: consistency Hash strategy. The service provider is provided ip hash ring, can ensure the same parameters of the request is always sent to the same provider, the provider when a station linked to the original request sent to the provider, based on the virtual node, in equal shares to other provider, will not cause dramatic changes.
  • Hash load balancing policy consistency principle?

Dubbo fault-tolerant strategy

When the service consumer calls the service provider fails, Dubbo there are several fault-tolerance policies:

  1. failover cluster failure retry : failure automatic switching, automatic retry the other machine, the default is that this is common in the read operation.
  2. failfast cluster fail-fast : a call failed on the error immediately, which is invoked only once, common in the write operation.
  3. failsafe cluster fail safe : ignored when an exception occurs. Commonly used in the interface calls unimportant, such as logging.
  4. failback cluster failure automatic recovery : automatic recording request failed background, and in accordance with certain late timing of the retransmission policy, more suitable for the write message queue (message notification) of this.
  5. forking cluster invoked in parallel : Parallel call multiple provider, as long as a successful return immediately.
  6. broadcacst cluster broadcast call : call one by one all the provider.

Dubbo implement RPC examples

For more examples see the original.
Reprinted from: https: //blog.csdn.net/houshaolin/article/details/76408399

  • Mock can simulate a variety of service calls anomalies in the test, it is also used to implement the service degradation.
Published 67 original articles · won praise 32 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_43751710/article/details/104664150