To the point

This is the 31st day of my participation in the August More Text Challenge

The RPC service

What is the RPC?

Remote Procedure Call (RPC) refers to Remote Procedure Call, which is an inter-process communication method. It is a technical idea, rather than a specification. It allows a program to call a procedure or function in another address space, usually on another machine on a shared network, without the programmer explicitly coding the details of this remote call. That is, programmers write essentially the same calling code whether they call local or remote functions.

The basic principle of RPC, RPC two core modules: communication, serialization.

A complete RPC call flow (synchronous call, asynchronous call separately) is as follows:

  • 1) Service consumer (Client) invocation invokes the service in a local invocation mode;

  • 2) After receiving the call, the Client stub is responsible for assembling methods and parameters into a message body capable of network transmission;

  • 3) The client stub finds the service address and sends the message to the server.

  • 4) The Server Stub decodes the message after receiving it.

  • 5) The Server Stub invokes the local service according to the decoding result;

  • 6) The local service executes and returns the result to the server stub;

  • 7) The Server Stub packages the returned result into a message and sends it to the consumer;

  • 8) The Client stub receives the message and decodes it.

  • 9) The service consumer gets the final result.

The goal of the RPC framework is to encapsulate steps 2 through 8 so that these details are transparent and invisible to the user

Netty communication principle

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high-performance protocol servers and clients. It greatly simplifies and simplifies network programming such as TCP and UDP socket servers.

BIO: (Blocking I/o)

NIO (Non-Blocking IO)

  • A Selector is usually called a Selector, it can also be translated as a multiplexer,

  • Connect ready, Accept ready, Read ready, Write ready

Netty basic principles:

Dubbo

Basic introduction to

Apache Dubbo (Incubating) is a high-performance, lightweight, open source Java RPC framework that provides three core capabilities: interface-oriented remote method calls, intelligent fault tolerance and load balancing, and automatic service registration and discovery. Liverpoolfc.tv: dubbo.apache.org/

The framework design

  • Config configuration layer: external configuration interface, centering on ServiceConfig and ReferenceConfig, can directly initialize configuration classes, or generate configuration classes through Spring configuration parsing
  • Proxy ServiceProxy layer: transparent proxy of service interfaces. The client Stub and server Skeleton of the service are generated. The extension interface is ProxyFactory.
  • Registry layer: encapsulates the registration and discovery of service addresses, centering on service URLS and extending interfaces as RegistryFactory, Registry, and RegistryService
  • Cluster routing layer: encapsulates routing and load balancing of multiple providers, Bridges registries, centers on Invoker, and extends interfaces to Cluster, Directory, Router, and LoadBalance
  • Monitor monitoring layer: Monitors the number and time of RPC calls. It centers on Statistics and extends interfaces to MonitorFactory, Monitor, and MonitorService
  • Protocol Remote Invocation layer: Encapsulates RPC Invocation with Protocol, Invoker, and half interface, based on Invocation and Result
  • Exchange information exchange layer: It encapsulates the Request and Response mode, turns synchronous to asynchronous, uses Request and Response as the center, and uses exchange channel, ExchangeClient and ExchangeServer as the expansion interface
  • Transport Network transport layer: Abstract MINA and Netty as the unified interface, Message as the center, extended interfaces are Channel, Transporter, Client, Server, Codec
  • Serialize data Serialization layer: reusable tools with Serialization, ObjectInput, ObjectOutput, and ThreadPool extensions

Start parsing and loading configuration information

Services available

The service reference

The basic composition of

  • Provider: A service Provider that exposes a service. At startup, the service Provider registers its services with the registry.
  • Service Consumer: the service Consumer who invokes the remote service. When the service Consumer starts up, it subscribes to the registry for the service it needs. The service Consumer selects one provider from the provider address list and invokes it based on the soft load balancing algorithm.
  • Registry: The Registry returns a list of service provider addresses to the consumer. If there are changes, the Registry pushes the change data to the consumer based on the long connection
  • Monitor: Service consumers and providers accumulate call times and call time in memory and regularly send statistics to the monitoring center once a minute

Call Relationship Description

  • The service container is responsible for starting, loading, and running the service provider.
  • At startup, service providers register their services with the registry.
  • At startup, service consumers subscribe to the registry for the services they need.
  • The registry returns a list of service provider addresses to the consumer, and if there are changes, the registry pushes the change data to the consumer based on the long connection.
  • The service consumer, from the provider address list, selects one provider to call based on the soft load balancing algorithm. If the call fails, selects another one to call.
  • Service consumers and providers accumulate calls and call times in memory and regularly send statistics to the monitoring center every minute.

Environment set up

Windows – installing zookeeper
  • Download the zookeeper url archive.apache.org/dist/zookee…
  • Run zkserver. CMD to decompress ZooKeeper. An error message is displayed when you run zkServer. CMD for the first time
  • CFG configuration file. Copy the zoo_sample. CFG file in the conf file and rename it zoo. CFG. Note the following points: dataDir=./ Directory of the temporary data store (writable relative path) clientPort=2181 ZooKeeper port number After the zooKeeper port number is changed, start ZooKeeper again
  • Run the zkcli. CMD command to test ls / : list all nodes saved under the ZooKeeper root create -e /atguigu 123: creates an atguigu node with the value 123 get /atguigu: obtains the value of the /atguigu node
Windows – Install dubo-admin admin console
  • Dubbo itself is not a service. Zookeeper is a JAR package that allows your Java application to connect to ZooKeeper and use ZooKeeper to consume and provide services. So you don’t have to start a Dubbo service on Linux.

  • But to make it easier for users to manage and monitor the many Dubbo services, officials have provided a visual monitoring program that will not affect use even if it is not installed.

  • Download the dubbo – admin github.com/apache/incu…

  • SRC \main\resources\application.properties specifies the ZooKeeper address

  • Dubbo-admin MVN clean package -dmaven.test. skip=true

  • Dubbo-admin Java -jar dubbo-admin-0.0.1 -snapshot. jar dubbo-admin Java -jar dubbo-admin-0.0.1 -snapshot. jar

Dubbo configuration

The configuration principle

  • JVM startup -d takes precedence, allowing the user to override parameters at deployment and startup time, such as changing protocol ports at startup.

  • XML is next, and if there is a configuration in XML, the corresponding configuration item in dubo.properties is invalid.

  • Finally, as the default, the corresponding configuration item for dubo.properties takes effect only if XML is not configured, and is usually used to share common configurations, such as the application name.

Retry count

  • Failure automatically switches, and when a failure occurs, retry other servers, but retry with longer delay. Retries =”2″ can be used to set the number of retries (excluding the first).

The retry times are as follows:

<dubbo:service retries="2" /> or <dubbo:reference retries="2" or <dubbo:reference> <dubbo:method name="findFoo" retries="2" /> </dubbo:reference>Copy the code

timeout

An indeterminate intermediate state (timeout) can occur in the call due to network or server unreliability. The timeout period must be set to prevent client resources (threads) from being suspended due to timeouts.

Dubbo consumers

Global Timeout Configuration

<dubbo: Reference interface="com.foo.BarService" timeout="2000"> <dubbo: Reference interface="com.foo.BarService" timeout="2000"> <dubbo:method name="sayHello" timeout="3000" /> </dubbo:reference>Copy the code

Dubbo server

Global Timeout Configuration

<dubbo:provider interface="com.foo.BarService" timeout="2000"> <dubbo:method name="sayHello" timeout="3000" /> </dubbo:provider>Copy the code

The configuration principle

Dubbo recommends configuring as many Consumer attributes on the Provider as possible:

  1. The provider of the service knows better than the consumer of the service performance parameters, such as the timeout of the invocation, the reasonable number of retries, and so on.

  2. After the Provider is configured, if the Consumer is not configured, the Provider configuration is used as the default value for Consumer. Otherwise, the Consumer uses the global Settings on the Consumer side, which is out of the Provider’s control and often unreasonable.

Configured coverage rules:

  • The method level configuration is superior to the interface level, that is, small Scope takes precedence
  • The Consumer configuration is better than the Provider configuration.
  • Finally, the configuration values for Dubbo Hard Code (see configuration documentation)

The version number

When an interface is implemented and an incompatible upgrade occurs, the version number can be used for transition. Services with different version numbers do not reference each other.

  • You can follow these steps to perform version migration:

  • During low-stress periods, upgrade half of the providers to the new version first

  • Then upgrade all customers to the new version

  • Then upgrade the remaining half of the providers to the new version

Old version service provider configuration:

"Dubbo: service interface =" com. Foo. BarService "version =" 1.0.0 "/ >Copy the code

New version service provider configuration:

"Dubbo: service interface =" com. Foo. BarService "version =" 2.0.0 "/ >Copy the code

Old version Service Consumer Configuration:

<dubbo:reference ID ="barService" interface="com.foo. barService" version="1.0.0" />Copy the code

New version service Consumer Configuration:

<dubbo:reference ID ="barService" interface="com.foo. barService" version="2.0.0" />Copy the code

If you do not need to differentiate versions, perform the following operations:

<dubbo:reference id="barService" interface="com.foo.BarService" version="*" />
Copy the code

High availability

Zookeeper is down and directly connected to Dubbo

  • Symptom: The ZooKeeper registry is down, and the services exposed by Dubbo can be consumed.
  • Reason: Robustness

  • The breakdown of the monitoring center does not affect the use of the system, but only the loss of some sample data
  • After the database goes down, the registry can still provide a list of services query through the cache, but it cannot register new services
  • If one of the registry peer clusters fails, it will automatically switch to the other one
  • After all registries go down, service providers and service consumers can still communicate through local caches
  • The service provider is stateless. If any service provider breaks down, the service is not affected
  • When all service providers go down, the service consumer application becomes unavailable and reconnects indefinitely waiting for the service provider to recover

Configure dubbo load balancing in a cluster

Dubbo provides multiple balancing policies for cluster load balancing. The default policy is random random call.

Load Balancing Policy

Random LoadBalance

Random, set the random probability according to the weight: the probability of collision on a section is high, but the larger the adjustment amount is, the more uniform the distribution will be, and the weight will be more uniform according to the probability, which is conducive to the dynamic adjustment of the provider weight.

RoundRobin LoadBalance

Round-robin, weighted after the convention: There is a problem of slow providers accumulating requests, for example: the second machine is slow but not hung up, and gets stuck when the request is switched to the second machine, and over time all the requests get stuck to the second machine.

LeastActive LoadBalance

Minimum number of active calls, random for the same number of active calls, active count is the difference before and after the call, so that the slower provider will receive fewer requests, because the slower provider will have more difference before and after the call.

ConsistentHash LoadBalance

Consistent Hash, where requests with the same parameters are always sent to the same provider.

When a provider hangs, requests originally sent to that provider are spread over other providers based on virtual nodes without drastic changes. Algorithm is used to refer to: en.wikipedia.org/wiki/Consis…

<dubbo:parameter key=”hash.arguments” value=”0,1″ /> <dubbo:parameter key=”hash.arguments” value=”0,1″ />

By default, 160 virtual nodes are used. To change the number, set <dubbo:parameter key=”hash.nodes” value=”320″ />**

Integration with Hystrix, service circuit breaker and downgrade processing

Service degradation

What is service degradation?

When the server pressure increases dramatically, according to the actual business situation and traffic, some services and pages are strategically not processed or processed in a simple way, so as to release server resources to ensure the normal or efficient operation of core transactions.

You can use the service degradation function to temporarily mask a failed non-critical service and define the return policy after the degradation.

Write dynamic configuration override rules to the registry:
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
Registry registry = registryFactory.getRegistry(URL.valueOf(Zookeeper: / / 10.20.153.10: "2181"));
registry.register(URL.valueOf("Override: / / 0.0.0.0 / com. Foo BarService? category=configurators&dynamic=false&application=foo&mock=force:return+null"));
Copy the code
Among them:
  • Mock =force:return+ NULL mock=force:return+ NULL Used to mask the impact on the caller when the unimportant service is unavailable.

  • Mock =fail:return+null mock=fail: Return +null = mock=fail: Return +null = mock=fail To tolerate the impact on the caller when the unimportant service is unstable.

The cluster tolerance

When a cluster invocation fails, Dubbo provides multiple fault tolerance schemes. By default, failover retry is used.

Cluster fault tolerant mode

Failover Cluster

Failure to switch automatically, when failure occurs, retry other servers. Typically used for read operations, but retries introduce longer delays. Retries =”2″ can be used to set the number of retries (excluding the first).

The retry times are as follows:
<dubbo:service retries="2" /> or <dubbo:reference retries="2" or <dubbo:reference> <dubbo:method name="findFoo" retries="2" /> </dubbo:reference>Copy the code
Failfast fast

Fast failure, only one call, failure immediately error. Typically used for non-idempotent writes, such as new records.

Failsafe Safe

Fail-safe, ignore exceptions when they occur. It is used to write audit logs.

Failback over

The system automatically recovers the failure, records the failure request in the background, and periodically resends the failure request. Typically used for message notification operations.

Forking Cluster

Call multiple servers in parallel and return if one succeeds. It is usually used for read operations that require high real-time performance but waste more service resources. The maximum parallel number can be set by forks=”2″.

Broadcast Cluster

Broadcast calls all providers, one by one, and an error is reported on any one [2]. Typically used to notify all providers to update local resource information such as caches or logs.

Cluster Mode Configuration

Follow the following example to configure the cluster pattern on the service provider and consumer side

<dubbo:service cluster="failsafe" /> <dubbo:reference cluster="failsafe" />Copy the code

Integrated hystrix

Hystrix aims to provide greater fault tolerance for delays and failures by controlling those nodes that access remote systems, services, and third-party libraries. Hystrix has thread and signal isolation with fallback mechanism and circuit breaker functionality, request caching and request packaging, and monitoring and configuration

Put the spring – the cloud – starter – netflix – hystrix

Spring Boot officially provides hystrix integration by adding dependencies directly to pum.xml:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> < version > 1.4.4. RELEASE < / version > < / dependency >Copy the code

Then add @enablehystrix to the Application class to EnableHystrix starter:

@SpringBootApplication
@EnableHystrix
public class ProviderApplication {
Copy the code

Configure the Provider client

Add the @hystrixCommand configuration to Dubbo’s Provider so that the call goes through the Hystrix proxy.

@ Service (version = "1.0.0")
public class HelloServiceImpl implements HelloService {
    @HystrixCommand(commandProperties = { @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") })
    @Override
    public String sayHello(String name) {
        // System.out.println("async provider received: " + name);
        // return "annotation: hello, " + name;
        throw new RuntimeException("Exception to show hystrix enabled."); }}Copy the code

Configure the Consumer end

For the Consumer side, you can add a layer of method calls and configure @hystrixCommand on method. When the call fails, it goes to a call with fallbackMethod = “reliable”.

@ the Reference (version = "1.0.0")
private HelloService demoService;

@HystrixCommand(fallbackMethod = "reliable")
public String doSayHello(String name) {
   return demoService.sayHello(name);
}
public String reliable(String name) {
   return "hystrix fallback value";
}
Copy the code

Find the registry, service provider, service consumer (Boot loader – source code analysis)

Dubbo custom tag implementation.

After Dubbo loads the configuration file through Spring, it triggers the registry, service provider, and service consumer to perform the relevant functions as designed by Dubbo. The alleged execution-related functions are as follows:

  • The registry starts, listening for the message provider’s registration service and the receiving message consumer’s service subscription (service registration and discovery mechanism).
  • Service providers register services with the registry.
  • The service consumer subscribes to the registry.

Next, from the perspective of using Dubbo, start with configuration files:

The general configuration of the Dubbo service provider is as follows:

<! Dubbo: Application name="uop" owner=" uCE "/> <! - using zookeeper registry service address exposure - > < dubbo: registry protocol = "zookeeper" address = "zookeeper: / / 192.168 xx. Xx: 2181? Backup = 192.168 xx. Xx, 2182192168 xx. Xx: 2183 "/ > <! Dubbox introduces Kryo and FST, two efficient Java serialization implementations, <dubbo:protocol name="dubbo" serialization="kryo" port="20990" /> <! Dubbo :provider timeout="5000" threadPool ="fixed" Threads ="100" Accepts ="1000" token="true"/> <! A service can be exposed with multiple protocols, and a service can be registered with multiple registries. Configure as many Consumer properties as possible on the Provider. Let the Provider implementer the start thinking about the Provider of service features, service quality problems - > < dubbo: service interface = "com. Yingjun. Dubbox. API. UserService" ref="userService" />Copy the code

Dubbo: Application, Dubbo: Registry, Dubbo: Protocol, Dubbo :provider, and Dubbo: Service define the dubbo application name, registry, protocol, and service provider parameter defaults Values, service providers, what are the implementation principles behind these configurations? How does it start and function?

Spring custom tag implementation principle

Dubbo custom tag and namespace implementation code in the module dubbo-config, the implementation principle will be described in detail below.

DubboNamespaceHandler

Implement dubbo namespace handler, its full path: com. Alibaba. Dubbo. Config. Spring. The schema. DubboNamespaceHandler, its source code to achieve the following:

public class DubboNamespaceHandler extends NamespaceHandlerSupport {

    static {
        Version.checkDuplicate(DubboNamespaceHandler.class);
    }

   @Override
    public void init(a) {
        registerBeanDefinitionParser("application".new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        registerBeanDefinitionParser("module".new DubboBeanDefinitionParser(ModuleConfig.class, true));
        registerBeanDefinitionParser("registry".new DubboBeanDefinitionParser(RegistryConfig.class, true));
        registerBeanDefinitionParser("monitor".new DubboBeanDefinitionParser(MonitorConfig.class, true));
        registerBeanDefinitionParser("provider".new DubboBeanDefinitionParser(ProviderConfig.class, true));
        registerBeanDefinitionParser("consumer".new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        registerBeanDefinitionParser("protocol".new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        registerBeanDefinitionParser("service".new DubboBeanDefinitionParser(ServiceBean.class, true));
        registerBeanDefinitionParser("reference".new DubboBeanDefinitionParser(ReferenceBean.class, false));
        registerBeanDefinitionParser("annotation".newAnnotationBeanDefinitionParser()); }}Copy the code

As can be seen from this, dubbo’s custom tags mainly include: Application, Module, Registry, Monitor, Provider, Consumer, Protocol, Service, Reference, Annotation, The specific analytical implementation class is mainly composed of DubboBeanDefinitionParser (based on the XML configuration files), AnnotationBeanDefinitionParser (based on annotations), below will be the realization of the detailed analysis of the above two class.

Define the dubo.xsd file

Dubo. XSD, Spring. handlers, and spring.schemas are defined in SRC /main/resouce/ meta-INF under dubo-config-spring module respectively.

ElasticJob (Spring) creates new namespaces and tags for ElasticJob (Spring)

Bean parsing mechanism

It should be noted that Spirng’s configuration supports XML configuration files and annotations, so Dubbo also supports two configurations, XML and annotations.