brpc protocol specification
A standard brpc protocol message has the following format:
0 1 2 3 4 6 8 10 12 14 16
+----+----+------+---+---+---+---+-----+----+----+----+----+-----+-----+------+-----+
| magic | flag | requestID |codec| timeout/status | headerLen | contentLen |
+----+----+------+---+---+---+---+-----+----+----+----+----+-----+-----+------+-----+
| header + content bytes ... ... |
| |
+-----------------------------------------------------------------------------------+The fields are described as follows:
Field name | Size (bytes) | Description |
magic | 2 | Magic number. A static field with the value 0xbcbc. |
flag | 1 | Message flag. The possible values are:
|
requestID | 4 | Request or response ID. |
codec | 1 | Serialization number. A static field with the value 0. |
timeout | 4 | Timeout period in milliseconds (ms). This field is valid when flag is set to 1 or 3 and indicates the request timeout period. Specify a value for this field in a request. |
status | 4 | Response code. This field is valid when flag is set to 2 or 4 and indicates the response status code. Specify a value for this field in a response. This field shares the same position as the timeout field. |
headerLen | 2 | Indicates the length of the key-value pairs in the message. |
contentLen | 2 | Indicates the length of the message body. |
header | - | The encoded format is a key-value pair string, such as The header contains service invocation information. For more information, see the table below. |
content bytes | - | The body of the business message. The content is not restricted. For example, you can pass a string. |
The header contains the following service invocation information:
No. | Contains key | Meaning | Example |
1 | interface | Service invocation interface | com.alipay.core.UserService |
2 | method | Service invocation method | userInfo |
brpc protocol invocation guide
The source code for the brpc server and client is available at:
https://github.com/mosn/extensions/tree/master/java-quickstart/java-plugin-quickstartbrpc-server is the server-side program. The server starts and listens on port 7766.
brpc-client is the client program. The client starts and listens on port 8080.
The brpc client supports REST API calls to trigger RPC invocations. Use the following command:
curl localhost:8080/invokeThe optional HTTP parameters are as follows:
service: The service interface for the call. The default value is
com.alipay.core.UserService.parameter: The parameters for the call. The format is
key=value,...=.... Use commas (,) to separate multiple key-value pairs.content: The RPC message body. This content is returned in the RPC response. The default value is
hello world.ipPort: The
ip:portfor the call. The default value is127.0.0.1:2045, which is the brpc MOSN client port.timeout: The call timeout period in milliseconds. The default value is 3000 ms. To debug a process that hangs for a long time, specify a longer timeout period.
Example:
curl localhost:8080/invoke\?timeout=6000\&content=yiji\¶meter=user=yiji,age=30In the curl command line, you must escape the ? and & characters with a backslash (\). This is not required for calls from Postman.
The code automatically generates a script to manually publish and subscribe to services after MOSN starts. You must run the script manually the first time you start MOSN:
cd ~/go/src/quick-start-practice/configs/codecs/brpc/
bash ./auto_pub_sub.shExercise
Use what you have learned to develop a brpc application with MOSN mecha.
Hint: When you implement the protocol, focus on decoding the fields in the header.
No. | Protocol element | Corresponding field | Example |
1 | Service identity | interface | com.alipay.core.UserService |
2 | Service method | method | userInfo |
Watch the following video to learn the steps and important considerations for brpc protocol development: