All Products
Search
Document Center

Deploy Dubbo applications to EDAS

Last Updated: Sep 29, 2019

You can host Dubbo microservice applications in EDAS and take advantage of shared components, enterprise-class security hardening, and full microservice solutions provided by EDAS to reduce O&M costs and improve security and development efficiency. This topic describes how to develop a Dubbo microservice sample application on-premises through XML configuration items and deploy it to EDAS. The sample application contains a service provider and a service consumer.

Why host applications in EDAS

By hosting Dubbo applications in EDAS, you can focus on building the logic of Dubbo applications rather than concerning yourself with creating and maintaining the registry, configuration center, and metadata center. Also, you can take advantage of EDAS capabilities such as elastic scaling, rate limiting and degradation, monitoring, and microservice governance for various management purposes. The entire hosting process is completely transparent to you, does not require you to learn anything, and does not increase your development costs.

Preparations

Before you start developing, be sure to complete the following tasks:

  • Download Maven and set environment variables.

  • Download the latest version of Nacos Server.

  • To start Nacos Server, follow these steps.

    1. Decompress the downloaded Nacos Server package.
    2. Go to the nacos/bin directory and start Nacos Server.
      • For Linux/UNIX/Mac: Run the sh startup.sh -m standalone command.
      • For Windows: Double-click the startup.cmd file to run it.

Create a service provider

Create a provider application project on-premises, add dependencies, configure service registration and discovery, and specify Nacos as the registry.

  1. Create a Maven project and add dependencies.

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

    2. Add dubbo, dubbo-registry-nacos, and nacos-client to the pom.xml file.

      Note: Dubbo 2.7.3 or later is required.

      1. <dependencies>
      2. <dependency>
      3. <groupId>org.apache.dubbo</groupId>
      4. <artifactId>dubbo</artifactId>
      5. <version>2.7.3</version>
      6. </dependency>
      7. <dependency>
      8. <groupId>org.apache.dubbo</groupId>
      9. <artifactId>dubbo-registry-nacos</artifactId>
      10. <version>2.7.3</version>
      11. </dependency>
      12. <dependency>
      13. <groupId>com.alibaba.nacos</groupId>
      14. <artifactId>nacos-client</artifactId>
      15. <version>1.1.1</version>
      16. </dependency>
      17. </dependencies>
  2. Develop a Dubbo service provider.

    All services in Dubbo are provided as interfaces.

    1. In src/main/java, create a package named com.alibaba.edas.

    2. In com.alibaba.edas, create an interface named IHelloService that contains a SayHello method.

      1. package com.alibaba.edas;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
    3. Create a class named IHelloServiceImpl in com.alibaba.edas to implement the interface.

      1. package com.alibaba.edas;
      2. public class IHelloServiceImpl implements IHelloService {
      3. public String sayHello(String str) {
      4. return "hello " + str;
      5. }
      6. }
  3. Configure the Dubbo service.

    1. In src/main/resources, create a file named provider.xml and open it.

    2. In provider.xml, add the Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).

      1. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
      3. xmlns="http://www.springframework.org/schema/beans"
      4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      5. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    3. In provider.xml, expose the interface and implementation class as a Dubbo service.

      1. <dubbo:application name="demo-provider"/>
      2. <dubbo:protocol name="dubbo" port="28082"/>
      3. <dubbo:service interface="com.alibaba.edas.IHelloService" ref="helloService"/>
      4. <bean id="helloService" class="com.alibaba.edas.IHelloServiceImpl"/>
    4. In provider.xml, specify Nacos Server that starts locally as the registry.

      1. <dubbo:registry address="nacos://127.0.0.1:8848" />
      • 127.0.0.1 is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the IP address to the corresponding one. When an application is deployed to EDAS, the registry address will be replaced with the address of the registry in EDAS. You do not need to make any changes.
      • 8848 is the port number of Nacos Server, which cannot be changed.
  4. Start the service.

    1. In com.alibaba.edas, create the Provider class and load Spring context to the main function of Provider based on the following code to expose the configured Dubbo service.

      1. package com.alibaba.edas;
      2. import org.springframework.context.support.ClassPathXmlApplicationContext;
      3. public class Provider {
      4. public static void main(String[] args) throws Exception {
      5. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
      6. context.start();
      7. System.in.read();
      8. }
      9. }
    2. Execute the main function of Provider to start the service.

  5. Log on to the Nacos console http://127.0.0.1:8848, in the left-side navigation pane, click Services to view the list of providers. You can see com.alibaba.edas.IHelloService in the list and can query the Service Group and Provider IP of the service.

Create a service consumer

Create a consumer application project locally, add dependencies, and configure it to subscribe to the Dubbo service.

  1. Create a Maven project and add dependencies.

    The procedure is the same as that for creating a provider. For more information, see the procedure for creating a provider.

  2. Develop the Dubbo service.

    All services in Dubbo are provided as interfaces.

    1. In src/main/java, create a package named com.alibaba.edas.

    2. In com.alibaba.edas, create an interface named IHelloService, which contains a SayHello method.

      Note: Generally, an interface is defined in an independent module. The provider and consumer reference the same module through Maven dependencies. In this topic, two identical interfaces are created for the provider and consumer for ease of description. However, we do not recommend this procedure in actual use.

      1. package com.alibaba.edas;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
  3. Configure the Dubbo service.

    1. In src/main/resources, create a file named consumer.xml and open it.

    2. In consumer.xml, add the Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).

      1. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
      3. xmlns="http://www.springframework.org/schema/beans"
      4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      5. http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    3. Add the following configuration to ‘consumer.xml’ to subscribe to the Dubbo service:

      1. <dubbo:application name="demo-consumer"/>
      2. <dubbo:registry address="nacos://127.0.0.1:8848"/>
      3. <dubbo:reference id="helloService" interface="com.alibaba.edas.IHelloService"/>
  4. Start and verify the service.

    1. Create the Consumer class in com.alibaba.edas and load Spring context to the main function of Consumer based on the following code to subscribe to and consume the Dubbo service:

      1. package com.alibaba.edas;
      2. import org.springframework.context.support.ClassPathXmlApplicationContext;
      3. import java.util.concurrent.TimeUnit;
      4. public class Consumer {
      5. public static void main(String[] args) throws Exception {
      6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
      7. context.start();
      8. while (true) {
      9. try {
      10. TimeUnit.SECONDS.sleep(5);
      11. IHelloService demoService = (IHelloService)context.getBean("helloService");
      12. String result = demoService.sayHello("world");
      13. System.out.println(result);
      14. } catch (Exception e) {
      15. e.printStackTrace();
      16. }
      17. }
      18. }
      19. }
    2. Execute the main function of Consumer to start the Dubbo service.

  5. Verify the creation result.

    1. After Dubbo is started, the console outputs hello world continuously, indicating successful service consumption.
    2. Log on to the Nacos console at http://127.0.0.1:8848. Then, in the left-side navigation pane, choose Services. On the Services page, select Callers.

      You can see com.alibaba.edas.IHelloService in the list and query the Service Group and Caller IP of the service.

Deploy the application to EDAS

You can deploy the application that uses local Nacos as the registry directly to EDAS without making any changes. This registry will be automatically replaced with the registry in EDAS.

Based on your actual needs, you can choose the type of cluster to deploy the application to, which is mainly ECS cluster or Container Service Kubernetes cluster, as well as the deployment method, which can be in the console or with tools. For more information, see Application deployment overview.

If you use the console for deployment, complete the following steps in your local application before deploying it:

  1. Add the following packaging plug-in configuration to the pom.xml file.

    • Provider

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. <executions>
      7. <execution>
      8. <goals>
      9. <goal>repackage</goal>
      10. </goals>
      11. <configuration>
      12. <classifier>spring-boot</classifier>
      13. <mainClass>com.alibaba.edas.Provider</mainClass>
      14. </configuration>
      15. </execution>
      16. </executions>
      17. </plugin>
      18. </plugins>
      19. </build>
    • Consumer

      1. <build>
      2. <plugins>
      3. <plugin>
      4. <groupId>org.springframework.boot</groupId>
      5. <artifactId>spring-boot-maven-plugin</artifactId>
      6. <executions>
      7. <execution>
      8. <goals>
      9. <goal>repackage</goal>
      10. </goals>
      11. <configuration>
      12. <classifier>spring-boot</classifier>
      13. <mainClass>com.alibaba.edas.Consumer</mainClass>
      14. </configuration>
      15. </execution>
      16. </executions>
      17. </plugin>
      18. </plugins>
      19. </build>
  2. Execute mvn clean package to package your local program into a JAR file.

After deploying Dubbo microservice applications to EDAS, you can use EDAS for microservice governance. For more information, see Dubbo service governance.

More information