Create a microservice application based on Spring Boot

Create a Dubbo application based on Spring Boot.

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.

Quickly Create an Application

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.

Project Structure Screenshot

Directly Use the Official Sample

You can also download the official pre-prepared sample project directly:

  1. $ git clone -b main --depth 1 https://github.com/apache/dubbo-samples
  2. $ cd dubbo-samples/11-quickstart

Start the Application Locally

Next, let’s try to start the application locally. Run the following command to start the application:

  1. ./mvnw

Note

Since the configuration file has enabled the registration center, to successfully start the application, you need to first start the Nacos or Zookeeper registration center server locally.

After the application starts successfully, the local process publishes the service using the Tripleprotocol on the specified port, and you can directly use cURL to test whether the service is running normally:

  1. curl \
  2. --header "Content-Type: application/json" \
  3. --data '["Dubbo"]' \
  4. 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.

Source Code Analysis

Import the prepared sample project into your favorite IDE development tool (taking IntelliJ IDEA as an example), the project structure is as follows:

Project Structure Screenshot

Maven Dependencies

Open pom.xml, and you can see the core dependencies related to Dubbo in the sample project as follows:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.apache.dubbo</groupId>
  5. <artifactId>dubbo-bom</artifactId>
  6. <version>3.3.0</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.apache.dubbo</groupId>
  15. <artifactId>dubbo-spring-boot-starter</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.dubbo</groupId>
  19. <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
  20. </dependency>
  21. </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.

Service Definition

The following is the standard Dubbo service definition based on a Java Interface.

  1. public interface DemoService {
  2. String sayHello(String name);
  3. }

In DemoService, the method sayHello is defined. Subsequent services published by the server and services subscribed by the consumer revolve around the DemoService interface.

Service Implementation

After defining the service interface, you can define the corresponding business logic implementation on the server side.

  1. @DubboService
  2. public class DemoServiceImpl implements DemoService {
  3. @Override
  4. public String sayHello(String name) {
  5. return "Hello " + name;
  6. }
  7. }

In the DemoServiceImpl class, the @DubboService annotation is added, and this configuration allows the Dubbo service to be published based on Spring Boot.

Initiating Service Calls

The sample application contains a consumer package simulating a remote call to the provider service.

  1. @Component
  2. public class Consumer implements CommandLineRunner {
  3. @DubboReference
  4. private DemoService demoService;
  5. @Override
  6. public void run(String... args) throws Exception {
  7. String result = demoService.sayHello("world");
  8. System.out.println("Receive result ======> " + result);
  9. }
  10. }

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").

Tip

Typically, remote calls are inter-process. To facilitate development, the sample project has directly embedded a @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.

Application Entry and Configuration File

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.

  1. # application.yml
  2. dubbo:
  3. application:
  4. name: dubbo-demo
  5. protocol:
  6. name: tri
  7. port: 50051
  8. registry:
  9. 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.

  1. @SpringBootApplication
  2. @EnableDubbo
  3. public class DemoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }

Publish Service Definitions to Remote Repository

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:

  1. mvn clean deploy

More Content

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)