跳到主要内容

gRPC Awesome

LB/Gateway/Proxy

参考

工具

UI

# brew install grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui@latest
grpcui -plaintext localhost:12345

# 使用 反射 则不需要 proto 信息
grpcui -use-reflection -base-path /api/rpc -plaintext localhost:18080
### helloworld
GRPC localhost:8888/helloworld.Greeter/SayHello
### health
GRPC localhost:8888/grpc.health.v1.Health/Check

Health

syntax = "proto3";

package grpc.health.v1;

message HealthCheckRequest {
string service = 1;
}

message HealthCheckResponse {
enum ServingStatus {
UNKNOWN = 0;
SERVING = 1;
NOT_SERVING = 2;
SERVICE_UNKNOWN = 3; // Used only by the Watch method.
}
ServingStatus status = 1;
}

service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);

rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}

RPC

gen

  • protoc plugins
  • protoc-gen-go-grpc
  • protoc-gen-grpc
  • protoc-gen-grpc-gateway
  • protoc-gen-grpc-java
  • protoc-gen-grpc-web
  • protoc-gen-go-drpc
    • storj/drpc
      • 一个更加简单轻量的 RPC 协议
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
go install github.com/grpc-ecosystem/protoc-gen-grpc-gateway-ts@latest
go install github.com/envoyproxy/protoc-gen-validate@latest

#
go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest
go install github.com/planetscale/vtprotobuf/cmd/protoc-gen-go-vtproto@latest
go install github.com/srikrsna/protoc-gen-gotag@latest
go install github.com/chrusty/protoc-gen-jsonschema/cmd/protoc-gen-jsonschema@latest

# 调用单个 插件
protoc -I . helloworld.proto --doc_out .
version: v1
managed:
enabled: true
go_package_prefix:
default: github.com/wenerme/torrenti/pkg/apis
except:
- buf.build/x/bundle
- buf.build/googleapis/googleapis
- buf.build/envoyproxy/protoc-gen-validate
plugins:
- name: go
out: .
opt: paths=source_relative
# - name: gotag
# out: .
# opt: paths=source_relative
- name: go-grpc
out: .
opt: paths=source_relative,require_unimplemented_servers=true
- name: grpc-gateway
out: .
opt:
- paths=source_relative
- generate_unbound_methods=true
#- grpc_api_configuration=path/to/config.yaml
#- standalone=true
- name: openapiv2
out: openapiv2
- name: grpc-gateway-ts
out: gen/web/api
opt: paths=source_relative

- name: doc
out: gen/doc
strategy: all
# <FORMAT>|<TEMPLATE_FILENAME>,<OUT_FILENAME>[,default|source_relative]
# FORMAT=docbook,html,markdown,json
# TEMPLATE_FILENAME Go template 文件
# source_relative 输出基于 input 的相对路径
opt: markdown,proto.md

- name: micro # go micro - web proxy & service
out: .
opt:
- paths=source_relative
- require_unimplemented_servers=false

- name: validate
out: .
opt:
- lang=go
- paths=source_relative
syntax = "proto3";

package acme.weather.v1;

option go_package = "github.com/acme/weather/gen/proto/go/acme/weather/v1;weatherv1";
option java_multiple_files = true;
option java_outer_classname = "WeatherProto";
option java_package = "com.acme.weather.v1";