All Products
Search
Document Center

Microservices Engine:Create a service registry on MSE for Dubbo microservice applications

Last Updated:Oct 15, 2024

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.

  1. Create a Maven project and add dependencies.

    1. Create a Maven project in an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.

    2. 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>            
  2. Develop a Dubbo service provider.

    All Dubbo services are exposed as interfaces.

    1. Create a package named com.alibaba.mse in the src/main/java directory.

    2. Create an interface named IHelloService that contains a SayHello method in the com.alibaba.mse package.

      package com.alibaba.mse;
      
      public interface IHelloService {
         String sayHello(String str);
      }                                
    3. 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;
          }
      }                          
  3. Configure a Dubbo service.

    1. Create a file named provider.xml in the src/main/resources directory and open the file.

    2. In the provider.xml file, 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">                   
    3. 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"/>                                
    4. 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" />                                
      Note

      If 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.com format.

      Note

      If 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.

  4. Start the service.

    1. 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();
          }
      }                
    2. Invoke the main function of the Provider class to start the Dubbo service.

  5. Verify the result.

    1. Log on to the MSE console.

    2. In the left-side navigation pane, choose Microservices Registry > Instances. On the Instances page, click the MSE instance that you created.

    3. 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.

    4. In the left-side navigation pane of the instance details page, choose Service Management > Services. 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.

  1. Create a Maven project in an IDE, such as IntelliJ IDEA or Eclipse.

  2. 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>
  3. Create a Dubbo consumer.

    1. Create a package named com.alibaba.mse in the src/main/java directory.

    2. Create an interface named IHelloService that contains a SayHello method in the com.alibaba.mse package.

      package com.alibaba.mse;
      
      public interface IHelloService {
          String sayHello(String str);
      }
      
  4. 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);
        }
    }
    
    Note

    The Reference annotation is com.alibaba.dubbo.config.annotation.Reference.

  5. Add the following configurations to the application.properties configuration file.

    server.port=8080
    dubbo.application.name=dubbo-consumer-demo
    dubbo.registry.address=nacos://mse.XX.nacos.mse.aliyuncs.com:8848
    Note

    The value of dubbo.registry.address must start with nacos://. The IP address and port number that follow nacos:// represent the endpoint of the MSE Nacos server.

  6. 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);
        }
    } 
  1. Verify the result.

    1. Log on to the MSE console.

    2. In the left-side navigation pane, choose Microservices Registry > Instances. On the Instances page, click the MSE instance that you created.

    3. 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.

    4. In the left-side navigation pane of the instance details page, choose Service Management > Services. 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`            

References