Network Protocol
1. RPC service framework and network protocol
The network protocol is very important in the RPC scenario. In the microservice scenario, the communication between service processes depends on the network that can be connected, and the network protocol that is consistent between the client and the server. Network protocol is an abstract concept. From the perspective of Dubbo-go application development, we might as well divide the protocols we care about into three dimensions for discussion.
1.1 unpacking protocol
The built-in packaging and unpacking protocols of the Dubbo-go service framework are all based on the TCP/IP protocol stack. On this basis, various protocols are encapsulated/introduced, such as Triple (dubbo3), Dubbo, and gRPC.
This type of protocol focuses on the encapsulation and disassembly process of TCP packets to ensure reliable point-to-point communication.
In the dubbo-go ecosystem, supporting multiple networks is often worth this type of protocol.
1.2 Serialization protocol
The serialization protocol is responsible for serializing objects in memory into a binary stream in a specific format. Some mainstream serialization libraries include: the json serialization method with good readability and wide application; the protobuf serialization method with high compression efficiency and better performance; the hessian2 serialization method adapted to the Java language, etc. Dubbo-go has these three built-in serialization methods
The serialization protocol needs to be paid attention to by developers during the business development process. The serialization protocol often requires specific object annotations:
An example of a protobuf sequence object generated by protoc-gen-go:
type HelloRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
}
A hessian2 serialized object that can communicate with java services
type HelloRequest struct {
Name string `hessian:"name"`
}
func (u *HelloRequest) JavaClassName() string {
return "org.apache.dubbo.sample.User"
}
The relationship between the serialization protocol and the unpacking protocol
- A packaging and unpacking protocol can be adapted to support multiple serialization protocols: for example, you can use dubbogo’s triple protocol to pass hessian serialization parameters to communicate with the Dubbo-java service framework; pass pb serialization parameters to communicate with native gRPC services Interoperability; by implementing the interface to customize your desired serialization method, such as json, so as to pass parameters with strong readability.
1.3 Interface protocol
The interface protocol is a protocol developed and maintained by business developers, which is used to describe the information of the service interface. Such as interface name, method, parameter type.
Taking Triple/gRPC as an example, developers can use plug-ins to generate a stub (.pb.go file) from the interface defined in the proto file. The stub file contains all the information of the interface and the interface protocol.
When writing a service, the client and the server introduce the same interface at the same time, which can ensure that the client initiates a call for a specific interface and method, which can be correctly identified and responded by the server.
An interface description file written by proto:
syntax = "proto3";
package api;
option go_package = "./;api";
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (User) {}
// Sends a greeting via stream
rpc SayHelloStream (stream HelloRequest) returns (stream User) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message User {
string name = 1;
string id = 2;
int32 age = 3;
}
The relationship between interface protocol and serialization protocol
- Interface protocol is an abstract concept. An interface protocol can be written in multiple interface description languages, and can be converted into multiple serialized protocol objects.
2. Network protocols supported by Dubbo-go
The network protocols and serialization methods supported by Dubbo-go are as follows:
| protocol | protocol name (for configuration) | serialization method | default serialization method | | ————— | —————– | :————- ———-: | ————– | | Triple 【Recommend】 | tri | pb/hessian2/msgpack/custom | pb | | Dubbo | dubbo | hessian2 | hessian2 | | gRPC | grpc | pb | pb | | jsonRPC | jsonrpc | json | json |
Related reading: [Dubbo-go Service Proxy Model]