grpc学习-http方式请求grpc接口
如果想使用http的方法请求grpc接口,则需要在.proto 文件中添加配置,然后接口会从 HTTP/JSON 请求映射转换为 gRPC。
映射 List 方法
/ Returns a list of all shelves in the bookstore.rpc ListShelves(google.protobuf.Empty) returns (ListShelvesResponse) {// Define HTTP mapping.// Client example (Assuming your service is hosted at the given 'DOMAIN_NAME'):// curl http://DOMAIN_NAME/v1/shelvesoption (google.api.http) = { get: "/v1/shelves" };}...message ListShelvesResponse {// Shelves in the bookstore.repeated Shelf shelves = 1;}
option (google.api.http) 指定此方法是一个 gRPC HTTP 映射注释。
get 指定此方法映射到 HTTP GET 请求。
"/v1/shelves" 是 GET 请求在调用该方法时使用的网址路径模板(附加到服务的网域)。网址路径也称为资源路径,因为它通常用于指定您要使用的“对象”或资源。在本例中是指我们的 Bookstore 的所有书架资源。
映射 Get 方法
// Returns a specific bookstore shelf.rpc GetShelf(GetShelfRequest) returns (Shelf) {// Client example - returns the first shelf:// curl http://DOMAIN_NAME/v1/shelves/1option (google.api.http) = { get: "/v1/shelves/{shelf}" };}...// Request message for GetShelf method.message GetShelfRequest {// The ID of the shelf resource to retrieve.int64 shelf = 1;}...// A shelf resource.message Shelf {// A unique shelf id.int64 id = 1;// A theme of the shelf (fiction, poetry, etc).string theme = 2;}
使用网址 http://mydomain/v1/shelves/1 发出 HTTP GET 请求会调用 gRPC 服务器的 GetShelf() 方法,使用包含所请求的书架 ID shelf(在此示例中为 1)的 GetShelfRequest。
此方法只需要客户端 shelf 提供一个请求字段值,该值是在采用花括号“捕获型”表示法的网址路径模板中指定的。如有必要,还可以捕获网址的多个部分,以识别请求的资源。例如,GetBook 方法需要客户端在网址中同时指定书架 ID 和图书 ID:
// Returns a specific book.rpc GetBook(GetBookRequest) returns (Book) {// Client example - get the first book from the second shelf:// curl http://DOMAIN_NAME/v1/shelves/2/books/1option (google.api.http) = { get: "/v1/shelves/{shelf}/books/{book}" };}...// Request message for GetBook method.message GetBookRequest {// The ID of the shelf from which to retrieve a book.int64 shelf = 1;// The ID of the book to retrieve.int64 book = 2;}
除了将文字和捕获型括号用于字段值之外,网址路径模板还可使用通配符指示应捕获该网址部分中的全部内容。上述示例中使用的 {shelf} 表示法实际上是 {shelf=*} 的快捷方式。
映射 Create 方法
// Creates a new shelf in the bookstore.rpc CreateShelf(CreateShelfRequest) returns (Shelf) {// Client example:// curl -d '{"theme":"Music"}' http://DOMAIN_NAME/v1/shelvesoption (google.api.http) = {post: "/v1/shelves"body: "shelf"};}...// Request message for CreateShelf method.message CreateShelfRequest {// The shelf resource to create.Shelf shelf = 1;}...// A shelf resource.message Shelf {// A unique shelf id.int64 id = 1;// A theme of the shelf (fiction, poetry, etc).string theme = 2;}
option (google.api.http) 指定此方法是一个 gRPC HTTP 映射注释。
post 指定此方法映射到 HTTP POST 请求。
"/v1/shelves" 是请求的网址路径。
body: "shelf" 在 HTTP 请求正文中用来以 JSON 格式指定您要添加的资源。
客户端按如下所示的方式调用该方法:
curl -d '{"theme":"Music"}' http://DOMAIN_NAME/v1/shelves
在正文中使用通配符
在正文映射中,可以使用特殊名称 * 来指示不受路径模板约束的每个字段应该映射到请求正文。此方式支持以下备用的 CreateShelf 方法定义。
// Creates a new shelf in the bookstore.rpc CreateShelf(CreateShelfRequest) returns (Shelf) {// Client example:// curl -d '{"shelf_theme":"Music", "shelf_size": 20}' http://DOMAIN_NAME/v1/shelves/123option (google.api.http) = {post: "/v1/shelves/{shelf_id}"body: "*"};}...// Request message for CreateShelf method.message CreateShelfRequest {// A unique shelf id.int64 shelf_id = 1;// A theme of the shelf (fiction, poetry, etc).string shelf_theme = 2;// The size of the shelfint64 shelf_size = 3;}
option (google.api.http) 指定此方法是一个 gRPC HTTP 映射注释。
post 指定此方法映射到 HTTP POST 请求。
"/v1/shelves/{shelf_id}" 是该请求的网址路径。{shelf_id}中的内容就是 shelf_id 字段在CreateShelfRequest中的值。
body: "*" 在 HTTP 请求正文中用于指定此示例中除 shelf_id 之外的所有剩余请求字段,这些字段是 shelf_theme 和 shelf_size。对于 JSON 正文中具有这两个名称的任何字段,其值都将在 CreateShelfRequest 的相应字段中使用。
curl -d '{"shelf_theme":"Music", "shelf_size": 20}' http://DOMAIN_NAME/v1/shelves/123
JSON 正文和路径模板来创建 CreateShelfRequest{shelf_id: 123 shelf_theme: "Music" shelf_size: 20},然后使用该模板调用 gRPC CreateShelf() 方法。
