This tutorial shows how to build a Dubbo service provider and consumer with Spring Boot annotations and Nacos as the service registry, then deploy to Enterprise Distributed Application Service (EDAS). You can select a registry and manage configurations based on your business requirements.
If you already have a Spring Boot-based Dubbo application, skip this topic and deploy the application directly to EDAS.
Quick start with the demo project
Clone the pre-built demo to skip the manual setup:
git clone https://github.com/aliyun/alibabacloud-microservice-demo.git
cd alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-bootAlternatively, download the ZIP archive. The demo for this tutorial is in alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot.
To scaffold a new Spring Boot project with the required dependencies pre-configured, use Cloud Native App Initializer.
Prerequisites
Before you begin, make sure that you have:
Maven installed with environment variables configured
The latest Nacos Server downloaded and started in standalone mode:
Extract the downloaded Nacos Server package.
Navigate to the
nacos/bindirectory.Start the server:
Linux, UNIX, or macOS: Run
sh startup.sh -m standalone.Windows: In
startup.cmd, setset MODE="standalone", then run the file.
(Optional) Interconnection between on-premises and cloud applications configured
Create a service provider
Step 1: Set up the Maven project
Create a Maven project named spring-boot-dubbo-provider and add the following dependencies to pom.xml. This example uses Spring Boot 2.0.6.RELEASE.
<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>Step 2: Define the service interface
In src/main/java, create the package com.alibaba.edas.boot and add the IHelloService interface:
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}Step 3: Implement the service
In the same package, create 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)";
}
}The @Service annotation here is com.alibaba.dubbo.config.annotation.Service from Apache Dubbo, not the Spring Framework @Service annotation.
Step 4: Configure the application
Create application.properties (or application.yaml) in src/main/resources:
# 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:8848The following table describes each property.
| Property | Description |
|---|---|
dubbo.scan.basePackages | Packages to scan for com.alibaba.dubbo.config.annotation.Service and com.alibaba.dubbo.config.annotation.Reference. Separate multiple packages with commas (,). Required; no default value. |
dubbo.application.name | Application name registered with Nacos. Required; no default value. |
dubbo.registry.address | Must start with nacos://, followed by the Nacos Server IP address and port. For local development, use 127.0.0.1:8848. When you deploy to EDAS with an EDAS-managed registry, EDAS replaces this address automatically. For a self-managed Nacos registry, replace this value with your registry address. Required; no default value. |
Step 5: Create the application entry class
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);
}
}Step 6: Verify the provider
Open the Nacos console at http://127.0.0.1:8848 and click Services in the left-side navigation pane. Confirm that com.alibaba.edas.boot.IHelloService appears in the services list with the correct service group and provider IP address.
Create a service consumer
Step 1: Set up the Maven project
Create a Maven project named spring-boot-dubbo-consumer and add the following dependencies to pom.xml. This example uses Spring Boot 2.0.6.RELEASE.
<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>Spring Boot 1.x reached end of life in August 2019. If you must use Spring Boot 1.x, use version 1.5.x with com.alibaba.boot:dubbo-spring-boot-starter version 0.1.0. We recommend upgrading to a newer version.
Step 2: Define the service interface
In src/main/java, create the package com.alibaba.edas.boot and add the same IHelloService interface used in the provider:
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}Step 3: Implement the service call
Create a REST controller that calls the remote Dubbo service:
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);
}
}The @Reference annotation is com.alibaba.dubbo.config.annotation.Reference from Apache Dubbo.
Step 4: Configure the application
Add the following properties to application.properties or application.yaml:
dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=nacos://127.0.0.1:8848| Property | Description |
|---|---|
dubbo.application.name | Application name registered with Nacos. Required; no default value. |
dubbo.registry.address | Must start with nacos://, followed by the Nacos Server IP address and port. Replace 127.0.0.1 with your Nacos Server address if it runs on a different machine. Required; no default value. |
Step 5: Create the application entry class
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);
}
}Step 6: Verify the consumer
Open the Nacos console at http://127.0.0.1:8848 and click Services in the left-side navigation pane. Confirm that com.alibaba.edas.boot.IHelloService appears in the services list with the correct service group and caller IP address.
Test the local setup
After both the provider and consumer are running, send a test request:
curl http://localhost:8080/sayHello/EDASExpected response:
Hello, EDAS (from Dubbo with Spring Boot)Deploy to EDAS
Application Configuration Management (ACM), integrated in EDAS, provides a commercial version of Nacos. When you deploy to EDAS, it automatically replaces the on-premises Nacos Server address (127.0.0.1:8848) with the managed registry address.
EDAS supports deployment to the following cluster types. Choose a cluster type and deployment method based on your requirements.
| Cluster type | Documentation |
|---|---|
| Elastic Compute Service (ECS) | Deploy to an ECS cluster |
| Container Service for Kubernetes (ACK) | Deploy to a Kubernetes cluster |
Package the application for console deployment
To deploy through the EDAS console, add the Spring Boot Maven plugin to pom.xml and build the JAR package before uploading.
Provider pom.xml:
<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 pom.xml:
<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>Build the JAR package:
mvn clean packageMigration note
Avoid using edas-dubbo-extension to deploy Dubbo applications to EDAS. This extension does not support EDAS features such as Dubbo service governance. Migrate to Nacos instead.