Apache Dubbo (Dubbo) is a microservice framework that provides service discovery capabilities between service providers and service consumers. This topic uses the Dubbo microservice application as an example to describe how to develop applications on your computer by configuring XML files and host the applications to SAE.
Benefits
After you host Dubbo applications to SAE, you can focus on building the logic of Dubbo applications without worrying about the establishment and maintenance of the registry and configuration center. The features provided by SAE, such as auto scaling, quick start and stop of applications in a batch, and application monitoring, can help greatly reduce your development and O&M costs.
Preparations
- Download Maven and set the environment variables.
- Start Nacos Server.
- Download and decompress the Nacos Server package.
- Go to the nacos/bin directory and start Nacos Server.
- Linux, UNIX, or macOS: Run the
sudo sh startup.sh -m standalone
command. - Windows: Run the
startup.cmd -m standalone
command.
Notestandalone
indicates that the startup.cmd file is run in standalone mode, not cluster mode. By default, the startup.cmd file is started in cluster mode. If you double-click the startup.cmd file to run the file in a Windows system, the startup fails. In this case, you must configureMODE="standalone"
in the startup.cmd file. For more information, see Quick Start for Nacos. - Linux, UNIX, or macOS: Run the
Versions
SAE supports Dubbo 2.5.x, 2.6.x, and 2.7.x. We recommended that you use 2.7.x for better service governance. This topic takes the version 2.7.3 as an example to describe how to host Dubbo applications to SAE.
Step 1: Create a service provider
- Create a Maven project and add dependencies.
- Create a Maven project by using an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.
- Add dubbo, dubbo-registry-nacos, and nacos-client to the
pom.xml
file.<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</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
insrc/main/java
. - Create an interface named
IHelloService
that contains aSayHello
method incom.alibaba.edas
.package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
- Create a class named
IHelloServiceImpl
incom.alibaba.edas
to implement the interface.package com.alibaba.edas; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
- Create a package named
- Configure the Dubbo service.
- Create a file named
provider.xml
insrc/main/resources
and open the file. - In
provider.xml
, add Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
- In
provider.xml
, expose the interface and implementation class as a Dubbo service.<dubbo:application name="demo-provider"/> <dubbo:protocol name="dubbo" port="28082"/> <dubbo:service interface="com.alibaba.edas.IHelloService" ref="helloService"/> <bean id="helloService" class="com.alibaba.edas.IHelloServiceImpl"/>
- In
provider.xml
, specify Nacos Server that starts locally as the registry.<dubbo:registry address="nacos://127.0.0.1:8848" />
127.0.0.1
is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the IP address to the corresponding one. When an application is deployed in SAE, the registry address will be replaced with the address of the registry in SAE. You do not need to make any changes.8848
is the port number of Nacos Server, which cannot be changed.
- Create a file named
- Start the service.
- Create the class Provider in
com.alibaba.edas
and load Spring context to the main function of Provider based on the following code to expose the configured Dubbo service.package com.alibaba.edas; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"}); context.start(); System.in.read(); } }
- Execute the main function of Provider to start the service.
- Create the class Provider in
- 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 thatcom.alibaba.edas.IHelloService
is available in the list of providers. In addition, you can query Service Group and Provider IP of the service.
Step 2: Create a service consumer
- Create a Maven project and add dependencies.
- Create a Maven project by using an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.
- Add dubbo, dubbo-registry-nacos, and nacos-client to the
pom.xml
file.<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</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
insrc/main/java
. - Create an interface named
IHelloService
that contains aSayHello
method incom.alibaba.edas
.Note Generally, an interface is defined in an independent module. The provider and consumer reference the same module through Maven dependencies. In this topic, two identical interfaces are created for the provider and consumer for ease of description. However, we do not recommend this procedure in actual use.package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
- Create a package named
- Configure the Dubbo service.
- Create a file named
consumer.xml
insrc/main/resources
and open the file. - In
consumer.xml
, add the Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
- Add the following configuration to
consumer.xml
to subscribe to the Dubbo service:<dubbo:application name="demo-consumer"/> <dubbo:registry address="nacos://127.0.0.1:8848"/> <dubbo:reference id="helloService" interface="com.alibaba.edas.IHelloService"/>
- Create a file named
- Start and verify the Dubbo service.
- Create the class Consumer in
com.alibaba.edas
and load Spring context to the main function of Consumer based on the following code to subscribe to and consume the Dubbo service:package com.alibaba.edas; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.concurrent.TimeUnit; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); context.start(); while (true) { try { TimeUnit.SECONDS.sleep(5); IHelloService demoService = (IHelloService)context.getBean("helloService"); String result = demoService.sayHello("world"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } } }
- Execute the main function of Consumer to start the Dubbo service.
- Create the class Consumer in
- Verify the creation result.After the Dubbo service is started, the console outputs
hello world
continuously, indicating successful service consumption.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, select Callers.You can see that
com.alibaba.edas.IHelloService
is available in the list. In addition, you can query Service Group and Caller IP of the service.
Step 3: Deploy applications to SAE
- In the pom.xml file of the application, add the following configuration, and then run the mvn clean package command to compile the local program into an executable JAR package.
- 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.Provider</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.Consumer</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build>
- Provider
- Deploy the compiled provider and consumer application packages to SAE. For more information, see Deploy a microservices application by using a JAR package in the SAE console.
References
The following table describes the operations that you can perform on an application after you deploy the application on SAE.
Operation | References |
Lifecycle management operations, such as updating, starting, stopping, and deleting an application, and scaling in or scaling out the instances for an application | |
Performance optimization operations, such as configuring auto scaling policies for an application, binding Server Load Balancer (SLB) instances to an application, and starting or stopping applications in batches | |
Application status-based operations, such as managing logs, configuring monitoring settings, viewing application events, and viewing change records |