This topic provides an example on how to use XML configuration files to develop two Dubbo microservice applications that are used as a provider and a consumer respectively in an on-premises environment. This topic also describes how to deploy the applications on Serverless App Engine (SAE).
Preparations
Download Maven and configure the environment variables.
Start Nacos Server.
Decompress the downloaded Nacos Server package.
Go to the
nacos/bin
directory and start Nacos Server.On Linux, UNIX, or macOS, run the
sh startup.sh -m standalone
command.On Windows, double-click the
startup.cmd
file to run the file.
Versions
SAE allows you to host applications that use Dubbo 2.5.x, 2.6.x, and 2.7.x. We recommended that you use Dubbo 2.7.x. Dubbo 2.7.x provides better service governance capabilities. In this example, Dubbo 2.7.3 is used to describe how to host Dubbo applications to SAE.
Step 1: Create a provider
Create a provider application project in an on-premises environment, add dependencies, configure service registration and discovery, and specify Nacos as the service registry.
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 the dubbo, dubbo-registry-nacos, and nacos-client dependencies 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>
Create a Dubbo provider.
All Dubbo services are provided by using interfaces.
Create a package named
com.alibaba.edas
in src/main/java.Create an interface named
IHelloService
that contains aSayHello
method in thecom.alibaba.edas
package.package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
Create a class named
IHelloServiceImpl
in thecom.alibaba.edas
package to implement the interface.package com.alibaba.edas; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
Configure a Dubbo service.
Create a file named
provider.xml
in the src/main/resources directory and open the file.In the
provider.xml
file, add the XML namespace (xmlns) and XML schema instance (xmlns:xsi) for Spring Cloud, and add the XML namespace (xmlns:dubbo) and XML 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.xml
file, expose the interface and 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.xml
file, specify on-premises Nacos Server as the service registry.<dubbo:registry address="nacos://127.0.0.1:8848" />
127.0.0.1
indicates the IP address of your Nacos Server. If your Nacos Server is deployed on another server, you must change the value to the IP address of the server. After you deploy the application on SAE, the registry address is automatically changed to the address of the registry in SAE.8848
indicates the port number of Nacos Server. The port number cannot be changed.
Start the Dubbo service.
Create a class named Provider in the
com.alibaba.edas
package and load the Spring context to the main function of the Provider class based on the following code to expose the 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 the Provider class to start the Dubbo service.
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, view the list of service providers.com.alibaba.edas.boot.IHelloService
is displayed in the list of service providers. You can also query the service group and provider IP address of the service.
Step 2: Create a consumer
Create a consumer application project in the on-premises environment, add dependencies, and then add the configurations that are used to subscribe to the Dubbo service.
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 the dubbo, dubbo-registry-nacos, and nacos-client dependencies 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>
Create a Dubbo consumer.
All Dubbo services are provided by using interfaces.
Create a package named
com.alibaba.edas
in src/main/java.Create an interface named
IHelloService
that contains aSayHello
method in thecom.alibaba.edas
package.NoteIn most cases, an interface is defined in an independent module. The provider and consumer reference the same module by using Maven dependencies. In this topic, two identical interfaces are created for the provider and consumer. However, we do not recommend that you use this method in actual scenarios.
package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
Configure a Dubbo service.
Create a file named
consumer.xml
in the src/main/resources directory and open the file.In the
consumer.xml
file, add the XML namespace (xmlns) and XML schema instance (xmlns:xsi) for Spring Cloud, and add the XML namespace (xmlns:dubbo) and XML 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
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"/>
Start and verify the Dubbo service.
Create a class named Consumer in the
com.alibaba.edas
package and load the Spring context to the main function of the Consumer class 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 the Consumer class to start the Dubbo service.
Verify the result.
After the Dubbo service is started, the console continuously returns
hello world
, which indicates that the service consumption is successful.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.com.alibaba.edas.IHelloService
is displayed in the list of service consumers. You can also view the service group and caller IP address of the service.
Step 3: Deploy the applications on SAE
Add the following configuration to the pom.xml files of Provider and Consumer, and run mvn clean package to compile the on-premises applications into executable JAR packages.
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 on SAE. For more information, see Deploy a microservices application by using a JAR package in the SAE console.