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

Prerequisites

Before you use Spring Boot to develop a Dubbo microservice-oriented application, make sure that the following operations are complete:
  • Download Maven and set 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 directory and start Nacos Server.
      • On Linux, UNIX, or macOS, run the sh startup.sh -m standalone command.
      • On Windows, double-click the startup.cmd file to run the file.
  • Optional:

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 the project by running the Git command git clone https://github.com/aliyun/alibabacloud-microservice-demo.git.

This project contains multiple demo projects. The demo 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 the required dependencies to the pom.xml file.

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

    <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. Create a package named com.alibaba.edas.boot in src/main/java.
    2. In com.alibaba.edas.boot, create an interface named IHelloService that contains a SayHello method.
      package com.alibaba.edas.boot;
      public interface IHelloService {
      String sayHello(String str);
      }                                
    3. Create a class named IHelloServiceImpl in com.alibaba.edas.boot to implement this 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 The service annotation is com.alibaba.dubbo.config.annotation.Service, which is an annotation class in Dubbo.
  4. Configure a Dubbo service.
    1. In src/main/resources, create a file named application.properties or application.yaml 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 preceding three configuration items because they have no default values.
      • The value of dubbo.scan.basePackages is the package whose code contains the com.alibaba.dubbo.config.annotation.Service and com.alibaba.dubbo.config.annotation.Reference annotations. Separate multiple packages with commas (,).
      • The value of dubbo.registry.address must start with nacos://, which is followed by the IP address and port 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 it to the corresponding IP address.
  5. Develop and start the Spring Boot entry class DubboProvider.
        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 at http://127.0.0.1:8848. In the left-side navigation pane, click Services. On the Services page, view the list of service providers.
    com.alibaba.edas.boot.IHelloService is displayed in the list of service providers. You can also 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. Spring Boot 2.0.6.RELEASE is used in this example.
        <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 Spring Boot 1.x reached the end of life in August 2019. We recommend that you use a later version to develop applications.
  3. Develop a Dubbo service consumer.
    1. Create a package named com.alibaba.edas.boot in src/main/java.
    2. In com.alibaba.edas.boot, create an interface named IHelloService that contains a SayHello method.
      package com.alibaba.edas.boot;
      
      public interface IHelloService {
       String sayHello(String str);
      }                                
  4. Develop a Dubbo service call.

    For example, if you want to call a remote Dubbo service in a controller, use the following sample code:

    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 The Reference annotation 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 preceding two configuration items because they have no default values.
    • The value of dubbo.registry.address must start with nacos://, which is followed by the IP address and port 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 it to the corresponding IP address.
  6. Develop and start the Spring Boot entry class DubboConsumer.
    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 at http://127.0.0.1:8848. In the left-side navigation pane, click Services. On the Services page, view the list of service consumers.
    com.alibaba.edas.boot.IHelloService is displayed in the list of service consumers. You can also view the service group and consumer IP address of the service.

Verify 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 updates the IP address and port for your on-premises Nacos Server, such as 127.0.0.1:8848.

You can select a cluster type and a deployment method as needed to deploy applications. The cluster can be an Elastic Compute Service (ECS) cluster or a Container Service for Kubernetes (ACK) cluster. You can deploy applications in the EDAS console or by using tools. 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 configuration 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 package your on-premises program into a JAR package.

Additional information

If you use edas-dubbo-extension to deploy Dubbo applications to EDAS, you cannot use the related capabilities provided by EDAS, such as Dubbo service governance. Therefore, we recommend that you do not use edas-dubbo-extension. We recommend that you migrate your applications to Nacos.