跳至正文
Wener Notes

gRPC Awesome

约 4 分钟阅读
标签:资源精选

LB/Gateway/Proxy

参考

工具

UI

bash
# 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
http
### helloworld
GRPC localhost:8888/helloworld.Greeter/SayHello
http
### health
GRPC localhost:8888/grpc.health.v1.Health/Check

Health

proto
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

    • 国内项目
    • 使用 grpc 的服务定义和序列化

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
bash
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 .
yaml
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
proto
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";

关联信息

反向链接、本文链接的其他页面和外部资料。

参考资料

GitHub55 条
依赖包1 条
其他外链5 条
最近更新commit 4b6e1d9编辑