This topic describes how to create a service registry on Microservices Engine (MSE) for Dubbo microservice applications. Dubbo microservice applications serve as providers and consumers. The service registry uses the ZooKeeper, Eureka, or Nacos engine, supports service registration and discovery of applications, and allows consumers to call providers.
Prerequisites
Before you create a service registry, make sure that the following operations are performed:
Maven is downloaded, and environment variables are configured.
A Nacos engine is created. For more information, see Create a Nacos engine.
A namespace is created. For more information, see the "Procedure" section in Create a namespace.
In this topic, the default namespace Public is used.
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 in an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.
Add the dubbo and nacos-client dependencies to the pom.xml file.
<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>
Develop a Dubbo service provider.
All Dubbo services are exposed as interfaces.
Create a package named
com.alibaba.msein thesrc/main/javadirectory.Create an interface named IHelloService that contains a SayHello method in the
com.alibaba.msepackage.package com.alibaba.mse; public interface IHelloService { String sayHello(String str); }Create a class named IHelloServiceImpl in the com.alibaba.mse package to implement the interface.
package com.alibaba.mse; 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/resourcesdirectory and open the file.In the
provider.xmlfile, add the XML namespace (xmlns) and XML schema instance (xmlns:xsi) for the Spring framework, and add the XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation) for the Dubbo framework.<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.mse.IHelloService" ref="helloService"/> <bean id="helloService" class="com.alibaba.mse.IHelloServiceImpl"/>In the provider.xml file, specify the on-premises Nacos server as the service registry.
<dubbo:registry address="nacos://mse.XX.nacos.mse.aliyuncs.com:8848" />NoteIf you want to use the public endpoint of the Nacos instance, configure an empty whitelist. For more information, see Configure a public IP address whitelist.
Find the Nacos instance in the instance list, and view the public endpoint of the Nacos instance in the Access Method column. The public endpoint is in the
mse.XX.nacos.mse.aliyuncs.comformat.NoteIf your service registry is an MSE Zookeeper instance, you must replace the code of the service registry in this step with the code of the ZooKeeper instance. For more information, see Usage notes.
Start the service.
Create a class named Provider in the com.alibaba.mse 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.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(); } }Invoke the main function of the Provider class to start the Dubbo service.
Verify the result.
Log on to the MSE console.
In the left-side navigation pane, choose . On the Instances page, click the MSE instance that you created.
Configure a whitelist for the MSE instance.
If you do not add IP addresses or CIDR blocks to the whitelist, all IP addresses are allowed to access the MSE instance.
In the left-side navigation pane of the instance details page, choose . If a provider service exists in the service list, the service is registered with the MSE instance.
Create a consumer
Create a Spring Boot consumer application project in the on-premises environment, add dependencies, and add the configurations that are used to subscribe to the Dubbo service.
Create a Maven project named spring-boot-dubbo-consumer and add dependencies.
Create a Maven project in an IDE, such as IntelliJ IDEA or Eclipse.
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>Create a Dubbo consumer.
Create a package named
com.alibaba.msein thesrc/main/javadirectory.Create an interface named
IHelloServicethat contains aSayHellomethod in thecom.alibaba.msepackage.package com.alibaba.mse; public interface IHelloService { String sayHello(String str); }
Call a remote Dubbo service in a controller by using the following code.
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); } }NoteThe Reference annotation is com.alibaba.dubbo.config.annotation.Reference.
Add the following configurations to the
application.propertiesconfiguration file.server.port=8080 dubbo.application.name=dubbo-consumer-demo dubbo.registry.address=nacos://mse.XX.nacos.mse.aliyuncs.com:8848NoteThe value of
dubbo.registry.addressmust start withnacos://. The IP address and port number that follow nacos:// represent the endpoint of the MSE Nacos server.Create and start the Spring Boot entry class
SpringBootDubboConsumerApplication.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 the result.
Log on to the MSE console.
In the left-side navigation pane, choose . On the Instances page, click the MSE instance that you created.
Configure a whitelist for the MSE instance.
If you do not add IP addresses or CIDR blocks to the whitelist, all IP addresses are allowed to access the MSE instance.
In the left-side navigation pane of the instance details page, choose . If the consumer service exists in the service list, the service registration is successful.
Verify the result
Run the curl http://localhost:8080/sayHello/mse command. If Hello, mse is returned, the consumer successfully calls the provider.
`curl http://localhost:8080/sayHello/mse`
`Hello, mse`