TCP-based RPC Communication Protocol - dubbo

Demonstrates how to develop services based on dubbo protocol communication.

This example demonstrates how to develop services based on dubbo protocol communication, and you can view the full code of this example:

Running the Example

You can follow these steps to try running the example source code corresponding to this document.

First, download the example source code using the following command:

git clone --depth=1 https://github.com/apache/dubbo-samples.git

Change into the example source code directory:

cd dubbo-samples/2-advanced/dubbo-samples-dubbo

Package the example using Maven:

mvn clean install -DskipTests

Start the Provider

Run the following command to start the provider:

java -jar ./dubbo-samples-dubbo-provider/target/dubbo-samples-dubbo-provider-1.0-SNAPSHOT.jar

Start the Consumer

Run the following command:

java -jar ./dubbo-samples-dubbo-consumer/target/dubbo-samples-dubbo-consumer-1.0-SNAPSHOT.jar

Source Code Explanation

Define the Service

First is the service definition. When using the dubbo protocol, we need to define the Dubbo service through a Java Interface.

public interface DemoService {
    String sayHello(String name);
}

Service Provider

Next, on the provider side, we need to provide the concrete implementation of the service.

@DubboService
public class DemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

Configure to use the dubbo protocol:

dubbo:
  protocol:
    name: dubbo
    port: 20880

Service Consumer

Configure the service reference as follows:

@Component
public class Task implements CommandLineRunner {
    @DubboReference(url = "dubbo://127.0.0.1:20880/org.apache.dubbo.protocol.dubbo.demo.DemoService")
    private DemoService demoService;
}

Next, you can initiate the RPC call to the remote service:

demoService.sayHello("world");

More Protocol Configuration

Serialization

The calls between the consumer and provider use the dubbo protocol, the default data encoding format (i.e., serialization) for method parameters is hessian2. You can also set to use any other serialization protocol; serialization does not affect the normal operation of the dubbo protocol (it only has some impact on encoding performance).

dubbo:
  protocol:
    name: dubbo
    port: 20880
    serialization: fastjson2

Shared Connections

For implementing the dubbo protocol, the default connection between consumer machine A and provider machine B is the same link, meaning that regardless of how many service calls exist between A and B, the same TCP connection is used by default. Of course, the Dubbo framework provides methods that allow you to adjust the number of TCP connections between A and B.

Additionally, the dubbo protocol supports configurations for payload limits, serialization, the number of connections, connection timeout, heartbeat, etc. For details, please refer to the Reference Manual - dubbo Protocol.