study grpc
$ brew install protobuf
$ go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
$ which protoc-gen-go-grpc
$ protoc --go_out=. --go-grpc_out=. proto/greet.proto
$ go mod tidy
$ protoc --proto_path=proto proto/*.proto --go_out=gen/
$ protoc --proto_path=proto proto/*.proto --go-grpc_out=gen/
$ go work init
$ go work use ./tools/ ./tools/gopls/
$ mkdir -p google/api
$ curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/annotations.proto > google/api/annotations.proto
$ curl https://raw.githubusercontent.com/googleapis/googleapis/master/google/api/http.proto > google/api/http.proto
$ protoc -I . --grpc-gateway_out ./gen/ \
--grpc-gateway_opt logtostderr=true \
--grpc-gateway_opt paths=source_relative \
--grpc-gateway_opt generate_unbound_methods=true \
proto/greet.proto
https://pkg.go.dev/github.com/gorilla/mux#section-readme
mux for gateway server
mux port != grpc port
$ brew install bufbuild/buf/buf
$ buf --help
Proxy tool that converts Protocol Buffers and gRPC services into HTTP JSON endpoints.
$ go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc
version: v1
plugins:
- name: go
out: ../pkg
opt:
- paths=source_relative
- name: go-grpc
out: ../pkg
opt:
- paths=source_relative
- name: grpc-gateway
out: ../pkg
opt:
- paths=source_relative
- generate_unbound_methods=true
- name: openapiv2
out: ../pkg
$ go get github.com/envoyproxy/protoc-gen-validate
if vscode ext error
"protoc": {
"path": "/usr/local/bin/protoc",
"options": [
"--proto_path=${workspaceRoot}/common/proto", # -> check
]
}
- simplest type of RPC
- a single request & a single response
client -request-> server
client <-response- server - ex) authentication or data retrieval
- a single request & multiple messages in response
client -request-> server
client <-response(messages)- server - client must wait for the server to send all of the messages before it can continue
- ex) downloading files || returning the results of a database query
- multiple messages in request & a single response
client -request(messages)-> server
client <-response- server - server must wait for the client to send all of the messages before it can send a response
- ex) uploading files || inserting multiple records into a database
- client and server can send multiple messages to each other in either direction
client <-multiple messages-> server - client and server can continue sending messages until one of them closes the connection
- ex) real-time data exchange (chat applications or streaming media)
https://grpc.io/docs https://grpc-ecosystem.github.io/grpc-gateway https://buf.build/docs