Microservices Engine (MSE) provides managed service registries based on Nacos, ZooKeeper, or Eureka. This guide walks you through registering a Dubbo provider and consumer with an MSE Nacos instance so that the consumer can discover and call the provider.
How it works
A Dubbo provider starts and registers its service interface with the Nacos instance on MSE.
A Dubbo consumer subscribes to the same Nacos instance to discover available providers.
The consumer invokes the provider through the Dubbo RPC protocol using the service interface.
Prerequisites
Before you begin, make sure that you have:
Maven installed and configured in your environment variables
An MSE Nacos engine instance. For details, see Create a Nacos engine
A namespace in the Nacos instance. For details, see Create a namespace
This guide uses the default Public namespace.
Set up the provider
Create a Dubbo provider that exposes a service interface and registers it with the MSE Nacos instance.
Step 1: Create a Maven project and add dependencies
Create a Maven project in your IDE (IntelliJ IDEA, Eclipse, or similar).
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>
Step 2: Define and implement the service interface
Dubbo services are exposed as Java interfaces. Define the interface first, then provide an implementation.
Create a package named
com.alibaba.msein thesrc/main/javadirectory.In the
com.alibaba.msepackage, create theIHelloServiceinterface with asayHellomethod:package com.alibaba.mse; public interface IHelloService { String sayHello(String str); }Create the
IHelloServiceImplclass in the same package to implement the interface:package com.alibaba.mse; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
Step 3: Configure the Dubbo service
Create a
provider.xmlfile in thesrc/main/resourcesdirectory.Add the Spring and Dubbo XML namespace declarations:
<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">Expose the interface as a Dubbo service and point the registry to your MSE Nacos instance: Replace
mse.XX.nacos.mse.aliyuncs.comwith the actual endpoint of your MSE Nacos instance. Find this endpoint in the Access Method column on the instance list page of the MSE console.NoteTo use the public endpoint, configure an empty whitelist first. For details, see Configure a public IP address whitelist.
NoteIf your service registry is an MSE ZooKeeper instance, replace the registry address accordingly. For details, see Usage notes.
<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"/> <!-- Replace mse.XX.nacos.mse.aliyuncs.com with your Nacos instance endpoint --> <dubbo:registry address="nacos://mse.XX.nacos.mse.aliyuncs.com:8848" />
Step 4: Start the provider
Create a
Providerclass in thecom.alibaba.msepackage. This class loads the Spring context and starts the Dubbo service: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 of theProviderclass to start the service.
Verify provider registration
Log on to the MSE console.
In the left-side navigation pane, choose Microservices Registry > Instances, then click the target MSE instance.
Configure a whitelist for the instance. If no IP addresses or CIDR blocks are added to the whitelist, all IP addresses are allowed to access the instance.
In the left-side navigation pane of the instance details page, choose Service Management > Services. If the provider service appears in the list, the registration succeeded.
Set up the consumer
Create a Spring Boot consumer application that subscribes to the Dubbo service through the same MSE Nacos instance.
Step 1: Create a Maven project and add dependencies
Create a Maven project named
spring-boot-dubbo-consumerin your IDE.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>
Step 2: Define the service interface
Create a package named
com.alibaba.msein thesrc/main/javadirectory.Create the same
IHelloServiceinterface used by the provider:package com.alibaba.mse; public interface IHelloService { String sayHello(String str); }
Step 3: Create a controller to call the provider
Create a
DemoConsumerControllerclass in thecom.alibaba.msepackage. Use the@Referenceannotation to inject the remote Dubbo service:NoteThe
@Referenceannotation iscom.alibaba.dubbo.config.annotation.Reference.package com.alibaba.mse; 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); } }
Step 4: Configure the registry address
Add the following properties to the application.properties file:
server.port=8080
dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=nacos://mse.XX.nacos.mse.aliyuncs.com:8848Replace mse.XX.nacos.mse.aliyuncs.com with your MSE Nacos instance endpoint.
The dubbo.registry.address value must start with nacos://, followed by the endpoint and port of the MSE Nacos instance.
Step 5: Start the consumer
Create the Spring Boot entry class
SpringBootDubboConsumerApplicationand start the application:package com.alibaba.mse; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDubboConsumerApplication.class, args); } }
Verify consumer registration
Log on to the MSE console.
In the left-side navigation pane, choose Microservices Registry > Instances, then click the target MSE instance.
Configure a whitelist for the instance. If no IP addresses or CIDR blocks are added to the whitelist, all IP addresses are allowed to access the instance.
In the left-side navigation pane of the instance details page, choose Service Management > Services. If the consumer service appears in the list, the registration succeeded.
Verify end-to-end service invocation
With both the provider and consumer running, test that the consumer can call the provider.
Run the following command:
curl http://localhost:8080/sayHello/mseExpected output:
Hello, mseThis response confirms that the consumer discovered and called the provider through the MSE Nacos service registry.