Open14
grpshuffleにBufのConnectを使ってみる
- grpshuffle-serverはgRPCを提供している
- grpshuffle-clientはserverを叩くようのクライアントだが、単純にCLIとしてgRPCを叩く機能とは別に、REST API serverを起動する機能がある
- このREST APIは自分でがんばってスキーマ決めて構築しているので、そこをconnect-goで簡略化できると嬉しい
まずはgrpshuffle-serverをconnectに置き換えてみよう。
何もせずにclientから使えるかな?
getting-startedを参考にやっていき
buf mod init
で作成。
buf.yaml
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
getting started通りにbuf.gen.yaml
を作成。
buf.gen.yaml
version: v1
plugins:
- name: go
out: gen
opt: paths=source_relative
- name: connect-go
out: gen
opt: paths=source_relative
lintがやべえ怒られた
❯ buf lint
grpshuffle.proto:2:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
grpshuffle.proto:2:1:Files with package "grpshuffle" must be within a directory "grpshuffle" relative to root but were in directory ".".
grpshuffle.proto:2:1:Package name "grpshuffle" should be suffixed with a correctly formed version, such as "grpshuffle.v1".
grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
health.proto:20:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
health.proto:20:1:Files with package "grpc.health.v1" must be within a directory "grpc/health/v1" relative to root but were in directory ".".
health.proto:34:5:Enum value name "UNKNOWN" should be prefixed with "SERVING_STATUS_".
health.proto:34:5:Enum zero value name "UNKNOWN" should be suffixed with "_UNSPECIFIED".
health.proto:35:5:Enum value name "SERVING" should be prefixed with "SERVING_STATUS_".
health.proto:36:5:Enum value name "NOT_SERVING" should be prefixed with "SERVING_STATUS_".
health.proto:37:5:Enum value name "SERVICE_UNKNOWN" should be prefixed with "SERVING_STATUS_".
health.proto:42:9:Service name "Health" should be suffixed with "Service".
health.proto:45:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
health.proto:45:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
health.proto:62:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
health.proto:62:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
health.proto:62:13:RPC request type "HealthCheckRequest" should be named "WatchRequest" or "HealthWatchRequest".
health.proto:62:49:RPC response type "HealthCheckResponse" should be named "WatchResponse" or "HealthWatchResponse".
grpshuffle.protoとhealth.protoを別ディレクトリに分けないといけないっぽい。
grpshuffle.proto:2:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
health.proto:20:1:Multiple packages "grpc.health.v1,grpshuffle" detected within directory ".".
あと、ルートから見てpackage名通りにディレクトリを掘らないといけないっぽい
grpshuffle.proto:2:1:Files with package "grpshuffle" must be within a directory "grpshuffle" relative to root but were in directory ".".
health.proto:20:1:Files with package "grpc.health.v1" must be within a directory "grpc/health/v1" relative to root but were in directory ".".
さらに、ちゃんと.v1
みたいにバージョン番号つけろやって出てきた
grpshuffle.proto:2:1:Package name "grpshuffle" should be suffixed with a correctly formed version, such as "grpshuffle.v1".
ディレクトリを移動
grpshuffle.proto → grpshuffle/v1/grpshuffle.proto
パッケージ名を変更
grpshuffle.proto
- package grpshuffle;
+ package grpshuffle.v1;
さらなるエラー
❯ buf lint
grpc/health/v1/health.proto:34:5:Enum value name "UNKNOWN" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:34:5:Enum zero value name "UNKNOWN" should be suffixed with "_UNSPECIFIED".
grpc/health/v1/health.proto:35:5:Enum value name "SERVING" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:36:5:Enum value name "NOT_SERVING" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:37:5:Enum value name "SERVICE_UNKNOWN" should be prefixed with "SERVING_STATUS_".
grpc/health/v1/health.proto:42:9:Service name "Health" should be suffixed with "Service".
grpc/health/v1/health.proto:45:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:45:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:3:"grpc.health.v1.HealthCheckRequest" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:3:"grpc.health.v1.HealthCheckResponse" is used as the request or response type for multiple RPCs.
grpc/health/v1/health.proto:62:13:RPC request type "HealthCheckRequest" should be named "WatchRequest" or "HealthWatchRequest".
grpc/health/v1/health.proto:62:49:RPC response type "HealthCheckResponse" should be named "WatchResponse" or "HealthWatchResponse".
grpshuffle/v1/grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
grpc.health.v1
はそもそもgit管理してないし消すか(なんかの理由があってローカルに置いてる)
消したら一個に減った。
Serviceは後ろにServiceってつけろよって言われてる。つけた
❯ buf lint
grpshuffle/v1/grpshuffle.proto:25:9:Service name "Compute" should be suffixed with "Service".
lint通ったのでgenerate。お〜できたできた
❯ buf generate
❯ tree gen
gen
└── grpshuffle
└── v1
├── grpshuffle.pb.go
└── grpshuffleconnect
└── grpshuffle.connect.go
3 directories, 2 files
grpc.health.v1
のprotoファイルはどう扱うのがいいのか
なんかhealthの実装がパッケージであった
bufbuild/connect-grpchealth-go
どうやらbuf.work.yaml
でprotoファイルのルートを指定できるらしい
buf.work.yaml // workspaceを指定。この場合は proto ディレクトリを指定する
https://tech.tier4.jp/entry/2022/02/02/160000
buf.work.yaml | Buf®
buf.work.yaml
version: v1
directories:
- proto
こんな感じになった
❯ tree proto
proto
└── korosuke613
└── grpshuffle
└── v1
└── grpshuffle.proto
3 directories, 1 file