Dubbo Note Sorting

Dubbo

1. Overview of Apache Dubbo

1.1 Introduction

Apache Dubbo is a high performance RPC framework, formerly Alibaba Open Source, a lightweight open source RPC framework that seamlessly integrates with the Spring framework, which Alibaba donated to the Apache Foundation in 2018

What is RPC?

RPC is called remote procedure call, which is a remote procedure call, such as two meters A and B. An application is deployed on server A and an application is deployed on server B. An application on server A wants to call the methods provided by the application on server B. Because the two applications are not in one memory space, they cannot be called directly. A network is required to express the semantics of the call and to propagate the data of the call.

It is important to note that RPC is not a specific technology, but refers to the entire network remote call process.

RPC is a generalized concept. Strictly speaking, all means of remote procedure call fall into the category of RPC. Each development language has its own RPC framework. There are many RPC frameworks in Java, widely used RMI, Hessian, Dubbo, etc.

Dubbo official website: http://dubbo.apache.org

Dubbo provides three core capabilities: Interface-oriented remote method calls, smart fault tolerance and load balancing, and automatic service registration and discovery.

1.2 Dubbo Architecture

The Dubbo architecture diagram is as follows:

Node role description:

nodeRole Name
ProviderService Providers Exposing Services
ConsumerConsumer calling remote service
RegistryRegistry for Service Registration and Discovery
MonitorMonitoring Center for Counting Service Calls and Time
ContainerService Run Container

Dashed line is asynchronous access, solid line is synchronous access, blue dashed line: functions completed at startup completion, functions executed during the running of the red dashed line (solid line) program

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

1. Service providers register their services with the registry at startup

2. Service consumers subscribe to their own services from the registry at startup

3. The registry returns a list of service provider addresses to the consumer, and if there is a change, the registry will push the change data to the consumer based on a long connection

4. Service consumers, from the list of provider addresses, select a provider to make a call based on the Soft Load Balancing algorithm, and if the call fails, choose another call.

5. Service consumers and providers, cumulative calls in memory and call times, regularly send statistics to the monitoring center every minute

2. Service Registry Zookeeper

As you can see from the previous Doubbo schema diagram, the Registry (Service Registry) plays a crucial role in this, and Dubbo officially recommends Zookeeper as the Service Registry

2.1 Introduction to Zookeeper

Zookeeper, a subproject of Apache Hadoop, is a tree-based directory service that supports change pushing. It is suitable for use as a Dubbo service registry with high industrial intensity and is recommended for use in production environments.

To understand Zookeeper's tree directory service, let's first look at the computer file system

My computer can be divided into multiple drive letters (e.g. C,D,E, etc.), several directories can be created under each drive letter. Files can be created under each directory, or subdirectories can be created, which ultimately form a tree structure. With this tree structure of directories, we can store files in different classes for later searching. And each file on the disk has a unique access path. For example:

C: \Windows\kkb\hello.txt.

Zookeeper Tree Directory Service:

Process description:

  • When the service provider starts: to/dubbo/com. Foo. Write your own URL address in the BarService/providers directory
  • When service consumer starts: subscription/dubbo/com. Foo. The URL address of the provider in the BarService/providers directory and to/dubbo/com. Foo. Write your own URL address in the BarService/consumer directory
  • When Monitoring Center starts: Subscription/dubbo/com. Foo. URL addresses for all providers and consumers in the BarService directory

2.2 Install Zookeeper

Download address: http://archive.apache.org/dist/zookeeper/

Trial version 3.4 of the course. 6. Compressed files are available after download

Installation steps:

Step 0: Install JDK (omitted)

Step 1: Upload the package to the Linux virtual machine

Step 3: Decompress the package: tar-zxvf compressed package name

Step 4: Enter the Zookeeper directory and create the data directory: mkdir data

Step 5: Enter the conf directory and place zoo_sample.cfg was renamed zoo.cfg mv zoo_sample.cfg zoo.cfg

Step 6: Open zoo.cfg file, modify the data property: dataDir=/your own file path/zookeeper-3.4.6/data

2.3 Start and stop Zookeeper

Enter the bin directory of Zookeeper and start the service command. / zkServer.sh start

Stop service command. / zkServer.sh stop

View service status. / zkServer.sh status

Client connections. / zkCli.sh

3.Dubbo Quick Start (Integrated SpringBoot)

As an RPC framework, the core function of Dubbo is to implement remote calls across networks. This section is to create two applications, one as a provider of services and one as a consumer of services. Implement a method for service consumers to remotely invoke service providers through Dubbo.

3.1 Service Provider Development

This article builds on previous SpringBoot travel projects and integrates Dubbo

Development steps:

(1) Create maven project, dubbo-travel-pro, import dependencies in pom.xml file

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</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-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--mybatis pulus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.11</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.apache.curator</groupId>-->
<!--            <artifactId>curator-framework</artifactId>-->
<!--            <version>5.1.0</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

(2) Configure application.yml file

Focus on dubbo configuration:

application.name: Current application name used by the registry to calculate dependencies between applications. Note: Consumer and provider names cannot be the same

registry.address: Connect to the registration service center Zookeeper, IP is the IP address of the server where Zookeeper resides

Protocol:Registration protocol and port

scan: scan the specified package, and classes with @Service annotations provided by Dubbo will be published as services

(3) Create service interfaces:

(4) Create service implementation classes:

Note the @Service comment here, you must use the comment provided by Dubbo

(5) Start the service:

After starting the project, in Zookeeper. / zkCli connects to the client, ls / Look at the currently registered service and see that there are more Dubbo s

3.2 Service Consumer Development

Development steps:
(1) Create a maven project (packaged in war) dubbo-travel-con with the same pom.xml configuration as the above service provider

(2) Writing Controller

Note: Injecting Service s in Controller uses the @Reference annotation provided by Dubbo

(3) Modify the application.yml file

Note that registration is not required here, as consumers only need to subscribe to the service

4.Dubbo Management Console

When we develop, we need to know which services are registered with the Zookeeper Registry and which consumers are consuming them. We can do this by deploying a management center. In fact, the management center is a web application, which can be deployed to tomcat.

4.1 Installation

Installation steps:

(1) Copy the dubbo-admin-2.6.0.war file from the data to tomcat's webapps directory
(2) Start tomcat, the war file will be automatically decompressed
(3) Modify the dubbo.properties file under WEB-INF, noting that the value corresponding to dubbo.registry.address needs to correspond to the ip address and port number dubbo.registry.address=zookeeper://192.168.134.129:2181 dubbo.admin.root.password=rootdubbo.admin.guest.password=gues

4.2 Access

Operation steps:
(1) Access http://localhost:8080/dubbo-admin-2.6.0/, enter user name (root) and password (root)

(2) Start the Service Provider Project and the Service Consumer Project to see the corresponding information




5. Load Balancing

Load Balance: The idea is to distribute requests across multiple units of operation for execution to accomplish work together.
In cluster load balancing, Dubbo provides a variety of balancing strategies (including random, polling, minimum number of active calls, consistent Hash), defaulting to random random random calls.
Configure the load balancing policy, either on the service provider side or on the service consumer side, as follows

  @Controller
  @RequestMapping("/demo")
  public class HelloController {
    //Configuring load balancing strategies on the side of service consumers
    @Reference(check = false,loadbalance = "random")
    private HelloService helloService;
    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
      //Remote Call
      String result = helloService.sayHello(name);
      System.out.println(result);
      return result;
   }
 }
//Configuring load balancing on the service provider side
@Service(loadbalance = "random")
public class HelloServiceImpl implements HelloService {
  public String sayHello(String name) {
    return "hello " + name;
 }
}

Dubbo load balancing can be observed by starting multiple service providers.
Note: Since we start multiple service providers on one machine, we need to modify the port numbers of tomcat and Dubbo services to prevent port conflicts.
In a real production environment, multiple service providers are deployed on separate machines, so there is no port conflict issue.

Keywords: Java Dubbo Zookeeper

Added by acallahan on Sat, 18 Dec 2021 07:10:12 +0200