All Products
Search
Document Center

Use Spring Boot to develop Dubbo applications

Last Updated: Sep 29, 2019

Spring Boot simplifies the configuration and deployment of microservice applications. Meanwhile, Nacos also provides the service registration and discovery as well as configuration management functions. Together, both functions can help you improve development efficiency. This topic describes how to use Spring Boot annotations to develop a Dubbo microservice sample application based on Nacos.

Preparations

Before using Spring Boot to develop a Dubbo microservice application, complete the following tasks:

  • Download Maven and set environment variables.

  • Download the latest version of Nacos Server.

  • To start Nacos Server, follow these steps.

    1. Decompress the downloaded Nacos Server package.
    2. Go to the nacos/bin directory and start Nacos Server.
      • For Linux/UNIX/Mac: Run the sh startup.sh -m standalone command.
      • For Windows: Double-click the startup.cmd file to run it.

Sample project

You can follow the steps described in this topic to build the project. Alternatively, you can directly download the sample project used in this topic, or use Git to clone the project by running this command: git clone https://github.com/aliyun/alibabacloud-microservice-demo.git.

This project contains many sample projects. The sample project used in this topic can be found in alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot.

Create a service provider

  1. Create a Maven project named spring-boot-dubbo-provider.

  2. Add required dependencies to the pom.xml file.

    The following uses Spring Boot 2.0.6.RELEASE as an example.

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-dependencies</artifactId>
    6. <version>2.0.6.RELEASE</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter</artifactId>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-actuator</artifactId>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.apache.dubbo</groupId>
    23. <artifactId>dubbo-spring-boot-starter</artifactId>
    24. <version>2.7.3</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>com.alibaba.nacos</groupId>
    28. <artifactId>nacos-client</artifactId>
    29. <version>1.1.1</version>
    30. </dependency>
    31. </dependencies>
  3. Develop a Dubbo service provider.

    All services in Dubbo are provided as interfaces.

    1. In src/main/java, create a package named com.alibaba.edas.boot.

    2. In com.alibaba.edas.boot, create an interface named IHelloService that contains a SayHello method.

      1. package com.alibaba.edas.boot;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
    3. Create a class named IHelloServiceImpl in com.alibaba.edas.boot to implement the interface.

      1. package com.alibaba.edas.boot;
      2. import com.alibaba.dubbo.config.annotation.Service;
      3. @Service
      4. public class IHelloServiceImpl implements IHelloService {
      5. public String sayHello(String name) {
      6. return "Hello, " + name + " (from Dubbo with Spring Boot)";
      7. }
      8. }

      Note: In Dubbo, the service annotation is com.alibaba.dubbo.config.annotation.Service.

  4. Configure the Dubbo service.

    1. In src/main/resources, create a file named application.properties or application.yaml and open it.

    2. In application.properties or application.yaml, add the following configuration items.

      1. # Base packages to scan Dubbo Components (e.g @Service , @Reference)
      2. dubbo.scan.basePackages=com.alibaba.edas.boot
      3. dubbo.application.name=dubbo-provider-demo
      4. dubbo.registry.address=nacos://127.0.0.1:8848

      Note:

      You must specify values for the preceding three configuration items because they have no defaults.

      • The value of dubbo.scan.basePackages is the name of the package with code containing the annotations com.alibaba.dubbo.config.annotation.Service and com.alibaba.dubbo.config.annotation.Reference. Separate multiple packages with commas (,).
      • The prefix of the value of dubbo.registry.address must start with nacos://. The IP address and port that follow refer to the address and port of Nacos Server, respectively. The IP address in the code example is a local address. If you deploy Nacos Server on another machine, change it to the actual IP address.
  5. Develop and start the Spring Boot handler class DubboProvider.

    1. package com.alibaba.edas.boot;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class DubboProvider {
    6. public static void main(String[] args) {
    7. SpringApplication.run(DubboProvider.class, args);
    8. }
    9. }
  6. Log on to the Nacos console at http://127.0.0.1:8848. In the left-side navigation pane, click Services to view the list of providers.

    You can see com.alibaba.edas.boot.IHelloService in the list of service providers and you can query the group and provider IP address of the service.

Create a service consumer

  1. Create a Maven project named spring-boot-dubbo-consumer.

  2. Add required dependencies to the pom.xml file.

    The following uses Spring Boot 2.0.6.RELEASE as an example.

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.boot</groupId>
    5. <artifactId>spring-boot-dependencies</artifactId>
    6. <version>2.0.6.RELEASE</version>
    7. <type>pom</type>
    8. <scope>import</scope>
    9. </dependency>
    10. </dependencies>
    11. </dependencyManagement>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.boot</groupId>
    15. <artifactId>spring-boot-starter-web</artifactId>
    16. </dependency>
    17. <dependency>
    18. <groupId>org.springframework.boot</groupId>
    19. <artifactId>spring-boot-actuator</artifactId>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.apache.dubbo</groupId>
    23. <artifactId>dubbo-spring-boot-starter</artifactId>
    24. <version>2.7.3</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>com.alibaba.nacos</groupId>
    28. <artifactId>nacos-client</artifactId>
    29. <version>1.1.1</version>
    30. </dependency>
    31. </dependencies>

    If you want to use Spring Boot 1.x, select Spring Boot 1.5.x. The corresponding com.alibaba.boot:dubbo-spring-boot-starter version is 0.1.0.

    Note: Spring Boot 1.x will reach end-of-life in August 2019. We recommend that you use a later version to develop applications.

  3. Develop a Dubbo consumer

    1. In src/main/java, create a package named com.alibaba.edas.boot.

    2. In com.alibaba.edas.boot, create an interface named IHelloService that contains a SayHello method.

      1. package com.alibaba.edas.boot;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
  4. Develop a Dubbo service call.

    For example, you need to call a remote Dubbo service once in Controller. The code is as follows.

    1. package com.alibaba.edas.boot;
    2. import com.alibaba.dubbo.config.annotation.Reference;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class DemoConsumerController {
    8. @Reference
    9. private IHelloService demoService;
    10. @RequestMapping("/sayHello/{name}")
    11. public String sayHello(@PathVariable String name) {
    12. return demoService.sayHello(name);
    13. }
    14. }

    Note: The Reference annotation is com.alibaba.dubbo.config.annotation.Reference.

  5. Add the following configuration items in the application.properties/application.yaml configuration file:

    1. dubbo.application.name=dubbo-consumer-demo
    2. dubbo.registry.address=nacos://127.0.0.1:8848

    Note:

    • The preceding two configuration items have no defaults and must be specified.
    • The prefix of the value of dubbo.registry.address must start with nacos://. The IP address and port that follow refer to the address and port of Nacos Server, respectively. The IP address in the code example is a local address. If you deploy Nacos Server on another machine, change it to the actual IP address.
  6. Develop and start the Spring Boot handler class DubboConsumer.

    1. package com.alibaba.edas.boot;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class DubboConsumer {
    6. public static void main(String[] args) {
    7. SpringApplication.run(DubboConsumer.class, args);
    8. }
    9. }
  7. Log on to the Nacos console at http://127.0.0.1:8848. Then, in the left-side navigation pane, choose Services. On the Services page that appears, select Callers to view the list of callers.

    You can see com.alibaba.edas.boot.IHelloService in the list. Also, you can view the group and caller IP address of the service.

Verify the result

  1. `curl http://localhost:8080/sayHello/EDAS`
  2. `Hello, EDAS (from Dubbo with Spring Boot)`

Deploy the application to EDAS

You can directly deploy the application that uses local Nacos as the registry to EDAS without making any changes. This registry will be automatically replaced with the registry in EDAS.

Based on your actual needs, you can choose the type of cluster to deploy the application to, which is mainly ECS cluster or Container Service Kubernetes cluster, as well as the deployment method, which can be in the console or with tools. For more information, see Application deployment overview.

If you use the console for deployment, complete the following steps in your local application before deploying it:

  1. Add the following packaging plug-in configuration items to the pom.xml file.

    • Provider

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. <executions>
      7. <execution>
      8. <goals>
      9. <goal>repackage</goal>
      10. </goals>
      11. <configuration>
      12. <classifier>spring-boot</classifier>
      13. <mainClass>com.alibaba.edas.boot.DubboProvider</mainClass>
      14. </configuration>
      15. </execution>
      16. </executions>
      17. </plugin>
      18. </plugins>
      19. </build>
    • Consumer

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. <executions>
      7. <execution>
      8. <goals>
      9. <goal>repackage</goal>
      10. </goals>
      11. <configuration>
      12. <classifier>spring-boot</classifier>
      13. <mainClass>com.alibaba.edas.boot.DubboConsumer</mainClass>
      14. </configuration>
      15. </execution>
      16. </executions>
      17. </plugin>
      18. </plugins>
      19. </build>
  2. Execute mvn clean package to package your local program into a JAR file.

More information