How to Use Protobuf and JSON Serialization in the Context of the Triple Protocol

This article introduces protobuf serialization, and how to use protobuf and json serialization in the context of the triple protocol.

1 Introduction

Protobuf (Protocol Buffers) is a lightweight and efficient data interchange format developed by Google, used for the serialization, deserialization, and transmission of structured data. Compared to text formats like XML and JSON, Protobuf has smaller data sizes, faster parsing speeds, and better scalability.

2 Usage

When using Protobuf (IDL) to develop triple communication services, the Dubbo server will automatically enable support for protobuf and protobuf-json serialization modes.

2.1 Add Dependencies

To use the triple + protobuf mode, the following dependencies must be added:

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.google.protobuf</groupId>
  4. <artifactId>protobuf-java</artifactId>
  5. <version>3.19.6</version>
  6. </dependency>
  7. <!-- Provides support for protobuf-json format requests -->
  8. <dependency>
  9. <groupId>com.google.protobuf</groupId>
  10. <artifactId>protobuf-java-util</artifactId>
  11. <version>3.19.6</version>
  12. </dependency>
  13. </dependencies>

2.2 Configuring Enablement

As long as the communication is based on Protobuf (IDL), protobuf serialization will be used once the protobuf file is defined and the triple protocol is enabled.

When accessing the triple service with cURL, the protobuf-json serialization mode will be enabled.

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

Example of protobuf service definition:

  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. }

Protocol configuration:

  1. # application.yml (Spring Boot)
  2. dubbo:
  3. protocol:
  4. name: tri

or

  1. # dubbo.properties
  2. dubbo.protocol.name=tri

or

  1. <dubbo:protocol name="tri" />

Feedback

Was this page helpful?

Yes No

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