Developing Triple Communication Services Using Protobuf (IDL)

The Triple protocol supports defining services using Protocol Buffers (Protobuf), making it more user-friendly for those who choose Protobuf for various scenarios such as multi-language, gRPC, and security.

Usage

POM dependency

  1. <plugin>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-maven-plugin</artifactId>
  4. <version>${dubbo.version}</version> <!-- Version 3.3.0 and above -->
  5. <configuration>
  6. <outputDir>build/generated/source/proto/main/java</outputDir> <!-- Refer to the following text for configurable parameters -->
  7. </configuration>
  8. </plugin>

Configurable parameters

ParameterRequiredDefault valueDescriptionNotes
outputDirfalse${project.build.directory}/generated-sources/protobuf/javaGenerated java file storage directory
protoSourceDirfalse${basedir}/src/main/protoproto file directory
protocArtifactfalsecom.google.protobuf:protoc:3.25.0:exe:Operating System Name:Operating System Architectureproto compiler component
protocVersionfalse3.25.0version of protobuf-java
dubboGenerateTypefalsetriCode generation typeCan be filled with tri or tri_reactor

Service Definition

Define the Greeter service using Protocol Buffers.

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. package org.apache.dubbo.samples.tri.unary;
  4. message GreeterRequest {
  5. string name = 1;
  6. }
  7. message GreeterReply {
  8. string message = 1;
  9. }
  10. service Greeter{
  11. rpc greet(GreeterRequest) returns (GreeterReply);
  12. }

Please note the package definition package org.apache.dubbo.samples.tri.unary;. In this example, the path defined in the package will also serve as the Java package name and service prefix. This means the complete definition of the rpc service is: org.apache.dubbo.samples.tri.unary.Greeter, which matches the path of the generated code exactly.

However, keeping consistency is not mandatory; you can define the Java package name separately from the service prefix, which is useful in some cross-language calling scenarios. For instance, in the following IDL definition:

  • The complete service name is greet.Greeter, which will be used during rpc calls and service discovery.
  • The Java package name is defined as org.apache.dubbo.samples.tri.unary, where the generated Java code will be placed.
  1. package greet;
  2. option java_package = "org.apache.dubbo.samples.tri.unary;"
  3. option go_package = "github.com/apache/dubbo-go-samples/helloworld/proto;greet";

Service Implementation

Running mvn clean compile will generate the Dubbo stub code. Next, extend the generated base class DubboGreeterTriple.GreeterImplBase and add specific business logic implementation:

  1. public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
  2. @Override
  3. public GreeterReply greet(GreeterRequest request) {
  4. LOGGER.info("Server {} received greet request {}", serverName, request);
  5. return GreeterReply.newBuilder()
  6. .setMessage("hello," + request.getName())
  7. .build();
  8. }
  9. }

Register the service to the server, where the protocol is set to tri to indicate that the Triple protocol is to be enabled.

  1. public class TriUnaryServer {
  2. public static void main(String[] args) throws IOException {
  3. ServiceConfig<Greeter> service = new ServiceConfig<>();
  4. service.setInterface(Greeter.class);
  5. service.setRef(new GreeterImpl());
  6. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
  7. bootstrap.protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50052))
  8. .service(service)
  9. .start().await();
  10. }
  11. }

Write Client Logic

  1. public class TriUnaryClient {
  2. public static void main(String[] args) throws IOException {
  3. DubboBootstrap bootstrap = DubboBootstrap.getInstance();
  4. ReferenceConfig<Greeter> ref = new ReferenceConfig<>();
  5. ref.setInterface(Greeter.class);
  6. ref.setUrl("tri://localhost:50052");
  7. bootstrap.reference(ref).start();
  8. Greeter greeter = ref.get();
  9. final GreeterReply reply = greeter.greet(GreeterRequest.newBuilder().setName("name").build());
  10. }
  11. }

Frequently Asked Questions

Protobuf Class Not Found

Since the underlying Triple protocol depends on the protobuf protocol for transmission, even if the defined service interface does not use protobuf, the protobuf dependency must still be included in the environment.

  1. <dependency>
  2. <groupId>com.google.protobuf</groupId>
  3. <artifactId>protobuf-java</artifactId>
  4. <version>3.19.4</version>
  5. </dependency>

Additionally, to support direct access to application/json format requests, the following dependency needs to be added.

  1. <dependency>
  2. <groupId>com.google.protobuf</groupId>
  3. <artifactId>protobuf-java-util</artifactId>
  4. <version>3.19.4</version>
  5. </dependency>

Generated Code Cannot Compile

When using Protobuf, ensure that the core Dubbo library version matches the protoc plugin version and run mvn clean compile to regenerate the code.

Before version 3.3.0

Versions before 3.3.0 use protobuf-maven-plugin to configure the protoc plugin, and the dubbo-compiler must match the core dubbo version used:

  1. <plugin>
  2. <groupId>org.xolstice.maven.plugins</groupId>
  3. <artifactId>protobuf-maven-plugin</artifactId>
  4. <version>0.6.1</version>
  5. <configuration>
  6. <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
  7. <outputDirectory>build/generated/source/proto/main/java</outputDirectory>
  8. <protocPlugins>
  9. <protocPlugin>
  10. <id>dubbo</id>
  11. <groupId>org.apache.dubbo</groupId>
  12. <artifactId>dubbo-compiler</artifactId>
  13. <version>${dubbo.version}</version>
  14. <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
  15. </protocPlugin>
  16. </protocPlugins>
  17. </configuration>
  18. <executions>
  19. <execution>
  20. <goals>
  21. <goal>compile</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>

Run the Example

This example demonstrates how to define a service using Protocol Buffers and publish it as an externally callable Triple protocol service. If you have multi-language interoperability, gRPC interaction, or are familiar with and prefer Protobuf development, you can use this approach; otherwise, consider the previous Java interface-based triple development model.

You can view the full code for this example.

Note

The example used in this article is based on native API coding; here is a Spring Boot version of the example for reference, which also follows the protobuf+triple model but includes service discovery configuration.

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

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

Change to the example source code directory:

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

Compile the project to generate code from the IDL, which calls the protoc plugin provided by Dubbo to generate the corresponding service definition code:

  1. mvn clean compile

The generated code is as follows:

  1. ├── build
  2. └── generated
  3. └── source
  4. └── proto
  5. └── main
  6. └── java
  7. └── org
  8. └── apache
  9. └── dubbo
  10. └── samples
  11. └── tri
  12. └── unary
  13. ├── DubboGreeterTriple.java
  14. ├── Greeter.java
  15. ├── GreeterOuterClass.java
  16. ├── GreeterReply.java
  17. ├── GreeterReplyOrBuilder.java
  18. ├── GreeterRequest.java
  19. └── GreeterRequestOrBuilder.java

Start Server

Run the following command to start the server.

  1. mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.unary.TriUnaryServer"

Access the Service

There are two ways to access the Triple service:

  • Using standard HTTP tools
  • Using Dubbo client SDK

cURL Access

  1. curl \
  2. --header "Content-Type: application/json" \
  3. --data '{"name":"Dubbo"}' \
  4. http://localhost:50052/org.apache.dubbo.samples.tri.unary.Greeter/greet/

Dubbo Client Access

Run the following command to start the Dubbo client and complete the service call.

  1. mvn compile exec:java -Dexec.mainClass="org.apache.dubbo.samples.tri.unary.TriUnaryClient"

Feedback

Was this page helpful?

Yes No

Last modified October 11, 2024: Update idl usage document (#3041) (e2c3c45727c)