Developing triple communication services using Java interfaces

The Triple protocol is fully compatible with gRPC but offers better usability without binding to Protobuf, allowing you to define services directly using Java interfaces.

Unlike the official Google gRPC implementation, the Dubbo implementation of the triple protocol offers better usability (not bound to Protobuf), allowing you to define services directly using Java interfaces. For users looking for smooth upgrades, having no multi-language business, or unfamiliar with Protobuf, the Java interface method is the simplest way to use triple.

Below is a basic example of developing a Dubbo service using the Java interface, which uses triple protocol communication. You can view the full code for this example.

Note

The example used in this article is coded based on the native API. There is also a Spring Boot version of the example for reference, which also follows the Java interface + triple model, with additional service discovery configuration.

Run the Example

First, you can download the example source code with the following command

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

Enter the example source code directory:

  1. cd dubbo-samples/1-basic/dubbo-samples-api

Start the Server

Run the following command to start the server

  1. mvn -Dexec.mainClass=org.apache.dubbo.samples.provider.Application exec:java

Start the Client

There are two ways to call the services published by the server:

  • Use standard HTTP tools, such as cURL
  • Develop a client using the Dubbo SDK

cURL

  1. curl \
  2. --header "Content-Type: application/json" \
  3. --data '["Dubbo"]' \
  4. http://localhost:50052/org.apache.dubbo.samples.api.GreetingsService/sayHi/

SDK Client

  1. mvn -Dexec.mainClass=org.apache.dubbo.samples.client.Application exec:java

Source Code Explanation

If you are a long-time Dubbo user, you will find the following content is basically the same as the previous Dubbo2 development model, with the protocol name changed from dubbo to tri.

Define Service

First is the service definition, using Java interfaces to define the Dubbo service.

  1. public interface GreetingsService {
  2. String sayHi(String name);
  3. }

Service Provider

Next, on the provider side, you need to provide the specific implementation of the service:

  1. public class GreetingsServiceImpl implements GreetingsService {
  2. @Override
  3. public String sayHi(String name) {
  4. return "hi, " + name;
  5. }
  6. }

Finally, publish the service:

  1. public static void main(String[] args) {
  2. DubboBootstrap.getInstance()
  3. .protocol(ProtocolBuilder.newBuilder().name("tri").port(50052).build())
  4. .service(ServiceBuilder.newBuilder().interfaceClass(GreetingsService.class).ref(new GreetingsServiceImpl()).build())
  5. .start()
  6. .await();
  7. }

Service Consumer

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

  1. public static void main(String[] args) throws IOException {
  2. ReferenceConfig<GreetingsService> reference =
  3. ReferenceBuilder.<GreetingsService>newBuilder()
  4. .interfaceClass(GreetingsService.class)
  5. .url("tri://localhost:50052")
  6. .build();
  7. DubboBootstrap.getInstance().reference(reference).start();
  8. GreetingsService service = reference.get();
  9. String message = service.sayHi("dubbo");
  10. }

Notes

Serialization Encoding

How does Dubbo support both ordinary Java objects and Protobuf objects? In the Dubbo implementation, there is an object type check that first determines whether the parameter type is a protobuf object. If not, a protobuf object will wrap the request and response to unify the transmission of ordinary Java objects as protobuf objects. The wrapper object declares serialization types internally to support serialization extensions.

The IDL for the wrapper is as follows:

  1. syntax = "proto3";
  2. package org.apache.dubbo.triple;
  3. message TripleRequestWrapper {
  4. // hessian4
  5. // json
  6. string serializeType = 1;
  7. repeated bytes args = 2;
  8. repeated string argTypes = 3;
  9. }
  10. message TripleResponseWrapper {
  11. string serializeType = 1;
  12. bytes data = 2;
  13. string type = 3;
  14. }

For requests, use TripleRequestWrapper for wrapping, and for responses, use TripleResponseWrapper for wrapping.

For request parameters, note that args is marked as repeated to support multiple parameters for Java methods. Of course, there can only be one serialization. The serialization implementation follows the spi of Dubbo2.

Performance

Due to an additional layer of serialization encoding for the data transmitted over the link (such as hessian2), and the method calls on the server side being based on reflection, the Java interface method may experience some performance degradation compared to the protobuf + triple encoding model.

While the Protobuf model does have some performance advantages, usability and cost of use will also increase sharply. We recommend considering the business scenario first; if there is no multi-language business or if you are a Dubbo2 veteran, then sticking with the Java interface model is a good, low-cost option.

gRPC Compatibility

Since gRPC only supports the protobuf model, the interface + triple model introduced in this article cannot interoperate with the official native gRPC protocol from Google.

Frontend Traffic Access

For HTTP traffic from the frontend (such as browsers or web applications), to access triple through the gateway, it must use the built-in application/json mode to initiate the call. For details, please refer to 【Usage Tutorial - HTTP Gateway Access】

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)