In this tutorial, you build a service registry on Microservices Engine (MSE) for an Apache Dubbo application and use it for service registration and discovery. By the end, a Dubbo consumer that runs on your local machine calls a provider that it discovers through an MSE Nacos registry.
Architecture
The example application is a Dubbo microservice application that consists of a provider and a consumer. You develop the two roles as two separate Maven projects on your local machine, and both connect to the same registry instance on MSE over its public endpoint.
Provider project — Exposes the
com.alibaba.mse.IHelloServiceinterface as a Dubbo service on port 28082 and registers the service with the registry instance on MSE.Consumer project — Subscribes to
com.alibaba.mse.IHelloServicethrough the same registry instance and calls the provider.Registry instance on MSE — Acts as the service registry that the provider registers with and the consumer subscribes to.
Registry types
MSE can host a Nacos, ZooKeeper, or Eureka service registry for a Dubbo application. This tutorial uses MSE Nacos, and every configuration example in this topic targets an MSE Nacos instance.
Nacos (used in this tutorial) — Set the Dubbo registry address to the public endpoint of your MSE Nacos instance. No other registry-specific configuration is required.
ZooKeeper — If you use MSE ZooKeeper as your service registry, replace the registry code in Step 2 with the corresponding code for ZooKeeper. For more information, see Usage notes.
Prerequisites
Download Maven and set the environment variables.
An IDE, such as IntelliJ IDEA or Eclipse, is installed on your local machine.
An MSE Nacos instance. For instructions, see Create a Nacos engine instance
Create a namespace. This tutorial uses the default namespace,
Public.
Step 1: Create the provider project and define the service
In this step, you create the Maven project for the provider and define the service that the provider exposes. In Dubbo, all services are provided as interfaces.
In your IDE, create a Maven project for the provider.
Add the
dubboandnacos-clientdependencies to thepom.xmlfile.<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.9</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>1.4.2</version> </dependency> </dependencies>In the
src/main/javadirectory, create a package namedcom.alibaba.mse.In
com.alibaba.mse, create an interface namedIHelloServicethat contains asayHellomethod.package com.alibaba.mse; public interface IHelloService { String sayHello(String str); }In
com.alibaba.mse, create a class namedIHelloServiceImplthat implements the interface.package com.alibaba.mse; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
Step 2: Configure the provider to use MSE Nacos as the registry
In this step, you create the provider.xml file, which exposes IHelloService as a Dubbo service and points the provider at the MSE Nacos instance. Get the endpoint of the instance first, because the registry configuration requires it.
Log on to the MSE console and view the public endpoint of the Nacos instance that you created on MSE. The public endpoint is shown in the Access Method column of the instance list and is in the
mse.XX.nacos.mse.aliyuncs.comformat.In the
src/main/resourcesdirectory of the provider project, create a file namedprovider.xml.Add the following configuration to
provider.xml. Replacemse.XX.nacos.mse.aliyuncs.comwith the public endpoint of your Nacos instance.<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"> <dubbo:application name="demo-provider"/> <dubbo:protocol name="dubbo" port="28082"/> <dubbo:service interface="com.alibaba.mse.IHelloService" ref="helloService"/> <bean id="helloService" class="com.alibaba.mse.IHelloServiceImpl"/> <dubbo:registry address="nacos://mse.XX.nacos.mse.aliyuncs.com:8848" /> </beans>
The following table describes the configuration in provider.xml.
| Element | Description |
xmlns, xmlns:xsi, xmlns:dubbo, and xsi:schemaLocation | Declares the Spring XML Namespace (xmlns) and XML Schema Instance (xmlns:xsi), and the Dubbo Namespace (xmlns:dubbo) and Schema Instance (xsi:schemaLocation). |
dubbo:application | The name of the provider application. This example uses demo-provider. |
dubbo:protocol | Publishes the service over the dubbo protocol on port 28082. |
dubbo:service and bean | Expose the IHelloService interface and the IHelloServiceImpl implementation class as a Dubbo service. |
dubbo:registry | Specifies the Nacos instance that you created on MSE as the service registry. The address is the public endpoint of the instance. |
Step 3: Allow the provider to access the MSE instance
The provider reaches the registry over the public endpoint of the MSE instance, so the whitelist of the instance must permit the traffic. Complete this step before you start the provider. Otherwise, registration fails.
In the MSE console, in the left-side navigation pane, choose . On the Instances page, click the MSE instance that you created.
Clear the whitelist of the instance. If you do not specify any IP address or mask, all addresses can access the instance. This tutorial uses an empty whitelist as an example. For more information, see Set a whitelist.
An empty whitelist allows all addresses to access the MSE instance.
Step 4: Start the provider and verify the registration
In this step, you start the provider and confirm in the MSE console that the service is registered with the Nacos instance.
In
com.alibaba.mse, create a class namedProvider. Use the following code to load the Spring context in themainmethod ofProviderand expose the Dubbo service that you configured.package com.alibaba.mse; 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
mainmethod ofProviderto start the service. Keep the process running until you finish the tutorial, because the consumer calls this process.In the MSE console, on the details page of your MSE instance, choose in the left-side navigation pane.
Verify that the service list contains
com.alibaba.mse.IHelloService, the interface that thedemo-providerapplication exposes.
Do not continue to the next step until the interface appears in the service list. If the interface does not appear, check the following configurations:
Whitelist — The whitelist of the MSE instance permits the address of your local machine. An empty whitelist permits all addresses.
Registry address — The
dubbo:registryaddress inprovider.xmluses the public endpoint of your instance, not themse.XX.nacos.mse.aliyuncs.complaceholder.Registry type — The
dubbo:registryaddress uses the scheme of the instance type that you created, such asnacos://for an MSE Nacos instance.
Step 5: Create the consumer and subscribe to the service
In this step, you create a second Maven project for the consumer, subscribe to com.alibaba.mse.IHelloService through the same MSE Nacos instance, and implement the class that calls the provider.
In your IDE, create a second Maven project for the consumer.
Add the
dubboandnacos-clientdependencies to thepom.xmlfile of the consumer project. The dependencies are identical to those of the provider project in Step 1.In the
src/main/javadirectory, create thecom.alibaba.msepackage and add theIHelloServiceinterface that you defined in Step 1. The consumer references the service by its interface, so the interface must be on the classpath of the consumer project.In the
src/main/resourcesdirectory of the consumer project, create a file namedconsumer.xml. Replacemse.XX.nacos.mse.aliyuncs.comwith the public endpoint of your Nacos instance, which is the same endpoint that you configured inprovider.xml.<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"> <dubbo:application name="demo-consumer"/> <dubbo:registry address="nacos://mse.XX.nacos.mse.aliyuncs.com:8848" /> <dubbo:reference id="helloService" interface="com.alibaba.mse.IHelloService"/> </beans>The
dubbo:registryelement points the consumer at the same registry as the provider, and thedubbo:referenceelement subscribes to thecom.alibaba.mse.IHelloServiceinterface that the provider exposes.In
com.alibaba.mse, create a class namedConsumer. Use the following code to load the Spring context in themainmethod ofConsumerand call thesayHellomethod of the subscribed service.package com.alibaba.mse; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"}); context.start(); IHelloService helloService = (IHelloService) context.getBean("helloService"); System.out.println(helloService.sayHello("mse")); } }
Step 6: Verify the call from the consumer to the provider
In this step, you confirm that the consumer discovers the provider through the MSE Nacos registry and receives the response of the sayHello method. The consumer passes mse as the argument, and IHelloServiceImpl returns hello followed by that argument.
Confirm that the provider process that you started in Step 4 is still running.
Run the
mainmethod ofConsumer.Check the console output of the consumer project. The following output indicates that the consumer called the provider successfully:
hello mse
If the call fails or no output is returned, check the following configurations:
Provider availability — The provider process is running, and
com.alibaba.mse.IHelloServiceappears in the service list of the MSE instance.Registry address — The
dubbo:registryaddress inconsumer.xmlis the public endpoint that you configured inprovider.xml.Interface name — The interface in the
dubbo:referenceelement ofconsumer.xmlmatchescom.alibaba.mse.IHelloService, which is the interface in thedubbo:serviceelement ofprovider.xml.Whitelist — The whitelist of the MSE instance permits the address of your local machine.