This topic describes how to develop a sample Dubbo microservice application locally using XML configurations and then deploy the application to Serverless App Engine (SAE). The example application contains a service provider (Provider) and a service consumer (Consumer).
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 standalonecommand.Windows: Run the
startup.cmd -m standalonecommand.
Notestandaloneindicates 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.
Version guide
The lifecycles of Dubbo 2.7.x and Dubbo 3.0.x ended in March 2023. We recommend that you use a later version to develop your applications. This topic uses Dubbo 3.2.15 as an example to describe how to host Dubbo applications on SAE.
Step 1: Create a provider
Create a provider application project in your local environment, add dependencies, configure service registration and discovery, and specify Nacos as the service registry.
Create a Maven project and add dependencies.
Use an IDE such as IntelliJ IDEA or Eclipse to create a Maven project.
Add the dubbo, dubbo-registry-nacos, and nacos-client dependencies to the
pom.xmlfile.<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.2.15</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>3.2.15</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 through interfaces.
In the src/main/java path, create a package
com.alibaba.edas.In
com.alibaba.edas, create an interfaceIHelloServicethat contains asayHellomethod.package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }In
com.alibaba.edas, create a classIHelloServiceImplthat implements this interface.package com.alibaba.edas; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
Configure the Dubbo service.
In the src/main/resources path, create and open the
provider.xmlfile.In the
provider.xmlfile, add the XML Namespace (xmlns) and XML Schema Instance (xmlns:xsi) for Spring, and the Namespace (xmlns:dubbo) and Schema Instance (xsi:schemaLocation) for Dubbo.<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 the
provider.xmlfile, 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 the
provider.xmlfile, specify the local Nacos Server as the service registry.<dubbo:registry address="nacos://127.0.0.1:8848" />127.0.0.1is the address of the Nacos Server. If your Nacos Server is deployed on another server, you must change this value to the IP address of that server. After you deploy the application to SAE, you do not need to make any changes. The service registry address is automatically replaced with the address of the service registry on SAE.8848is the port for the Nacos Server and cannot be modified.
Start the service.
In the
com.alibaba.edaspackage, create the Provider class. In its main function, use the following code to load the Spring Context and 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(); } }Run the main function of the Provider to start the service.
Log on to the Nacos console at
http://127.0.0.1:8848. In the navigation pane on the left, click Service List to view the provider list.The service provider list contains
com.alibaba.edas.IHelloService. You can also query the Service Group and Provider IP of the service.
Step 2: Create a consumer
Create a consumer application project in your local environment, add dependencies, and configure the service subscription.
Create a Maven project and add dependencies.
Use an IDE such as IntelliJ IDEA or Eclipse to create a Maven project.
Add the dubbo, dubbo-registry-nacos, and nacos-client dependencies to the
pom.xmlfile.<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.2.15</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>3.2.15</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.1.1</version> </dependency> </dependencies>
Develop a Dubbo service consumer.
All Dubbo services are provided through interfaces.
In the src/main/java path, create a package named
com.alibaba.edas.In the
com.alibaba.edaspackage, create an interfaceIHelloServicethat contains asayHellomethod.NoteIn most cases, an interface is defined in an independent module. The provider and consumer then reference the same module using Maven dependencies. In this topic, two identical interfaces are created for the provider and consumer. However, this method is not recommended for production scenarios.
package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
Configure the Dubbo service.
In the src/main/resources path, create and open a
consumer.xmlfile.In the
consumer.xmlfile, add the XML Namespace (xmlns) and XML Schema Instance (xmlns:xsi) for Spring, and the Namespace (xmlns:dubbo) and Schema Instance (xsi:schemaLocation) for Dubbo.<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 the
consumer.xmlfile 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"/>
Start and verify the service.
In the
com.alibaba.edaspackage, create the `Consumer` class. In its `main` function, use the following code to load the Spring Context and 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(); } } } }Run the main function of the Consumer to start the service.
Verify the results.
After startup, the console continuously prints
hello world, which indicates that the service is successfully consumed.Log on to the Nacos console at
http://127.0.0.1:8848and click Services in the navigation pane on the left. On the Services page, select Callers.The service
com.alibaba.edas.IHelloServiceis displayed. You can also view its Service Group and Subscriber IP.
Step 3: Deploy to SAE
Add the following configuration to the pom.xml files of both the provider and the consumer. Then, run the mvn clean package command to compile the local project 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.sae.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.sae.Consumer</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build>
Deploy the compiled provider and consumer application packages to SAE. For more information, see Deploy a Java application.