In addition to the traditional XML configuration method, you can use Spring Boot to develop Dubbo applications. This method is especially suitable for developers who are not proficient in Java, have little experience with Maven, or are unfamiliar with the Dubbo framework. This topic describes how to use Spring Boot to develop a Dubbo application from scratch and use the SAE service registry for service registration and discovery.
Prerequisites
Download Maven and configure the environment variables.
Download the latest version of Nacos Server.
Start Nacos Server.
Decompress the downloaded Nacos Server package.
Go to the
nacos/binpath 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 standalonecommand.Windows: In the
startup.cmdfile, configure the MODE parameter by using theset MODE="standalone"command. Then, run the file.
Why use Spring Boot to develop Dubbo applications
Spring Boot simplifies the configuration and deployment of microservice applications. Nacos provides features for service registration, discovery, and configuration management. By integrating Spring Boot with Nacos, you can quickly build Spring-based Dubbo services with higher development efficiency than the traditional XML-based method.
You can use the following two methods to develop Dubbo applications based on Spring Boot:
Develop applications using XML configuration files.
Develop applications using Spring Boot annotations.
For more information about developing applications with XML configuration files, see Host Dubbo applications in SAE. This topic describes how to develop Dubbo services using Spring Boot annotations.
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
Create a Maven project named
spring-boot-dubbo-provider.Add the required dependencies to the
pom.xmlfile.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>Develop a Dubbo service provider.
All Dubbo services are provided by using interfaces.
In the
src/main/javapath, create a package namedcom.alibaba.edas.boot.In the
com.alibaba.edas.bootpackage, create an interface namedIHelloServicethat contains a method namedSayHello.package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); }In the
com.alibaba.edas.bootpackage, create a class namedIHelloServiceImplto 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)"; } }NoteIn 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.
Configure a Dubbo service.
In the
src/main/resourcespath, create theapplication.propertiesorapplication.yamlfile and open the file.Add the following configuration items to the
application.propertiesorapplication.yamlfile:# 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:8848NoteYou 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.basePackagesis the packages whose code containscom.alibaba.dubbo.config.annotation.Serviceandcom.alibaba.dubbo.config.annotation.Reference. Separate multiple packages with commas (,).The value of
dubbo.registry.addressmust 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 Enterprise Distributed Application Service (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.
Develop and start the
DubboProviderentry 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); } }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.IHelloServiceis displayed. You can query the service group and provider IP address of the service.
Create a service consumer
Create a Maven project named
spring-boot-dubbo-consumer.Add the required dependencies to the
pom.xmlfile.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.
NoteThe lifecycle of Spring Boot 1.x ended in August 2019. We recommend that you use a new version to develop your application.
Develop a Dubbo service consumer.
In the
src/main/javapath, create a package namedcom.alibaba.edas.boot.In the
com.alibaba.edas.bootpackage, create an interface namedIHelloServicethat contains a method namedSayHello.package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); }
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); } }NoteThe Reference annotation is com.alibaba.dubbo.config.annotation.Reference.
Add the following configuration items to the
application.properties or application.yamlfile:dubbo.application.name=dubbo-consumer-demo dubbo.registry.address=nacos://127.0.0.1:8848NoteYou 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.addressmust start withnacos://, 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.
Develop and start the
DubboConsumerentry 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); } }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.IHelloServiceis displayed. You can view the service group and caller IP address of the service.
Verify the result
`curl http://localhost:8080/sayHello/SAE`
`Hello, SAE (from Dubbo with Spring Boot)` Deploy the application to SAE
You can deploy an application that uses an on-premises Nacos instance as the registry to SAE without modifying the application. SAE automatically replaces the on-premises registry with the SAE registry at runtime.
You can deploy applications using the console or other tools. For more information, see Application deployment.
Before you deploy an application using the console, perform the following steps to compile the application into an executable JAR or WAR package.
Add the following configurations of the packaging plug-in to the
pom.xmlfile.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 the mvn clean package command to build your on-premises program into a JAR package.