The gRPC plugin requires you to provide a .proto
file to describe the rule for converting HTTP into gRPC. HttpRule
is the syntax of the conversion rule.
The HTTP request body can be in JSON format only, and the request method can be RESTful only. The relationships between supported HTTP methods and CRUD operations are as listed below:
Supported HTTP Method | Corresponding CURD |
---|---|
POST | Create |
GET | Read |
PUT | Update/Replace |
PATCH | Update/Modify |
DELETE | Delete |
The following protocols are supported by the backend:
The following types are supported by the backend:
HttpRule
defines the mapping relationship between HTTP and gRPC in two dimensions:
In the RPC method definition, you can add option (google.api.http)
as the HttpRule
mapping syntax.
Note:
- In
HttpRule
, the HTTP method needs to be in lowercase, and an RPC method corresponds to an HTTP request.- There is only one method, unless you use
addtional_bindings
. For the usage methods and use cases ofaddtional_bindings
, see below.
Below is a sample .proto
file with the HttpRule
rule:
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
import "google/api/annotations.proto";
package helloworld;
// Chinese remarks are supported
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
get: "/grpc_upstream/{name}"
additional_bindings {
get: "/grpc_upstream_tls/{name}"
}
};
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Note:You must import the relevant
HttpRule
syntax: import "google/api/annotations.proto". In addition to theHttpRule
, all other syntaxes are the same as those of the gRPC service. Therefore, you only need to add theHttpRule
to the original.proto
file of the gRPC service.
.proto
file syntax locallyIf the uploaded .proto
file has problems, you can use the protoc tool to check the file as follows:
googleapis.tar.gz
to any directory such as /tmp
.GOOGLEAPIS_DIR:GOOGLEAPIS_DIR=/tmp/googleapis
..proto
file. The file is valid if there are no errors and warnings.protoc --include_imports --include_source_info --proto_path=${GOOGLEAPIS_DIR} --proto_path=. --descriptor_set_out=api_descriptor.pb ex ample.proto
HttpRule
is a specific value (not *):HttpRule
is *:HttpRule
is undefined:service Messaging {
rpc GetMessage(GetMessageRequest) returns (Message) {
option (google.api.http) = {
get:"/v1/messages/{message_id}"
};
}
}
message GetMessageRequest {
message SubMessage {
string subfield = 1;
}
string message_id = 1; // Mapped to URL path.
int64 revision = 2; // Mapped to URL query parameter `revision`.
SubMessage sub = 3; // Mapped to URL query parameter `sub.subfield`.
}
message_id
in GetMessageRequest
is obtained from the URL path, and other parameters, such as revision
and subfield
in sub
, are obtained from the query parameters of the URL.{message_id}
in "/v1/messages/{message_id}" is the path template. The parameter specifying message_id
is obtained from the URL path./v1/messages/123456?revision=2&sub.subfield=foo
, the corresponding gRPC request is GetMessage(message_id: "123456" revision: 2 sub: SubMessage(subfield:"foo"))
.service Messaging {
rpc UpdateMessage(Message) returns (Message) {
option (google.api.http) = {
patch: "/v1/messages/{message_id}"
body: "*"
};
}
}
message Message {
string message_id = 1;
string text = 2;
}
message_id
is obtained from the URL path of the request, while other parameters such as text
are obtained from the body. Note that if the body is \*, parameters won't be obtained from the query parameters of the request.PATCH /v1/messages/123456 { "text": "Hi!" }
, the corresponding gRPC request is UpdateMessage(message_id: "123456" text: "Hi!")
.service Messaging {
rpc UpdateMessage(UpdateMessageRequest) returns (Message) {
option (google.api.http) = {
patch: "/v1/messages/{message_id}"
body: "message"
};
}
}
message UpdateMessageRequest {
string message_id = 1; // mapped to the URL
Message message = 2; // mapped to the body
}
message Message {
string text = 1; // The resource content.
}
PATCH /v1/messages/123456 { "text": "Hi!" }
, the corresponding gRPC request is UpdateMessage(message_id: "123456" text: "Hi!")
.additional_bindings
:additional_bindings
is used for API compatibility or exposure of two HTTP requests with the same RPC method.service Messaging {
rpc GetMessage(GetMessageRequest) returns (Message) {
option (google.api.http) = {
get: "/v1/messages/{message_id}"
additional_bindings {
get: "/v1/users/{user_id}/messages/{message_id}"
}
};
}
}
message GetMessageRequest {
string message_id = 1;
string user_id = 2;
}
GET /v1/messages/123456
, the corresponding gRPC request is GetMessage(message_id: "123456")
.GET /v1/users/me/messages/123456
, the corresponding gRPC request is GetMessage(message_id: "123456")
.Note:For the detailed
HttpRule
, see http.proto.
Only one .proto
file can be uploaded for a gRPC backend plugin. Therefore, you need to place all RPC methods that need to be exposed into the same .proto
file and add HttpRule
.
proto file format error
parameter format is incorrect. Modify it and perform the operation again" is reported when I save the gRPC plugin. What should I do?The submitted .proto
file is in an incorrect format. This may be because HttpRule
isn't imported. In this case, you need to add import "google/api/annotations.proto"
to the .proto
file.
Solution: Bind the gRPC backend plugin to this API.
Example of successful access after modification:
Solution: The path defined in option
in HttpRule
in the .proto
file is different from the one you accessed. Check the .proto
file and modify the paths to the same value.
Solution example:
Example of successful access after modification:
Was this page helpful?