The following document will guide you in creating a Dubbo application based on Spring Boot from scratch, configuring microservice foundational capabilities such as the Triple communication protocol and service discovery.
Create a Dubbo microservice application by accessing start.dubbo.apache.org. Add components as shown in the image below, and you can quickly create a Dubbo application in seconds. Download and unzip the generated sample application.
You can also download the official pre-prepared sample project directly:
$ git clone -b main --depth 1 https://github.com/apache/dubbo-samples
$ cd dubbo-samples/11-quickstart
Next, let’s try to start the application locally. Run the following command to start the application:
./mvnw
After the application starts successfully, the local process publishes the service using the Triple protocol on the specified port, and you can directly use cURL to test whether the service is running normally:
curl \
--header "Content-Type: application/json" \
--data '["Dubbo"]' \
http://localhost:50051/com.example.demo.dubbo.api.DemoService/sayHello/
In addition to using the command line, we can also start the project in the IDE, modify the example, or debug locally.
Import the prepared sample project into your favorite IDE development tool (taking IntelliJ IDEA as an example), the project structure is as follows:
Open pom.xml, and you can see the core dependencies related to Dubbo in the sample project as follows:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
</dependency>
</dependencies>
Among them, dubbo-spring-boot-starter
and dubbo-zookeeper-spring-boot-starter
introduce the dependencies related to the Dubbo kernel framework and Zookeeper client respectively. More content can be viewed in the list of Spring Boot Starters supported by Dubbo.
The following is the standard Dubbo service definition based on a Java Interface.
public interface DemoService {
String sayHello(String name);
}
In DemoService
, the method sayHello
is defined. Subsequent services published by the server and services subscribed by the consumer revolve around the DemoService
interface.
After defining the service interface, you can define the corresponding business logic implementation on the server side.
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
In the DemoServiceImpl
class, the @DubboService
annotation is added, and this configuration allows the Dubbo service to be published based on Spring Boot.
The sample application contains a consumer package simulating a remote call to the provider service.
@Component
public class Consumer 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);
}
}
In the Task
class, a RPC subscription is obtained from Dubbo via @DubboReference
, and this demoService
can be called as if it were a local call: demoService.sayHello("world")
.
@DubboReference
call. If you want to learn how to develop an independent Consumer (client) process for initiating remote calls to Dubbo services, we have a sample project with independent consumer and provider modules for reference.Since we created a Spring Boot application, Dubbo-related configuration information is stored in the application.yml
configuration file. Based on the following configuration, the Dubbo process will listen for triple protocol requests on port 50051, while the instance’s ip:port information will be registered to the Zookeeper server.
# application.yml
dubbo:
application:
name: dubbo-demo
protocol:
name: tri
port: 50051
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
Here is the entry point for the entire application, and the @EnableDubbo
annotation is used to load and start Dubbo-related components.
@SpringBootApplication
@EnableDubbo
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
After completing the application development, we need to publish the service definitions to an externally public or organization-internal Maven repository, so that applications that call these services can load and use them.
As we saw earlier, the sample project includes two modules, api and service. Switch to the api directory, and the following command will complete the publishing action:
mvn clean deploy