All Products
Search
Document Center

Use Spring Boot to develop Dubbo applications

Last Updated: Mar 25, 2019

If you have limited experience in Java and Maven and are not familiar with Dubbo, you can develop services on Dubbo from scratch and use the EDAS service registry for service registration and discovery.

You can develop services on Dubbo from scratch in either of the following ways:

  • Use XML
  • Use Spring Boot annotations

For more information about the XML development method, see Deploy Dubbo applications to EDAS.This topic describes how to use Spring Boot annotations to develop services on Dubbo.Spring Boot helps you quickly build a Spring-based service on Dubbo with simplest configurations. It is more efficient than the XML development method.

Why is the EDAS service registry used?

The EDAS service registry implements SPI-based registry extension provided by Dubbo. It completely supports service registration, routing rules, and rule configuration on Dubbo.

The EDAS service registry can completely replace ZooKeeper and Redis as the registry for your service on Dubbo.In contrast to ZooKeeper and Redis, the EDAS service registry has the following advantages:

  • The EDAS service registry is a shared component, saving you the costs of operating, maintaining, and deploying components such as ZooKeeper.
  • The EDAS service registry implements authentication and encryption in the communication process, improving the security of your service registration links.
  • The EDAS service registry is fully integrated with other EDAS components to provide you with a complete set of microservice solutions.

Local development

Preparation

  • Download, start, and configure the lightweight configuration center

    To facilitate local development, EDAS provides a lightweight configuration center that has the basic features of the EDAS service registry.You can deploy the applications developed based on the lightweight configuration center to EDAS without making any modifications to code or configurations.

    For more information about how to download, start, and configure the lightweight configuration center, see Configure the lightweight configuration center.The latest version is recommended.

  • Download Maven and set environment variables (skip this step if Maven is installed locally).

Create a service provider

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

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

    The following takes the 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>com.alibaba.boot</groupId>
    23. <artifactId>dubbo-spring-boot-starter</artifactId>
    24. <version>0.2.0</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>com.alibaba.edas</groupId>
    28. <artifactId>edas-dubbo-extension</artifactId>
    29. <version>1.0.6</version>
    30. </dependency>
    31. </dependencies>

    If you need to use Spring Boot 1.x, use Spring Boot 1.5.x and com.alibaba.boot:dubbo-spring-boot-starter 0.1.0.

    Note: Spring Boot 1.x will expire in August 2019, so we recommend that you use a later version to develop applications.

  3. Develop a Dubbo service provider.

    Services in Dubbo are provided over APIs.

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

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

      1. package com.alibaba.edas.boot;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
    3. In com.alibaba.edas.boot, create a IHelloServiceImpl class for the API.

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

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

  4. Configure Dubbo services.

    1. In the src/main/resources path, create an application.properties or application.yaml file and open it.

    2. Add the following configuration to the application.properties or application.yaml file.

      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=edas://127.0.0.1:8080

      Note:

      • The preceding three configurations have no default values and must be specified.
      • The value of dubbo.scan.basePackages is the name of the package with codes containing annotations com.alibaba.dubbo.config.annotation.Service and com.alibaba.dubbo.config.annotation.Reference.Multiple packages are separated by commas (,).
      • The value of dubbo.registry.address must start with edas://. The IP address and port indicate a lightweight configuration center.
  5. Develop and start the Spring Boot entry 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 console of the lightweight configuration center. In the left-side navigation pane, click Services to view the list of service providers.

    com.alibaba.edas.boot.IHelloService exists 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 the necessary dependencies to the pom.xml file.

    The following takes the 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>com.alibaba.boot</groupId>
    23. <artifactId>dubbo-spring-boot-starter</artifactId>
    24. <version>0.2.0</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>com.alibaba.edas</groupId>
    28. <artifactId>edas-dubbo-extension</artifactId>
    29. <version>1.0.6</version>
    30. </dependency>
    31. </dependencies>

    If you need to use Spring Boot 1.x, use Spring Boot 1.5.x and com.alibaba.boot:dubbo-spring-boot-starter 0.1.0.

    Note: Spring Boot 1.x will expire in August 2019, so we recommend that you use a later version to develop applications.

  3. Develop Dubbo service consumers.

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

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

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

    For example, to remotely call Dubbo services in the controller, develop the following code:

    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 to the application.properties or application.yaml file.

    1. dubbo.application.name=dubbo-consumer-demo
    2. dubbo.registry.address=edas://127.0.0.1:8080

    Note:

    • The preceding two configurations have no default values and must be specified.
    • The value of dubbo.registry.address must start with edas://. The IP address and port indicate a lightweight configuration center.
  6. Develop and start the Spring Boot entry 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 console of the lightweight configuration center. In the left-side navigation pane, click Services. On the Services page, click Callers to view the list of callers.

    com.alibaba.edas.boot.IHelloService exists in the list and you can view the group and caller IP address of the service.

Result verification

  1. Locally verify the result.

    curl http://localhost:17080/sayHello/EDAS

    Hello, EDAS (from Dubbo with Spring Boot)

  2. Verify the result in EDAS.

    curl http://localhost:8080/sayHello/EDAS

    Hello, EDAS (from Dubbo with Spring Boot)

Deploy applications to EDAS

edas-dubbo-extension is designed to support the migration of local applications to EDAS. You can directly deploy applications to EDAS without making any modifications to code or configurations.

  1. Add the following configuration to the pom.xml files of DubboProvider and DubboConsumer. Then, run mvn clean package to package native programs into executable JAR packages.

    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. </execution>
    12. </executions>
    13. </plugin>
    14. </plugins>
    15. </build>
  2. Deploy applications based on the appropriate documents for the corresponding cluster types.