Spring Boot simplifies the configuration and deployment of microservice-oriented applications.
Nacos provides the service registration and discovery as well as configuration management
features. Using Spring Boot and Nacos together can help you improve development efficiency.
This topic describes how to use Spring Boot annotations to develop a sample Dubbo
microservice-oriented application based on Nacos.
Prerequisites
Before using Spring Boot to develop a Dubbo microservice-oriented application, complete
the following tasks:
- Download Maven and set the environment variables.
- Download the latest version of Nacos Server.
-
Start Nacos Server.
- Decompress the downloaded Nacos Server package.
- Go to the
nacos/bin
directory and start Nacos Server as follows:
- For Linux, UNIX, or MacOS: Run the
sh startup.sh -m standalone
command.
- For Windows: Double-click the
startup.cmd
file to run the file.
-
Sample project
You can follow the steps described in this topic to build the project. Alternatively,
you can directly download the sample 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 many demos. The demo used in this topic can be found in alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot
.
Create a service provider
- Create a Maven project named
spring-boot-dubbo-provider
.
- Add required dependencies to the
pom.xml
file.
The following takes Spring Boot 2.0.6.RELEASE as an 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>
- Develop a Dubbo service provider.
All services in Dubbo are provided as interfaces.
- Create a package named
com.alibaba.edas.boot
in src/main/java
.
- Create an interface named
IHelloService
that contains a SayHello
method in com.alibaba.edas.boot
.
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}
- Create a class named
IHelloServiceImpl
to implement the interface in com.alibaba.edas.boot
.
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 In Dubbo, the service annotation is com.alibaba.dubbo.config.annotation.Service.
- Configure the Dubbo service.
- In
src/main/resources
, create a file named application.properties
or application.yaml
and open it.
- In
application.properties
or application.yaml
, add the following configuration items.
# 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 defaults.
- The value of
dubbo.scan.basePackages
is the name of the package with code containing annotations com.alibaba.dubbo.config.annotation.Service
and com.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 port of Nacos Server. The IP address in the code
example is a local address. If your Nacos Server is deployed on another machine, change
it to the corresponding IP address.
- Develop and start the Spring Boot main 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);
}
}
- Log on to the Nacos console at
http://127.0.0.1:8848
. In the left-side navigation pane, click Services to view the list of providers.
You can see that com.alibaba.edas.boot.IHelloService
is available in the list of providers. In addition, you can query Service Group and
Provider IP of the service.
Create a service consumer
- Create a Maven project named
spring-boot-dubbo-consumer
.
- Add required dependencies to the
pom.xml
file.
The following takes Spring Boot 2.0.6.RELEASE as an 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, select Spring Boot 1.5.x. The corresponding com.alibaba.boot:dubbo-spring-boot-starter
version is 0.1.0.
Note Spring Boot 1.x has reached the end of life in August 2019. We recommend that you
use a later version to develop applications.
- Develop a Dubbo consumer.
- Create a package named
com.alibaba.edas.boot
in src/main/java
.
- Create an interface named
IHelloService
that contains a SayHello
method in com.alibaba.edas.boot
.
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}
- Develop a Dubbo service call.
For example, you need to call a remote Dubbo service once in a controller. The code
is as follows.
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.
- 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 defaults.
- The value of
dubbo.registry.address
must start with nacos://
, followed by the IP address and port of Nacos Server. The IP address in the code
example is a local address. If your Nacos Server is deployed on another machine, change
it to the corresponding IP address.
- Develop and start the Spring Boot main 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);
}
}
- 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, click the Callers tab to view the list of callers.
You can see that com.alibaba.edas.boot.IHelloService
is available in the list. In addition, you can view the 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
You can deploy the application that uses local Nacos as the registry directly to Enterprise
Distributed Application Service (EDAS) without making any changes. This registry will
be automatically replaced with the registry in EDAS.
Based on your actual needs, you can choose to deploy the application in an Elastic
Compute Service (ECS) cluster or Container Service Kubernetes cluster by using the
console or command line tools. For more information, see the following documentation.
If you use the console for deployment, follow these steps in your local application
before deploying it:
- 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>
- Run mvn clean package to package your local program into a JAR file.
References
- In addition to Spring Boot, you can also develop Dubbo microservice-oriented applications
by using XML. For more information, see Host Dubbo applications to EDAS.
- If you are using
edas-dubbo-extension
, see Host Dubbo applications to EDAS with edas-dubbo-extension. If you use edas-dubbo-extension
to host Dubbo applications, you cannot use related capabilities provided by EDAS,
such as Dubbo service governance. Therefore, we recommend that you migrate to Nacos
instead.