Use Spring Boot annotations to build a Dubbo service provider and consumer from scratch, register them with Nacos, and deploy to SAE. This approach removes the need for verbose XML configuration and is a good starting point if you're new to Maven or the Dubbo framework.
Prerequisites
Before you begin, ensure that you have:
Maven installed and its environment variables configured
The latest Nacos Server downloaded and running in standalone mode:
Decompress the Nacos Server package.
Go to the
nacos/bindirectory.Start Nacos Server:
Linux, UNIX, or macOS: Run
sh startup.sh -m standaloneWindows: Set
set MODE="standalone"instartup.cmd, then run the file
Demo project
Follow the steps in this topic to build the project from scratch, or download the demo project directly. You can also clone the repository:
git clone https://github.com/aliyun/alibabacloud-microservice-demo.gitThe demo for this topic is in alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot.
Create a service provider
Step 1: Set up the 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
All Dubbo services are exposed through interfaces.
In
src/main/java, create a package namedcom.alibaba.edas.boot.In that package, create an interface named
IHelloService:
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}Step 3: Implement the service
In the com.alibaba.edas.boot package, create a class named IHelloServiceImpl to implement IHelloService:
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@Serviceannotation here iscom.alibaba.dubbo.config.annotation.Serviceprovided by Dubbo — not the Spring@Serviceannotation.
Step 4: Configure the Dubbo service
In src/main/resources, create application.properties (or application.yaml) and add:
# Packages to scan for Dubbo annotations (@Service, @Reference)
dubbo.scan.basePackages=com.alibaba.edas.boot
dubbo.application.name=dubbo-provider-demo
dubbo.registry.address=nacos://127.0.0.1:8848All three configuration items are required — none have default values.
dubbo.scan.basePackages: the package(s) containing@Serviceand@Referenceannotations. Separate multiple packages with commas.
dubbo.registry.address: must start withnacos://, followed by the Nacos Server IP address and port. If you use the SAE-managed registry, SAE automatically replaces this address at runtime. If you use a self-managed Nacos instance, replace127.0.0.1:8848with its IP address and port.
Step 5: Start the provider
Create the Spring Boot main class DubboProvider and run it:
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);
}
}Verify the provider
Log in to the Nacos console at http://127.0.0.1:8848. In the left-side navigation pane, click Services. Confirm that com.alibaba.edas.boot.IHelloService appears in the service list, along with its service group and provider IP address.
Create a service consumer
Step 1: Set up the project
Create a Maven project named spring-boot-dubbo-consumer and add the following dependencies to pom.xml. This example also 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>If you need to use Spring Boot 1.x, use Spring Boot 1.5.x with com.alibaba.boot:dubbo-spring-boot-starter:0.1.0. Note that the lifecycle of Spring Boot 1.x ended in August 2019 — using a newer version is strongly recommended.Step 2: Reference the service interface
In src/main/java, create the package com.alibaba.edas.boot and add the same IHelloService interface as in the provider:
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}Step 3: Call the remote service
Create a controller that uses the @Reference annotation to inject the remote IHelloService and expose it via HTTP:
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@Referenceannotation here iscom.alibaba.dubbo.config.annotation.Referenceprovided by Dubbo.
Step 4: Configure the consumer
In src/main/resources, add the following to application.properties (or application.yaml):
dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=nacos://127.0.0.1:8848Both configuration items are required. If Nacos Server runs on a different host, replace 127.0.0.1:8848 with the actual IP address and port.Step 5: Start the consumer
Create the Spring Boot main class DubboConsumer and run it:
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);
}
}Verify the consumer
Log in to the Nacos console at http://127.0.0.1:8848. In the left-side navigation pane, click Services. Confirm that com.alibaba.edas.boot.IHelloService appears in the service list, along with its service group and caller IP address.
Verify service invocation
After both the provider and consumer are running, send a test request:
curl http://localhost:8080/sayHello/SAEExpected output:
Hello, SAE (from Dubbo with Spring Boot)Deploy to SAE
SAE automatically replaces the on-premises Nacos registry with the SAE service registry at runtime, so no code changes are required.
Before deploying via the console, build each application into an executable JAR or WAR package:
Add the Maven packaging plugin to
pom.xml. 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>Run
mvn clean packageto build the JAR file.Deploy the package to SAE. For deployment options, see Application deployment.
What's next
Host Dubbo applications in SAE — learn how to develop Dubbo services using XML configuration files instead of annotations
Application deployment — explore all deployment methods supported by SAE