Dubbo Spring Boot Starter develops micro-service applications

Harshit NagpalHarshit Nagpal
10 min read

Aim

Develop micro-service development based on Dubbo x Spring Boot from scratch to understand Dubbo x Spring Boot configuration.

Difficulty

low

Environmental requirements

  • System: Windows, Linux, MacOS

  • JDK 8 and above ( recommended to use JDK17)

  • Git

  • IntelliJ IDEA( optional )

  • Docker ( optional )

Project introduction

In this task, it will be divided into 3 sub-modules for independent development to simulate the deployment structure in the production environment.

.  // apache/dubbo-samples/1-basic/dubbo-samples-spring-boot
├── dubbo-samples-spring-boot-interface       // Shared API module
├── dubbo-samples-spring-boot-consumer        // Consumer module
└── dubbo-samples-spring-boot-provider        // Server module

As shown above, there are 3 modules, of which interface Module was consumer with provider The two modules rely on each other to store the API interface used in RPC communications.

.  // apache/dubbo-samples/1-basic/dubbo-samples-spring-boot
├── dubbo-samples-spring-boot-interface       // Shared API modules
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── org
│                   └── apache
│                       └── dubbo
│                           └── springboot
│                               └── demo
│                                   └── DemoService.java // API interface
├── dubbo-samples-spring-boot-consumer        // Consumer module
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── org
│       │   │       └── apache
│       │   │           └── dubbo
│       │   │               └── springboot
│       │   │                   └── demo
│       │   │                       └── consumer
│       │   │                           ├── ConsumerApplication.java // Consumer side startup class
│       │   │                           └── Task.java                // Consumer side simulation call task
│       │   └── resources
│       │       └── application.yml       // Spring Boot Configuration file
├── dubbo-samples-spring-boot-provider        // Server module
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── org
│           │       └── apache
│           │           └── dubbo
│           │               └── springboot
│           │                   └── demo
│           │                       └── provider
│           │                           ├── DemoServiceImpl.java         // Server implementation class
│           │                           └── ProviderApplication.java     // Server startup class
│           └── resources
│               └── application.yml       // Spring Boot Configuration file
└── pom.xml

The above is the file structure of the project that will be used next in this tutorial.

Rapid deployment ( is based on Samples direct start )

This chapter will teach you how to deploy and run an example based on Dubbo x Spring Boot step by step through a few simple commands.

Note: The code details for deployment in this chapter can be apache/dubbo-samples In this warehouse 1-basic/dubbo-samples-spring-boot Found in China, the explanation will also be carried out in the next chapter.

1. Get test engineering

Before starting the entire tutorial, we need to get the code of the test project. All Dubbo test code is stored in apache/dubbo-samples In this warehouse, the following order can help you obtain all the codes of the Samples warehouse.

git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git

2. Start a simple registration center

For a micro-service application, the registration center is an indispensable component. Only through the registration center can the consumer end successfully discover the address information of the service end and then call it.

In order to make this course easier to get started, we provide a simple starter based on the Apache Zookeeper registration center. If you need to deploy a registration center in the production environment, please refer to Initialization of the production environment A high-availability registration center is deployed.

Windows:
./mvnw.cmd clean compile exec:java -pl tools/embedded-zookeeper

Linux / MacOS:
./mvnw clean compile exec:java -pl tools/embedded-zookeeper

Docker:
docker run --name some-zookeeper -p 2181:2181 --restart always -d zookeeper

3. Local packaging API module

In order to successfully compile service end and consumer end modules, it is necessary to pack and install locally dubbo-samples-spring-boot-interface module.

./mvnw clean install -pl 1-basic/dubbo-samples-spring-boot
./mvnw clean install -pl 1-basic/dubbo-samples-spring-boot/dubbo-samples-spring-boot-interface

4. Start service provider

After starting the registration center, the next step is to start a service provider that provides services externally. Corresponding examples are also provided in dubbo-samples, which can be quickly pulled up by the following order.

Windows:  
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-spring-boot/dubbo-samples-spring-boot-provider -Dexec.mainClass="org.apache.dubbo.springboot.demo.provider.ProviderApplication"  

Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-spring-boot/dubbo-samples-spring-boot-provider -Dexec.mainClass="org.apache.dubbo.springboot.demo.provider.ProviderApplication"  

Note: You need to open an independent terminal to run, and the command will remain executed.

After executing the above order, wait for a while to appear as follows:Current Spring Boot Application is await) means that the service provider has been activated, marking that the service provider can provide services to the outside world.

2023-02-08 17:13:00.357  INFO 80600 --- [lication.main()] o.a.d.c.d.DefaultApplicationDeployer     :  [DUBBO] Dubbo Application[1.1](dubbo-springboot-demo-provider) is ready., dubbo version: 3.2.0-beta.4, current host: 30.221.128.96
2023-02-08 17:13:00.369  INFO 80600 --- [lication.main()] o.a.d.s.d.provider.ProviderApplication   : Started ProviderApplication in 9.114 seconds (JVM running for 26.522)
2023-02-08 17:13:00.387  INFO 80600 --- [pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener :  [Dubbo] Current Spring Boot Application is await...

5. Start service consumer

The last step is to start a service consumer to call the service provider, which is the core of the RPC call, to provide a bridge for service consumers to call the service provider.

Windows:  
./mvnw.cmd clean compile exec:java -pl 1-basic/dubbo-samples-spring-boot/dubbo-samples-spring-boot-consumer -Dexec.mainClass="org.apache.dubbo.springboot.demo.consumer.ConsumerApplication"

Linux / MacOS:
./mvnw clean compile exec:java -pl 1-basic/dubbo-samples-spring-boot/dubbo-samples-spring-boot-consumer -Dexec.mainClass="org.apache.dubbo.springboot.demo.consumer.ConsumerApplication"

After executing the above order, wait for a while to appear as follows:Hello world), the printed data was returned by the service provider after processing, marking the success of a service call.

2023-02-08 17:14:33.045  INFO 80740 --- [lication.main()] o.a.d.s.d.consumer.ConsumerApplication   : Started ConsumerApplication in 11.052 seconds (JVM running for 31.62)
Receive result ======> Hello world
2023-02-08 17:14:33.146  INFO 80740 --- [pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener :  [Dubbo] Current Spring Boot Application is await...
Wed Feb 08 17:14:34 CST 2023 Receive result ======> Hello world
Wed Feb 08 17:14:35 CST 2023 Receive result ======> Hello world
Wed Feb 08 17:14:36 CST 2023 Receive result ======> Hello world
Wed Feb 08 17:14:37 CST 2023 Receive result ======> Hello world

Practice ( From zero code development version )

This chapter will teach you how to develop a micro-service application from zero step by step through the hand-held tutorial.

1. Start registration center

For a micro-service application, the registration center is an indispensable component. Only through the registration center can the consumer end successfully discover the address information of the service end and then call it.

In order to make this course easier to get started, we provide a simple starter based on the Apache Zookeeper registration center. If you need to deploy a registration center in the production environment, please refer Initialization of the production environment high-availability registration center is deployed.

Windows:
git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git
cd dubbo-samples
./mvnw.cmd clean compile exec:java -pl tools/embedded-zookeeper

Linux / MacOS:
git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git
cd dubbo-samples
./mvnw clean compile exec:java -pl tools/embedded-zookeeper

Docker:
docker run --name some-zookeeper -p 2181:2181 --restart always -d zookeeper

2. Initialization project

Starting from this section, the construction and testing of the project will be based on IntelliJ IDEA.

As shown in the figure above, a basic project can be established.

After building a basic project, we still need to create dubbo-spring-boot-demo-interfacedubbo-spring-boot-demo-provider with dubbo-spring-boot-demo-consumer Three sub-modules.

After creating three sub-modules, you need to create a few folders:

  1. in dubbo-spring-boot-demo-consumer/src/main/java Create below org.apache.dubbo.springboot.demo.consumer package

  2. in dubbo-spring-boot-demo-interface/src/main/java Create below org.apache.dubbo.springboot.demo package

  3. in dubbo-spring-boot-demo-provider/src/main/java Create below org.apache.dubbo.springboot.demo.provider package

3. Add Maven dependence

After initializing the project, we need to add Dubbo related maven dependence first.

For multi-module projects, the father’s project is first required pom.xml The configuration inside depends on information.

edit ./pom.xml For this file, add the following configuration.

<groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>dubbo-spring-boot-demo-interface</module>
        <module>dubbo-spring-boot-demo-provider</module>
        <module>dubbo-spring-boot-demo-consumer</module>
    </modules>

    <properties>
        <dubbo.version>3.2.0-beta.4</dubbo.version>
        <spring-boot.version>2.7.8</spring-boot.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Dubbo -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

Then in dubbo-spring-boot-consumer with dubbo-spring-boot-provider Two modules pom.xml Make specific reliance on configuration.

edit ./dubbo-spring-boot-consumer/pom.xml with ./dubbo-spring-boot-provider/pom.xml For both documents, add the following configuration.

<dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-demo-interface</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- spring boot starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

In this configuration, the dependence of dubbo and zookeeper( and the corresponding connector curator) is defined.

After adding the above configuration, you can pass IDEA Maven - Reload All Maven Projects Refresh dependence.

4. Define service interface

Service interface Dubbo communicates the bridge between consumer and service ends.

In dubbo-spring-boot-demo-interface Module org.apache.dubbo.samples.api Build below DemoService Interface, defined as follows:

package org.apache.dubbo.springboot.demo;

public interface DemoService {

    String sayHello(String name);
}

in DemoService Medium, defined sayHello This method. Follow-up services end-issued services, and consumer end subscription services are all around DemoService The interface unfolded.

5. Define the realization of service ends

After defining the service interface, the corresponding realization can be defined on the side of the service end. The realization of this part is a remote realization relative to the consumer end, and there is no relevant information locally.

Indubbo-spring-boot-demo-provider Module org.apache.dubbo.samples.provider Build below DemoServiceImpl Category, defined as follows:

package org.apache.dubbo.springboot.demo.provider;

import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.springboot.demo.DemoService;

@DubboService
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

in DemoServiceImpl Medium, realized DemoService Interface, for sayHello Method return Hello name.

Note: AtDemoServiceImpl Added to the class @DubboService Note that this configuration can be based on Spring Boot to publish Dubbo services.

6. Configuration Service End Yaml Configuration File

From this step to step 7, some basic information of Dubbo will be configured by Spring Boot.

First, we first create a configuration file for the service end.

In dubbo-spring-boot-demo-provider Module resources Established under the resource folder application.yml The document is defined as follows:

dubbo:
  application:
    name: dubbo-springboot-demo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181

In this configuration file, the application name of Dubbo, Dubbo protocol information, and the registered center address used by Dubbo are defined.

7. Configuration consumption end YAML configuration file

Similarly, we need to create configuration files on the consumer end.

In dubbo-spring-boot-demo-consumer Module resources Established under the resource folder application.yml The document is defined as follows:

dubbo:
  application:
    name: dubbo-springboot-demo-consumer
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:127.0.0.1}:2181

In this configuration file, the application name of Dubbo, Dubbo protocol information, and the registered center address used by Dubbo are defined.

8. Based on Spring configuration service end start-up class

In addition to configuring Yaml configuration files, we also need to create a start-up category based on Spring Boot.

First, we first create the start-up category of the service end.

In dubbo-spring-boot-demo-provider Module org.apache.dubbo.springboot.demo.provider Build below Application Category, defined as follows:

package org.apache.dubbo.springboot.demo.provider;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

In this start-up category, one is configured ProviderApplication To read what we defined in step 6 above application.yml Configure files and start applications.

9. Based on Spring configuration consumer end startup

Similarly, we need to create start-up classes for the consumer end.

In dubbo-spring-boot-demo-consumer Module org.apache.dubbo.springboot.demo.consumer Build below Application Category, defined as follows:

package org.apache.dubbo.springboot.demo.consumer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

In this start-up category, one is configured ConsumerApplication To read what we defined in step 7 above application.yml Configure files and start applications.

10. Configure consumer end request tasks

In addition to configuring the start-up category of the consumer end, we can also be based on the Spring Boot model CommandLineRunnerTo create

In dubbo-spring-boot-demo-consumer Module org.apache.dubbo.springboot.demo.consumer Build below Task Category, defined as follows:

package org.apache.dubbo.springboot.demo.consumer;

import java.util.Date;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.springboot.demo.DemoService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Task implements CommandLineRunner {
    @DubboReference
    private DemoService demoService;

    @Override
    public void run(String... args) throws Exception {
        String result = demoService.sayHello("world");
        System.out.println("Receive result ======> " + result);

        new Thread(()-> {
            while (true) {
                try {
                    Thread.sleep(1000);
                    System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
        }).start();
    }
}

In Task In the category, pass@DubboReference Obtained a RPC subscription from Dubbo, this demoService It can be called directly like a local call. in runA line was created in the method to call.

11. Start application

As of step 10, the code has been developed, and this section will start the entire project and verify it.

The first is to start org.apache.dubbo.samples.provider.Application ,Waiting for a while to appear as shown in the log (Current Spring Boot Application is await) means that the service provider has been activated, marking that the service provider can provide services to the outside world.

[Dubbo] Current Spring Boot Application is await...

Then startorg.apache.dubbo.samples.client.Application , Waiting for a while to appear as shown in the log (Hello world ) means that the service consumer end is activated and called to the service end to obtain results successfully.

Receive result ======> Hello world
0
Subscribe to my newsletter

Read articles from Harshit Nagpal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshit Nagpal
Harshit Nagpal