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.
Decompress the downloaded Nacos Server package.
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: In the
startup.cmd
file, configure the MODE parameter by using theset MODE="standalone"
command. Then, run the file.
(Optional): Implement interconnection between on-premises and cloud applications.
You can use Cloud Native App Initializer to create a Spring Boot project. For more information, see Cloud Native App Initializer.
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.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>
Develop a Dubbo service provider.
All Dubbo services are provided by using interfaces.
In the
src/main/java
path, create a package namedcom.alibaba.edas.boot
.In the
com.alibaba.edas.boot
package, create an interface namedIHelloService
that contains a method namedSayHello
.package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); }
In the
com.alibaba.edas.boot
package, create a class namedIHelloServiceImpl
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)"; } }
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/resources
path, create theapplication.properties
orapplication.yaml
file and open the file.Add the following configuration items to the
application.properties
orapplication.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
NoteYou 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 containscom.alibaba.dubbo.config.annotation.Service
andcom.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 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
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); } }
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
Create a Maven project named
spring-boot-dubbo-consumer
.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.
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/java
path, create a package namedcom.alibaba.edas.boot
.In the
com.alibaba.edas.boot
package, create an interface namedIHelloService
that 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.yaml
file:dubbo.application.name=dubbo-consumer-demo dubbo.registry.address=nacos://127.0.0.1:8848
NoteYou 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 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
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); } }
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.
Verify the result
`curl http://localhost:8080/sayHello/EDAS`
`Hello, EDAS (from Dubbo with Spring Boot)`
Deploy the application to EDAS
Application Configuration Management (ACM) that is integrated in EDAS provides an official 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:
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>
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.