BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Apache Dubbo, a Java-Based RPC Framework, Graduates to Top-Level Project

Apache Dubbo, a Java-Based RPC Framework, Graduates to Top-Level Project

Leia em Português

This item in japanese

The Apache Software Foundation recently announced Apache Dubbo as a top-level project. Apache Dubbo is an open source, remote procedure call framework based on Java. It was originally developed at Alibaba, open-sourced in 2011, and entered the Apache Incubator in February 2018. Dubbo brings key functionalities such as interface-based remote call, fault tolerance and load balancing, and automatic service registration and discovery.

The Dubbo architecture is shown below:

(Image taken from https://dubbo.apache.org/)

  1. Container is responsible for launching, loading, and running the service Provider
  2. Provider registers its services to Register during its initialization
  3. Consumer subscribes the services it needs from the Register when it starts
  4. Register returns the Providers list to Consumer, and when a change occurs, the Register push the changed data to Consumer
  5. Based on a soft load balancing algorithm, the Consumer will select one of the Providers and executes the invocation, automatically choose another Provider when a fail occurs.
  6. Both Consumer and Provider will count the number service invocations and time-consuming in memory, and send the statistics to Monitor every minute.

Apache Dubbo features include:

  • A transparent interface based RPC
  • Intelligent load balancing, which supports multiple load balancing strategies out of the box
  • Automatic service registration and discovery
  • High extensibility, micro-kernel and plugin design which ensures that it can easily be extended by third party implementation across core features like protocol, transport, and serialization
  • Runtime traffic routing, which can be configured at runtime so that traffic can be routed according to different rules, making it easy to support features such as blue-green deployment, data center aware routing, etc
  • Visualized service governance, which provides rich tools for service governance and maintenance such as querying service metadata, health status, and statistics

To get started using Dubbo, developers first add the Maven dependency:

<dependencies>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.7.2<</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>2.7.2<</version>
        <type>pom</type>
    </dependency>
</dependencies>

Next, a service interface is defined and implemented in the provider:

package org.apache.dubbo.samples.api;

//service interface
public interface GreetingService {
    String sayHello(String name);
}
 
//provider, implementation of service interface
public class GreetingServiceImpl implements GreetingService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

The provider would be configured as follows:

package org.apache.dubbo.demo.provider;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.samples.api.GreetingService;
import java.io.IOException;
 
public class Application {

    public static void main(String[] args) throws IOException {
        ServiceConfig serviceConfig = new ServiceConfig();
        serviceConfig.setApplication(new ApplicationConfig("first-dubbo-provider"));
        serviceConfig.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
        serviceConfig.setInterface(GreetingService.class);
        serviceConfig.setRef(new GreetingServiceImpl());
        serviceConfig.export();
        System.in.read();
    }
}

And then built and run:

# mvn clean package
# mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.demo.provider.Application exec:java

The following code snippets show how to create a consumer:

package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.samples.api.GreetingService;

public class Application {
    public static void main(String[] args) {
        ReferenceConfig referenceConfig = new ReferenceConfig();
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        referenceConfig.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234"));
        referenceConfig.setInterface(GreetingService.class);
        GreetingService greetingService = referenceConfig.get();
        System.out.println(greetingService.sayHello("world"));
    }
}

Beginning a new project using Apache Dubbo is relatively simple; developers can follow the steps above, or by use Dubbo's Spring initializr, which is basically a fork of Spring initializr. In addition to Java, Dubbo supports Node.js, Python, and PHP.

Apache Dubbo is in use at companies such as Alibaba Group, China Life, China Telecom, Dangdang, Didi Chuxing, Haier, Industrial and Commercial Bank of China, NetEase, Qunar, and Youzan.

More details about Dubbo can be found at https://dubbo.apache.org. Developers who want to contribute should access the contributor guide. There is also extensive documentation available.

Rate this Article

Adoption
Style

BT