gRPC: 如何让 gRPC 提供 Restful API 服务?

2022年01月14日 阅读数:4
这篇文章主要向大家介绍gRPC: 如何让 gRPC 提供 Restful API 服务?,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

介绍

本文将介绍如何让一个 gRPC 服务,同时提供 gRPC 和 Restful API。html

  • 为了能让 gRPC 提供 REST API,咱们须要使用 grpc-gateway

请访问以下地址获取完整教程:git

使用 rk-boot

rk-boot 是集成了 Gin, gRPC 和一系列流行 Go 语言框架的启动器,用户能够经过 rk-boot 快速启动企业级 Go 语言微服务。github

先决条件

使用过 GRPC 的用户都应该知道,protocol buffer 文件须要使用相关的命令行,把 *.proto 文件编译成 *.go 文件。golang

根据不一样须要,会使用到不一样的命令行文件。以 Go 语言为例,咱们须要大体以下几个命令行文件。json

工具 介绍 安装
protobuf protocol buffer 编译所需的命令行 Install
protoc-gen-go 从 proto 文件,生成 .go 文件 Install
protoc-gen-go-grpc 从 proto 文件,生成 GRPC 相关的 .go 文件 Install
protoc-gen-grpc-gateway 从 proto 文件,生成 grpc-gateway 相关的 .go 文件 Install
protoc-gen-openapiv2 从 proto 文件,生成 swagger 界面所需的参数文件 Install

除了安装上述命令行,咱们还须要根据须要,运行至少4种不一样命令来编译 *.proto 文件,很是晦涩难懂。bootstrap

具体操做方式可参考个人前一篇文章:【GRPC: 使用 Buf 快速编译 GRPC proto 文件】 或者访问:【https://rkdev.info/cn/docs/bootstrapper/user-guide/grpc-golang/basic/grpc-gateway/】api

安装

go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc

快速开始

1.建立 api/v1/greeter.proto

syntax = "proto3";

package api.v1;

option go_package = "api/v1/greeter";

service Greeter {
  rpc Greeter (GreeterRequest) returns (GreeterResponse) {}
}

message GreeterRequest {
  string name = 1;
}

message GreeterResponse {
  string message = 1;
}

2.建立 api/v1/gw_mapping.yaml

type: google.api.Service
config_version: 3

# Please refer google.api.Http in https://github.com/googleapis/googleapis/blob/master/google/api/http.proto file for details.
http:
  rules:
    - selector: api.v1.Greeter.Greeter
      get: /api/v1/greeter

3.建立 buf.yaml

version: v1beta1
name: github.com/rk-dev/rk-demo
build:
  roots:
    - api

4.建立 buf.gen.yaml

version: v1beta1
plugins:
  # protoc-gen-go needs to be installed, generate go files based on proto files
  - name: go
    out: api/gen
    opt:
     - paths=source_relative
  # protoc-gen-go-grpc needs to be installed, generate grpc go files based on proto files
  - name: go-grpc
    out: api/gen
    opt:
      - paths=source_relative
      - require_unimplemented_servers=false
  # protoc-gen-grpc-gateway needs to be installed, generate grpc-gateway go files based on proto files
  - name: grpc-gateway
    out: api/gen
    opt:
      - paths=source_relative
      - grpc_api_configuration=api/v1/gw_mapping.yaml
  # protoc-gen-openapiv2 needs to be installed, generate swagger config files based on proto files
  - name: openapiv2
    out: api/gen
    opt:
      - grpc_api_configuration=api/v1/gw_mapping.yaml

5.编译 proto file

$ buf generate

以下的文件会被建立。app

$ tree api/gen 
api/gen
└── v1
    ├── greeter.pb.go
    ├── greeter.pb.gw.go
    ├── greeter.swagger.json
    └── greeter_grpc.pb.go
 
1 directory, 4 files

6.建立 boot.yaml

grpc:
  - name: greeter                   # Name of grpc entry
    port: 8080                      # Port of grpc entry
    enabled: true                   # Enable grpc entry
    enableReflection: true

7. 建立 main.go

package main

import (
	"context"
	"fmt"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-demo/api/gen/v1"
	"github.com/rookie-ninja/rk-grpc/boot"
	"google.golang.org/grpc"
)

// Application entrance.
func main() {
    // Create a new boot instance.
    boot := rkboot.NewBoot()

    // ***************************************
    // ******* Register GRPC & Gateway *******
    // ***************************************

    // Get grpc entry with name
    grpcEntry := boot.GetEntry("greeter").(*rkgrpc.GrpcEntry)
    // Register grpc registration function
    grpcEntry.AddRegFuncGrpc(registerGreeter)
    // Register grpc-gateway registration function
    grpcEntry.AddRegFuncGw(greeter.RegisterGreeterHandlerFromEndpoint)

    // Bootstrap
    boot.Bootstrap(context.Background())

    // Wait for shutdown sig
    boot.WaitForShutdownSig(context.Background())
}

// Implementation of [type GrpcRegFunc func(server *grpc.Server)]
func registerGreeter(server *grpc.Server) {
    greeter.RegisterGreeterServer(server, &GreeterServer{})
}

// Implementation of grpc service defined in proto file
type GreeterServer struct{}

func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
    return &greeter.GreeterResponse{
        Message: fmt.Sprintf("Hello %s!", request.Name),
    }, nil
}

8.文件夹结构

$ tree
.
├── api
│   ├── gen
│   │   └── v1
│   │       ├── greeter.pb.go
│   │       ├── greeter.pb.gw.go
│   │       ├── greeter.swagger.json
│   │       └── greeter_grpc.pb.go
│   └── v1
│       ├── greeter.proto
│       └── gw_mapping.yaml
├── boot.yaml
├── buf.gen.yaml
├── buf.yaml
├── go.mod
├── go.sum
└── main.go

4 directories, 12 files

9.验证

rk-boot 会使用同一个端口来映射 gRPC 和 gRPC-gateway,很是方便。框架

$ go run main.go
  • 验证 Restful API
$ curl "localhost:8080/api/v1/greeter?name=rk-dev"
{"message":"Hello rk-dev!"}
  • 验证 gRPC
$ grpcurl -d '{"name": "rk-dev"}' -plaintext localhost:8080 api.v1.Greeter.Greeter
{
  "message": "Hello rk-dev!"
}

Gateway server option

rk-boot 有一个 enableRkGwOption 选项,启动以后,会包含以下几个功能。curl

RK 推荐的 server option,提供以下的功能。

rkgrpc.RkGwServerMuxOptions

功能 详情
HttpErrorHandler 主要代码从原有 grpc-gateway 代码中抄写而来,启动器会返回 RK 推荐的 API 错误结构
MarshalerOption protojson.MarshalOptions{UseProtoNames: false, EmitUnpopulated: true},UnmarshalOptions: protojson.UnmarshalOptions{}}
Metadata 注入以下 GRPC metadata: x-forwarded-method, x-forwarded-path, x-forwarded-scheme, x-forwarded-user-agent 和 x-forwarded-remote-addr
OutgoingHeaderMatcher 把原有的 grpc metadata 转发到 http 头部(没有前缀)
IncomingHeaderMatcher 把原有的 http 头部 转发到 grpc metadata(没有前缀)

1.启动 enableRkGwOption

为了可以验证,咱们会添加 gRPC 日志拦截器。

  • boot.yaml
grpc:
  - name: greeter                   # Name of grpc entry
    port: 8080                      # Port of grpc entry
    enabled: true                   # Enable grpc entry
    enableReflection: true          # Enable grpc reflection
    enableRkGwOption: true          # Enable rk style grpc gateway server option
    interceptors:
      loggingZap:
        enabled: true               # Enable logging interceptor for validation

2.验证日志

$ curl "localhost:8080/api/v1/greeter?name=rk-dev"

gwMethod, gwPath, gwScheme, gwUserAgent 将会记录到日志中

------------------------------------------------------------------------
endTime=2021-07-09T21:03:43.518106+08:00
...
payloads={"grpcMethod":"Greeter","grpcService":"api.v1.Greeter","grpcType":"unaryServer","gwMethod":"GET","gwPath":"/v1/greeter","gwScheme":"http","gwUserAgent":"curl/7.64.1"}
...

3.验证传入的 metadata

func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
    // Print incoming headers
    fmt.Println(rkgrpcctx.GetIncomingHeaders(ctx))

    return &greeter.GreeterResponse{
        Message: fmt.Sprintf("Hello %s!", request.Name),
    }, nil
}
map[:authority:[0.0.0.0:8080] accept:[*/*] content-type:[application/grpc] user-agent:[grpc-go/1.38.0] x-forwarded-for:[::1] x-forwarded-host:[localhost:8080] x-forwarded-method:[GET] x-forwarded-path:[/v1/greeter] x-forwarded-remote-addr:[[::1]:57082] x-forwarded-scheme:[http] x-forwarded-user-agent:[curl/7.64.1]]

错误映射

grpc-gateway 中,咱们须要了解 gateway 到 grpc 的错误映射,即 http 到 grpc 的错误映射。

这是默认的 grpc-gateway 中的错误映射。

GRPC 错误码 GRPC 错误码描述 Gateway(Http) 错误码 Gateway(Http) 错误码描述
0 OK 200 OK
1 CANCELLED 408 Request Timeout
2 UNKNOWN 500 Internal Server Error
3 INVALID_ARGUMENT 400 Bad Request
4 DEADLINE_EXCEEDED 504 Gateway Timeout
5 NOT_FOUND 404 Not Found
6 ALREADY_EXISTS 409 Conflict
7 PERMISSION_DENIED 403 Forbidden
8 RESOURCE_EXHAUSTED 429 Too Many Requests
9 FAILED_PRECONDITION 400 Bad Request
10 ABORTED 409 Conflict
11 OUT_OF_RANGE 400 Bad Request
12 UNIMPLEMENTED 501 Not Implemented
13 INTERNAL 500 Internal Server Error
14 UNAVAILABLE 503 Service Unavailable
15 DATA_LOSS 500 Internal Server Error
16 UNAUTHENTICATED 401 Unauthorized

1.验证错误(标准 Go 语言错误)

根据错误映射,将会返回 500。

func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
	return nil, errors.New("error triggered manually")
}
$ curl "localhost:8080/api/v1/greeter?name=rk-dev"
{
    "error":{
        "code":500,
        "status":"Internal Server Error",
        "message":"error triggered manually",
        "details":[]
    }
}

2.验证错误(grpc 错误)

咱们须要经过 status.New() 来建立 GRPC 错误。咱们推荐使用 rkerror 库中的函数来建立错误。

func (server *GreeterServer) Greeter(ctx context.Context, request *greeter.GreeterRequest) (*greeter.GreeterResponse, error) {
	return nil, rkerror.PermissionDenied("permission denied manually").Err()
}
curl "localhost:8080/v1/greeter?name=rk-dev"
{
    "error":{
        "code":403,
        "status":"Forbidden",
        "message":"permission denied manually",
        "details":[
            {
                "code":7,
                "status":"PermissionDenied",
                "message":"[from-grpc] permission denied manually"
            }
        ]
    }
}