Environmental requirements
-
System: Windows, Linux, MacOS
-
JDK 8 and above (JDK17 is recommended)
-
Git
-
IntelliJ IDEA (optional)
-
Docker (optional)
Project Introduction
In this task, it will be divided into three sub-modules for independent development, simulating the deployment architecture in the production environment.
. // apache/dubbo-samples/1-basic/dubbo-samples-spring-boot
├── dubbo-samples-spring-boot-interface // 共享 API 模块
├── dubbo-samples-spring-boot-consumer // 消费端模块
└── dubbo-samples-spring-boot-provider // 服务端模块
As shown above, there are 3 modules in total, and interface
the module is co-dependent consumer
with provider
two modules to store the API interface used by RPC communication.
. // apache/dubbo-samples/1-basic/dubbo-samples-spring-boot
├── dubbo-samples-spring-boot-interface // 共享 API 模块
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── org
│ └── apache
│ └── dubbo
│ └── springboot
│ └── demo
│ └── DemoService.java // API 接口
├── dubbo-samples-spring-boot-consumer // 消费端模块
│ ├── pom.xml
│ └── src
│ ├── main
│ │ ├── java
│ │ │ └── org
│ │ │ └── apache
│ │ │ └── dubbo
│ │ │ └── springboot
│ │ │ └── demo
│ │ │ └── consumer
│ │ │ ├── ConsumerApplication.java // 消费端启动类
│ │ │ └── Task.java // 消费端模拟调用任务
│ │ └── resources
│ │ └── application.yml // Spring Boot 配置文件
├── dubbo-samples-spring-boot-provider // 服务端模块
│ ├── pom.xml
│ └── src
│ └── main
│ ├── java
│ │ └── org
│ │ └── apache
│ │ └── dubbo
│ │ └── springboot
│ │ └── demo
│ │ └── provider
│ │ ├── DemoServiceImpl.java // 服务端实现类
│ │ └── ProviderApplication.java // 服务端启动类
│ └── resources
│ └── application.yml // Spring Boot 配置文件
└── pom.xml
The above is the file structure of the project that will be used in this tutorial.
Rapid deployment (direct start based on Samples)
This chapter will teach you step by step how to deploy and run a use case based on Dubbo x Spring Boot through a few simple commands.
Note: The details of the code deployed in this chapter can be found in the apache/dubbo-samples warehouse 1-basic/dubbo-samples-spring-boot
, which will be explained in the next chapter.
1. Get the test project
Before starting the whole tutorial, we need to get the code of the test project. All test case codes of Dubbo are stored in the apache/dubbo-samples warehouse, the following command can help you get all the codes in the Samples warehouse.
git clone --depth=1 --branch master [email protected]:apache/dubbo-samples.git
2. Start a simple registry
For a microservice application, the registry is an indispensable component. Only through the registration center, the consumer can successfully discover the address information of the server, and then make a call.
To make this tutorial easier to get started, we provide a simple starter based on the Apache Zookeeper registry. If you need to deploy the registry in a production environment, please refer to the production environment initialization article to deploy a highly available registry.
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 the server and consumer modules, it is necessary to package and install the modules locally first dubbo-samples-spring-boot-interface
.
./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 the service provider
After starting the registry, the next step is to start a service provider that provides services externally. Corresponding samples are also provided in dubbo-samples, which can be pulled up quickly by the following command.
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"
注:需要开一个独立的 terminal 运行,命令将会保持一直执行的状态。
After executing the above command, wait for a while for the following log ( Current Spring Boot Application is await
) to appear, which means that the service provider has started, indicating 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 the service consumer
The last step is to start a service consumer to call the service provider, which is the core of the RPC call, providing a bridge for the service consumer 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 command, wait for a while for the following log ( Hello world
) to appear. The printed data is returned after the service provider processes it, 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
Hands-on practice (from zero code development version)
This chapter will teach you how to develop a microservice application from scratch through step-by-step tutorials.
1. Start the registry
For a microservice application, the registry is an indispensable component. Only through the registration center, the consumer can successfully discover the address information of the server, and then make a call.
To make this tutorial easier to get started, we provide a simple starter based on the Apache Zookeeper registry. If you need to deploy the registry in a production environment, please refer to the production environment initialization article to deploy a highly available registry .
Windows:
git clone --depth=1 --branch master [email protected]: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 [email protected]: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. Initialize the project
Starting from this section, the project will be built and tested based on IntelliJ IDEA.
As shown above, a basic project can be built.
After building the basic project, we also need to create dubbo-spring-boot-demo-interface
, dubbo-spring-boot-demo-provider
and dubbo-spring-boot-demo-consumer
three sub-modules.
After creating the three submodules, you need to create the following folders:
-
Create package
dubbo-spring-boot-demo-consumer/src/main/java
underorg.apache.dubbo.springboot.demo.consumer
-
Create package
dubbo-spring-boot-demo-interface/src/main/java
underorg.apache.dubbo.springboot.demo
-
Create package
dubbo-spring-boot-demo-provider/src/main/java
underorg.apache.dubbo.springboot.demo.provider
The final folder reference is shown in the image above.
3. Add Maven dependency
After initializing the project, we need to add Dubbo-related maven dependencies first.
pom.xml
For multi-module projects, you first need to configure dependency information in the parent project .
Edit ./pom.xml
this file and 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 configure the specific dependencies in the dubbo-spring-boot-consumer
two dubbo-spring-boot-provider
modules .pom.xml
Edit ./dubbo-spring-boot-consumer/pom.xml
and ./dubbo-spring-boot-provider/pom.xml
add the following configuration to both files.
<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 dependencies of dubbo and zookeeper (and the corresponding connector curator) are defined.
After adding the above configuration, you can Maven - Reload All Maven Projects
refresh the dependencies through IDEA.
4. Define the service interface
The service interface is the bridge between the consumer and the server in Dubbo.
Create an interface under the dubbo-spring-boot-demo-interface
module , defined as follows:org.apache.dubbo.samples.api
DemoService
package org.apache.dubbo.springboot.demo;
public interface DemoService {
String sayHello(String name);
}
In , this method GreetingsService
is defined . Subsequent services published by the server and services subscribed by the consumer are all developed sayHi
around the interface.GreetingsService
5. Define the implementation of the server
After defining the service interface, you can define the corresponding implementation on the server side. Compared with the consumer side, this part of the implementation is a remote implementation, and there is no relevant information locally.
Create a class under the dubbo-spring-boot-demo-provider
module , defined as follows:org.apache.dubbo.samples.provider
DemoServiceImpl
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
, the interface is implemented DemoService
, for sayHello
the method to return Hello name
.
Note: Annotations DemoServiceImpl
are added to the class . Through this configuration, Dubbo services can be published based on Spring Boot.@DubboService
6. Configure the server-side Yaml configuration file
From this step to step 7, some basic information of Dubbo will be configured through Spring Boot.
First, let's create a configuration file for the server.
Create a file under the resource folder dubbo-spring-boot-demo-provider
of the module , defined as follows:resources
application.yml
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 Dubbo application name, Dubbo protocol information, and the registry address used by Dubbo are defined.
7. Configure the consumer-side YAML configuration file
Similarly, we need to create a configuration file for the consumer.
Create a file under the resource folder dubbo-spring-boot-demo-consumer
of the module , defined as follows:resources
application.yml
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 Dubbo application name, Dubbo protocol information, and the registry address used by Dubbo are defined.
8. Configure the server startup class based on Spring
In addition to configuring the Yaml configuration file, we also need to create a Spring Boot-based startup class.
First of all, we first create the startup class of the server.
Create a class under the dubbo-spring-boot-demo-provider
module , defined as follows:org.apache.dubbo.springboot.demo.provider
Application
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 startup class, one is configured ProviderApplication
to read the configuration file defined in the previous step 6 application.yml
and start the application.
9. Configure the consumer startup class based on Spring
Similarly, we need to create a startup class for the consumer.
Create a class under the dubbo-spring-boot-demo-consumer
module , defined as follows:org.apache.dubbo.springboot.demo.consumer
Application
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 startup class, one is configured ConsumerApplication
to read the configuration file defined in step 7 above application.yml
and start the application.
10. Configure consumer request tasks
CommandLineRunner
In addition to configuring the startup class of the consumer side, we can also create based on the Spring Boot mode
Create a class under the dubbo-spring-boot-demo-consumer
module , defined as follows:org.apache.dubbo.springboot.demo.consumer
Task
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
the class, by @DubboReference
obtaining an RPC subscription from Dubbo, this demoService
can be called directly like a local call. run
A thread is created in the method to make the call .
11. Start the application
As of step 10, the code has been developed, and this section will start the entire project and verify it.
The first thing is to start org.apache.dubbo.samples.provider.Application
. After a while, the log ( Current Spring Boot Application is await
) shown in the figure below will appear, which means that the service provider has started, indicating that the service provider can provide services to the outside world.
[Dubbo] Current Spring Boot Application is await...
Then start. org.apache.dubbo.samples.client.Application
After a while, the log ( Hello world
) as shown in the figure below will appear, which means that the service consumer has started and called the server to obtain the result successfully.
Receive result ======> Hello world