wedge

My first contact with Dubbo was in my first company after graduation. At that time, Ali stopped maintaining Dubbo, but there were still quite a few companies using it. Dubbox was developed by Dangdang. After several years of Dubbo’s silence, Ali started to upgrade Dubbo. On January 8, 2018, Dubbo version 2.6.0 was released. Ali later made Dubbo an open source contribution to Apache, incubator-Dubbo, which is now Apache’s top-level project.

Introduction to Dubbo

Distributed system

Evolution of Web applications

With the development of the Internet, the scale of Web applications continues to expand, and its development has gone through the following stages

    1. A single application architecture, a single application, deploys all functions together.
    1. Vertical application architecture, breaking down applications into discrete applications
    1. Distributed service architecture, when more and more vertical applications, the interaction between applications is inevitable, the core business is extracted as an independent service, gradually forming a stable service center, so that front-end applications can respond to the changing market demand more quickly. At this point, the distributed Services framework (RPC) for business reuse and integration is key.

Flow rack

    1. In the mobile computing architecture, when there are more and more services, problems such as capacity assessment and waste of small service resources gradually emerge. At this time, a scheduling center should be added to manage cluster capacity in real time based on access pressure and improve cluster utilization. At this point, a resource scheduling and Governance center (SOA) for improving machine utilization is key.

What is a distributed system

A distributed system is a collection of several systems that operate independently, making users use it as if they were using one system.

What is the Dubbo

A high-performance, lightweight open source Java RPC distributed services framework. It provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery. RPC generally refers to remote procedure calls. RPC stands for Remote Procedure Call

What can Dubbo do

  • As there are more and more services, service URL configuration management becomes very difficult and the single point of pressure on the F5 hardware load balancer increases.
  • As the dependencies between services become so complex that it is not even clear which application should be launched before which, the architect cannot fully describe the application’s architectural relationships.
  • As more and more services are deployed, the capacity problem of the service is exposed. How much machine support does the service require? When should you add machines?

Dubbo architecture

The module describe
Provider Service provider
Consumer Service consumer
Registry Service Registry
Monitor A monitoring center that collects statistics on service invocation times and invocation time
Container Service run container

Scheduling process

  • 0. Start: The service container is responsible for starting, loading, and running the service provider.

  • 1. Register: Service providers register their services with the registry at startup time.

  • 2. Subscribe: At startup, service consumers subscribe to the registry for the services they need.

  • 3. Notify: The registry returns the service provider address list to the consumer. If there is a change, the registry will push the change data to the consumer based on the long connection.

  • 4. Invoke: Service consumer selects one provider to invoke based on soft load balancing algorithm from the provider address list. If the invocation fails, it selects another one to invoke.

  • 5. Count: accumulative call times and call time of service consumers and providers in memory, and regularly send statistical data to the monitoring center once a minute.

Dubbo integration SpringBoot entry

To use Zookeeper as the registry, install and start Zookeeper

The project structure

Dubo-interface that both the service provider and the consumer depend on

package com.wuzhu.service;

public interface HelloService {
   public String sayHello(String name);
}
Copy the code

Service Provider (Dubo-provider)

Dubbo – provider of pom. The XML
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < artifactId > dubbo - provider < / artifactId > < groupId > com. Wuzhu < / groupId > < version > 1.0 - the SNAPSHOT < / version > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.0. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! Wuzhu </groupId> <artifactId> dubo-interface </artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency > <! <dependency> <groupId>com.alibaba.spring. Boot </groupId> < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 2.0.0 < / version > < / dependency > <! > <dependency> <groupId>com.101tec</groupId> <artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
The dubo-provider configuration file application.yml

Configure the service provider’s port, and the corresponding Dubbo configuration, registry configuration

server:
 port: 8847
spring:
 dubbo:
   application:
     name: dubbo-provider
   registry: zookeeper://localhost:2181
Copy the code

If you do not use the Zookeeper registry, use point-to-point direct connection and set Registry as follows

server:
 port: 8847
spring:
 dubbo:
   application:
     name: dubbo-provider
   registry:
     register: false
Copy the code
Implementing an interface

In the Service provider implementation interface, note that the @service annotation used here is Dubbo, not Spring.

package com.wuzhu.service; import com.alibaba.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @Service @Component public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; }}Copy the code
Start the class
package com.wuzhu; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); }}Copy the code

Service consumer

Dubbo – consumer pom. XML

Both the consumer and provider rely on the Dubbo-interface. The Dubbo-provider is responsible for implementation, and the Dubbo-consumer is used for remote invocation.

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Wuzhu < / groupId > < artifactId > dubbo - consumer < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.0. RELEASE < / version > < relativePath / > < / parent > <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > < Java version > 1.8 < / Java version > </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! <dependency> <groupId>com.wuzhu</groupId> <artifactId> dubo-interface </artifactId> < version > 1.0 - the SNAPSHOT < / version > < / dependency > <! <dependency> <groupId>com.alibaba.spring. Boot </groupId> < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 2.0.0 < / version > < / dependency > <! > <dependency> <groupId>com.101tec</groupId> <artifactId> <version>0.10</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code
Start the class
package com.wuzhu; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); }}Copy the code
Consumer configuration application.yml

Consumers also have to register with the registry

server:
  port: 8848
spring:
  dubbo:
    application:
      name: dubbo-consumer
    registry: zookeeper://localhost:2181
Copy the code
The service call

Use @Reference annotations

package com.wuzhu.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.wuzhu.service.HelloService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Reference private HelloService helloService; @GetMapping("/hello") public String hello(){ return helloService.sayHello("Dubbo"); }}Copy the code

If you do not use a registry but use point-to-point connection, you can add a parameter URL to specify the URL of the service provider. Remove the Registry configuration from the YML file

    @Reference(url = "dubbo://localhost:8847")
    private HelloService helloService;
Copy the code

Configuration article

Startup detection

Dubbo checks whether dependent services are available at startup. If not, an exception will be thrown to prevent Spring initialization from being completed so that problems can be detected early when it goes online. By default, check=”true”. Check can be turned off by checking =”false”. Turn off startup checking for all services (no provider reported an error) :

# # to shut down a service startup check dubbo.reference.com.wuzhu.HelloService.check=false # # close all service startup check, Check =false Dubo.consumer.check =false Closes dubo.registry. Check =false when the registry is startedCopy the code

Configuring coverage Relationships

  • Method first, interface second, global configuration second.
  • If the level is the same, the consumer takes precedence over the provider.

The official document shows the relationship from high to low of configuring overrides, taking setting timeout as an example

Load balancing

Dubbo offers a variety of balancing strategies:

  • The Random Random,
  • RoundRobin polling,
  • LeastActive Minimum number of active calls, random for the same active number
  • ConsistentHash: Requests with the same parameters are always sent to the same provider.

The default value is random

Service degradation

What is service degradation

Service degradation refers to the strategic degradation of some services and pages according to the current service status and traffic when the server pressure increases dramatically, so as to release server resources and ensure the normal operation of core tasks. For example, if three services are running on a server, one of them is abnormal, causing a large number of threads to accumulate and occupying server resources. As a result, the other two services cannot run properly, you can take measures to degrade the abnormal services to ensure the normal running of other services.

Two services offered by Dubbo were downgraded
  • Mock =force:return+ NULL; 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) returns null when the invocation fails. To tolerate the impact on the caller when the unimportant service is unstable.

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

The cluster tolerance

Dubbo provides several fault-tolerant schemes in the event of a cluster call failure:

  • Failover Cluster (default mode) Failover is performed automatically. If a 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).
<dubbo:service retries="2" /> or <dubbo:reference retries="2" /> or // Sets the number of retries for a method. <dubbo:reference> <dubbo:method name="findFoo" retries="2" /> </dubbo:reference>Copy the code
  • Failfast Cluster

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

  • Failsafe Cluster

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

  • Failback Cluster

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

The broadcast calls all the providers, one by one, and an error is reported on any one. Typically used to notify all providers to update local resource information such as caches or logs.

Configure cluster fault tolerance

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

Support for multiple protocols

Dubbo supports multiple protocols such as Dubbo, RMI, Thrift, Hessain, HTTP, and Redis. It allows multiple protocols to be configured and supports different protocols on different services or multiple protocols on the same service.

<! -- <dubbo:protocol name="dubbo" port="20880" /> <dubbo:protocol name=" rmI "port="1099" /> <! - use of dubbo agreement exposed service - > < dubbo: service interface = "com. Alibaba. Hello. API. HelloService" version = "1.0.0" ref = "HelloService" protocol="dubbo" /> <! - use the rmi protocol exposed services - > < dubbo: service interface = "com. Alibaba. Hello. API. DemoService" version = "1.0.0" ref = "DemoService" protocol="rmi" /> <! -- <dubbo:protocol name="dubbo" port="20880" /> <dubbo:protocol name="hessian" port="8080" /> <! - using multiple protocols to expose services - > < dubbo: service id = "helloService" interface = "com. Alibaba. Hello. API. HelloService" version = "1.0.0" protocol="dubbo,hessian" />Copy the code

serialization

Dubbo supports hession, Java binary serialization, JSON, SOAP text serialization and other serialization protocols. Default Hessian serialization protocol.

Concurrency control

Concurrency control in Dubbo can be implemented by configuring the Executes parameter and actives of the Service for example: limit each method of HelloService to 10 concurrent server executions (or pool threads) :

<dubbo:service interface="com.wuzhu.HelloService" executes="10" />
Copy the code

Limit the number of concurrent execution (or connection occupancy) requests per client to no more than 10 per method of HelloService:

<dubbo:service interface="com.wuzhu.HelloService" actives="10" />
Copy the code

Or Dubbo’s @service annotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Inherited
public @interfaceService { Class<? > interfaceClass()default void.class; .int executes(a) default 0; .int actives(a) default 0;
    }
Copy the code

The end of the

Compared with SpringCloud, which is popular in recent years, Dubbo pays more attention to RPC framework of service governance. It uses THE NIO framework of Netty, transmits the underlying TCP protocol, and implements RPC serialized by Hession. SpringCloud, on the other hand, is a microservices system solution consisting of multiple components. Dubbo official documents from the introduction to configuration, and then to the principle of a more comprehensive. I suggest you look it up when you study.

Resources: Official documentation