[Dubbo notes 7] SpringBoot integrates Dubbo (Zookeeper)

Posted by markanite on Mon, 27 Sep 2021 02:52:07 +0200

Copyright notice: This article is a small original article, non-commercial free reprint - keep the signature - indicate the source, thank you!
Website of this article: https://sunkuan.blog.csdn.net/article/details/120502052







before [Dubbo notes sorting v] SpringBoot integrating Dubbo (Multicast) I introduced how to use SpringBoot to integrate Dubbo, but the Multicast registry is used in that blog. In this blog, we change to Zookeeper registry. The changes are not big. I will only demonstrate the changes. Please check other codes in the previous blog.

1, Dubbo demo

1. Increase Zookeeper dependency

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- dubbo rely on -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            
            <!--zookeeper Registry client import curator client -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

2. Maven dependency


2, Dubbo API

1,pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.demo</groupId>
        <artifactId>dubbo-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.demo</groupId>
    <artifactId>dubbo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-api</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- dubbo rely on -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        
        <!--zookeeper Registry client import curator client -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
    </dependencies>

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

</project>

3, Dubbo provider

1. application.yml (stand-alone)

#Prevent port conflicts
server:
  port: 8001

dubbo:
  application:
    # apply name
    name: provider
  registry:
    # Address of Registration Center
    address: zookeeper://127.0.0.1:2181
    # Timeout in milliseconds
    timeout: 6000
  # Meta center address
  metadata-report:
    address: zookeeper://127.0.0.1:2181
  protocol:
    #Agreement name
    name: dubbo
    #Protocol port
    port: 20880
  scan:
    #Scan package location
    base-packages: com.demo.service

2. application.yml (cluster)

#Prevent port conflicts
server:
  port: 8001

dubbo:
  application:
    # apply name
    name: provider
  registry:
    # Address of Registration Center
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
    # Timeout in milliseconds
    timeout: 6000
  # Meta center address
  metadata-report:
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
  protocol:
    #Agreement name
    name: dubbo
    #Protocol port
    port: 20880
  scan:
    #Scan package location
    base-packages: com.demo.service

4, Dubbo consumer

1,application.yml

#Prevent port conflicts
server:
  port: 8002

dubbo:
  application:
    # apply name
    name: consumer
  registry:
    # Address of Registration Center
    address: zookeeper://127.0.0.1:2181

2. application.yml (cluster)

#Prevent port conflicts
server:
  port: 8002

dubbo:
  application:
    # apply name
    name: consumer
  registry:
    # Address of Registration Center
    address: zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183

5, Test procedure

Execute DubboProviderApplication first, and then DubboConsumerApplication. The execution effect is as follows:

As can be seen from the above execution results, the service provider registers the service on the 2183 server, and the service consumer gets the service from the 2182 server.
 
As for which server is randomly assigned to register, it may be 2183 this time and 2182 or 2181 next time; The same is true for service consumers. It may be 2182 this time and 2181 or 2183 next time.
 
No matter which server the service provider registers the service on, other servers will synchronize without rotating the master-slave node, which is the synchronization state of the master-slave node in Zookeeper.

Let's add a little knowledge. How can Zookeeper ensure the state synchronization of master and slave nodes?

The core of Zookeeper is atomic broadcasting, which ensures the synchronization between servers. The protocol that implements this mechanism is called zab protocol. zab protocol has two modes: recovery mode (master selection) and broadcast mode (synchronization). When the service starts or the leader crashes, zab enters the recovery mode. When the leader is elected and most servers complete the synchronization with the leader, the recovery mode ends. State synchronization ensures that the leader and server have the same system state.


6, Program code

Download link of this article code: https://download.csdn.net/download/sun8112133/24821073



If there is anything inappropriate in the blog, please be sure to tell me. The road ahead is rugged. I hope we can help each other and move forward side by side!


Topics: Java Dubbo Spring Boot Zookeeper