All Products
Search
Document Center

Enterprise Distributed Application Service:Use Spring Boot to develop a Dubbo microservices application

Last Updated:Sep 21, 2023

Spring Boot simplifies the configuration and deployment of microservices applications. You can select a registry and manage configurations based on your business requirements. This topic describes how to develop a demo Dubbo microservices application based on Nacos by using Spring Boot annotations. If you already have a Dubbo application that is developed by using Spring Boot, you can skip this topic and directly deploy the application to Enterprise Distributed Application Service (EDAS).

Prerequisites

Before you use Spring Boot to develop a Dubbo microservices application, make sure that the following operations are complete:

  • Download Maven and configure the environment variables.

  • Download the latest version of Nacos Server.

  • Start Nacos Server.

    1. Decompress the downloaded Nacos Server package.

    2. Go to the nacos/bin path and use one of the following methods to start Nacos Server. The methods vary based on your OS:

      • Linux, UNIX, or macOS: Run the sh startup.sh -m standalone command.

      • Windows: Double-click the startup.cmd file to run the file.

  • (Optional): Implement interconnection between on-premises and cloud applications.

Demo project

You can perform the steps described in this topic to build a project. You can also download the demo project used in this topic, or clone a project by running the git clone https://github.com/aliyun/alibabacloud-microservice-demo.git command in Git.

The cloned project contains multiple demo projects. The demo project used in this topic is stored in the alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot path.

Create a service provider

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

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

    The following sample code provides an example on how to add dependencies. In this example, Spring Boot 2.0.6.RELEASE is used.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>                   
  3. Develop a Dubbo service provider.

    All Dubbo services are provided by using interfaces.

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

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

      package com.alibaba.edas.boot;
      public interface IHelloService {
      String sayHello(String str);
      }                                
    3. In the com.alibaba.edas.boot package, create a class named IHelloServiceImpl to implement the interface.

      package com.alibaba.edas.boot;
      import com.alibaba.dubbo.config.annotation.Service;
      @Service
      public class IHelloServiceImpl implements IHelloService {
      public String sayHello(String name) {
        return "Hello, " + name + " (from Dubbo with Spring Boot)";
       }
      }                                
      Note

      In the sample code, the Service annotation is an annotation class that is provided by Dubbo. The full name of the annotation class is com.alibaba.dubbo.config.annotation.Service.

  4. Configure a Dubbo service.

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

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

      # Base packages to scan Dubbo Components (e.g @Service , @Reference)
      dubbo.scan.basePackages=com.alibaba.edas.boot
      dubbo.application.name=dubbo-provider-demo
      dubbo.registry.address=nacos://127.0.0.1:8848                                
      Note
      • You must specify values for the three configuration items in the preceding code because the configuration items do not have default values.

      • The value of dubbo.scan.basePackages is the packages whose code contains com.alibaba.dubbo.config.annotation.Service and com.alibaba.dubbo.config.annotation.Reference. Separate multiple packages with commas (,).

      • The value of dubbo.registry.address must start with nacos://, followed by the IP address and the port number of Nacos Server. The IP address in the sample code is an on-premises address. If you use an EDAS-managed registry, EDAS automatically changes the sample IP address to the IP address of the registry. If you use a self-managed Nacos registry, change the value of dubbo.registry.address in the sample code to the IP address of the registry.

  5. Develop and start the DubboProvider entry class of Spring Boot.

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

    In the list, com.alibaba.edas.boot.IHelloService is displayed. You can query the service 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 required dependencies to the pom.xml file.

    The following sample code provides an example on how to add dependencies. In this example, Spring Boot 2.0.6.RELEASE is used.

        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.1</version>
        </dependency>
    
    </dependencies>                        

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

    Note

    The lifecycle of Spring Boot 1.x ended in August 2019. We recommend that you use a new version to develop your application.

  3. Develop a Dubbo service consumer.

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

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

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

    The following sample code provides an example on how to call a remote Dubbo service in a controller:

    package com.alibaba.edas.boot;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
        public class DemoConsumerController {
    
            @Reference
            private IHelloService demoService;
    
            @RequestMapping("/sayHello/{name}")
            public String sayHello(@PathVariable String name) {
                return demoService.sayHello(name);
            }
        }                        
    Note

    In the sample code, the Reference annotation is used. The full name of the annotation class is com.alibaba.dubbo.config.annotation.Reference.

  5. Add the following configuration items to the application.properties or application.yaml file:

    dubbo.application.name=dubbo-consumer-demo
    dubbo.registry.address=nacos://127.0.0.1:8848                        
    Note
    • You must specify values for the two configuration items in the preceding code because the configuration items do not have default values.

    • The value of dubbo.registry.address must start with nacos://, followed by the IP address and the port number of Nacos Server. The IP address in the sample code is an on-premises address. If your Nacos Server is deployed on another machine, change the sample IP address to the IP address of the machine.

  6. Develop and start the DubboConsumer entry class of Spring Boot.

    package com.alibaba.edas.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DubboConsumer {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboConsumer.class, args);
        }
    
    }                        
  7. Log on to the Nacos console from http://127.0.0.1:8848. In the left-side navigation pane, click Services. On the Services page, view the list of caller services.

    In the list, com.alibaba.edas.boot.IHelloService is displayed. You can view the service group and caller IP address of the service.

Check the result

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

`Hello, EDAS (from Dubbo with Spring Boot)`            

Deploy the application to EDAS

Note

Application Configuration Management that is integrated in EDAS provides a formal commercial version of Nacos. When you deploy an application to EDAS, EDAS automatically changes the IP address and the port number, 127.0.0.1:8848, of your on-premises Nacos Server.

To deploy an application to EDAS, you can select a cluster type and a deployment method based on your business requirements. The following types of clusters are supported: Elastic Compute Service (ECS) clusters and Container Service for Kubernetes (ACK) clusters. The following deployment methods are supported: tools or the EDAS console. For more information, see the following topics:

If you deploy your application in the EDAS console, perform the following steps in your on-premises program before you deploy the application:

  1. Add the following configurations of the packaging plug-in to the pom.xml file.

    • Provider

      <build>
       <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                         <configuration>
                             <classifier>spring-boot</classifier>
                             <mainClass>com.alibaba.edas.boot.DubboProvider</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                                
    • Consumer

      <build>
       <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                         <configuration>
                             <classifier>spring-boot</classifier>
                             <mainClass>com.alibaba.edas.boot.DubboConsumer</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                                
  2. Run the mvn clean package command to build your on-premises program into a JAR package.

Additional information

If you use edas-dubbo-extension to deploy a Dubbo application to EDAS, you cannot use specific features of EDAS, such as Dubbo service governance. Therefore, we recommend that you do not use edas-dubbo-extension. We recommend that you migrate your application to Nacos.