All Products
Search
Document Center

Deploy Dubbo applications to EDAS

Last Updated: Mar 25, 2019

If you have limited experience in Java and Maven and are not familiar with Dubbo, you can develop services on Dubbo from scratch and use the EDAS service registry for service registration and discovery.

Note: Currently, EDAS supports Dubbo in versions 2.5.3 to 2.7.0. If using 2.7.0 or later versions of Dubbo, you need to add the dependency of edas-dubbo-extension 2.0.2 for you application..

If you are familiar with Dubbo, you can read related sections as needed.

This document uses a provider plus a consumer as an example to describe how to develop service providers and consumers locally, deploy them on EDAS, register services with the EDAS service registry, and allow consumers to call providers.

Local development describes key information concerning development. For details about Dubbo, you can download edas-dubbo-demo.

Why is the EDAS service registry used?

The EDAS service registry implements SPI-based registry extension provided by Dubbo. It fully supports service registration, routing rules, and rule configuration on Dubbo.

The EDAS service registry can completely replace ZooKeeper and Redis as the registry for your service on Dubbo.In contrast to ZooKeeper and Redis, the EDAS service registry has the following advantages:

  • The EDAS service registry is a shared component, saving you the costs of operating, maintaining, and deploying components such as ZooKeeper.
  • The EDAS service registry implements authentication and encryption in the communication process, improving the security of your service registration links.
  • The EDAS service registry is fully integrated with other EDAS components to provide you with a complete set of microservice solutions.

Local development

Preparation

  • Download, start, and configure the lightweight configuration center.

    To facilitate local development, EDAS provides a lightweight configuration center that has the basic features of the EDAS service registry.Applications developed based on the lightweight configuration center can be deployed on EDAS without any modifications to code or configurations.

    For more information about how to download, start, and configure the lightweight configuration center, see Configure a lightweight configuration center.The latest version is recommended.

  • Download Maven and set the environment variables (skip this step if Maven has been installed).

Create a service provider

  1. Create a Maven project and introduce dependencies.

    1. Use an IDE instance (for example, IDEA and Eclipse) to create a Maven project.

    2. Add the dubbo and edas-dubbo-extension dependencies to the Maven file pom.xml, in 2.6.2 and 1.0.1 versions respectively.

      1. <dependencies>
      2. <dependency>
      3. <groupId>com.alibaba</groupId>
      4. <artifactId>dubbo</artifactId>
      5. <version>2.6.2</version>
      6. </dependency>
      7. <dependency>
      8. <groupId>com.alibaba.edas</groupId>
      9. <artifactId>edas-dubbo-extension</artifactId>
      10. <version>1.0.6</version>
      11. </dependency>
      12. </dependencies>
  2. Develop a Dubbo service provider.

    Services in Dubbo are provided over APIs.

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

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

      1. package com.alibaba.edas;
      2. public interface IHelloService {
      3. String sayHello(String str);
      4. }
    3. In com.alibaba.edas, create a IHelloServiceImpl class for the API.

      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 Dubbo services.

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

    2. Add Spring-related XML NameSpace (xmlns) and XML Schema Instance (xmlns:xsi), and Dubbo-related NameSpace (xmlns:dubbo) and Scheme Instance (xsi:schemaLocation) to provider.xml.

      1. <beans xmlns="http://www.springframework.org/schema/beans"
      2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      4. xsi:schemaLocation="http://www.springframework.org/schema/beans
      5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      6. http://code.alibabatech.com/schema/dubbo
      7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    3. In provider.xml, configure APIs and implementation classes as Dubbo services.

      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 the lightweight configuration center of EDAS as the registry.

      where, 127.0.0.1 is the address of the lightweight configuration center, which must be changed to the corresponding IP address if your lightweight configuration center is deployed on another instance.The lightweight configuration center does not support port modification, so port 8080 must be used.

      1. <dubbo:registry id="edas" address="edas://127.0.0.1:8080"/>
  4. Start the service.

    1. In com.alibaba.edas, create a class provider, and load Spring Context in the main function of the provider to allow the calling of the configured Dubbo services.

      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 the provider to start services.

  5. Log on to the console of the lightweight configuration center http://127.0.0.1:8080. In the left-side navigation pane, click Services to view the list of service providers.com.alibaba.edas.IHelloService exists in the list of service providers and you can query service groups and provider IP addresses.

Create a service consumer

  1. Create a Maven project and introduce dependencies.

    The procedure for creating a service consumer is the same as that for creating a service provider.For details, see the procedure for creating a service provider.

  2. Develop Dubbo services.

    Services in Dubbo are provided over APIs.

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

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

      Note: APIs are usually defined in a separate module. Both service providers and service consumers reference this module through the Maven dependency.In this document, for simplicity, two identical APIs are created for service providers and consumers respectively. We do not recommend this configuration in practice.

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

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

    2. Add Spring-related XML NameSpace (xmlns) and XML Schema Instance (xmlns:xsi), and Dubbo-related NameSpace (xmlns:dubbo) and Scheme Instance (xsi:schemaLocation) to consumer.xml.

      1. <beans xmlns="http://www.springframework.org/schema/beans"
      2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      3. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
      4. xsi:schemaLocation="http://www.springframework.org/schema/beans
      5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      6. http://code.alibabatech.com/schema/dubbo
      7. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    3. Add the following configuration to consumer.xml and subscribe to Dubbo services.

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

    1. In com.alibaba.edas, create a class consumer, and load Spring Context in the main function of the consumer to subscribe to and consume Dubbo services.

      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 the consumer to start services.

  5. Verify creation results.

    1. After the service is started, hello world is continuously output in the console, indicating that the service is consumed successfully.
    2. Log on to the lightweight configuration center console http://127.0.0.1:8080. In the left-side navigation pane, click Services. On the Services page, select Callers. com.alibaba.edas.IHelloService, service group, and caller IP address are displayed.

Deploy applications to EDAS

edas-dubbo-extension is designed to support the migration of local applications to EDAS. You can directly deploy applications to EDAS without making any modifications to code or configurations.

  1. Add the following configuration to the pom.xml files of both the provider and the consumer. Then, run mvn clean package to package native programs into executable JAR packages.

    • 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. Deploy applications based on the appropriate documents for the corresponding cluster types.