brew install protoc-gen-go
brew install protoc-gen-go-grpc
gRPC | REST |
---|---|
Protocol Buffers | JSON |
HTTP 2 | HTTP 1 |
Streaming | Unary |
Bi directional | Client->Server |
Free design | GET/POST/UPDATE/DELETE/... |
rpc Greet (GreetRequest) returns (GreetResponse);
rpc GreetManyTimes (GreetRequest) returns (stream GreetResponse);
rpc LongGreet (stream GreetRequest) returns (GreetResponse);
rpc GreetEveryone (stream GreetRequest) returns (stream GreetResponse);
Generators are in this folder: SSL
// .......
tls := true // change that to false if needed
opts := []grpc.DialOption{}
if tls {
certFile := "ssl/ca.crt"
creds, err := credentials.NewClientTLSFromFile(certFile, "localhost")
if err != nil {
log.Fatalf("Failed loading certificated: %v\n", err)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
}
conn, err := grpc.Dial(addr, opts...)
// .......
// .......
opts := []grpc.ServerOption{}
tls := true // change that to false if needed
if tls {
certFile := "ssl/server.crt"
keyFile := "ssl/server.pem"
creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
if err != nil {
log.Fatalf("Failed loading certificated: %v\n", err)
}
opts = append(opts, grpc.Creds(creds))
}
log.Printf("Listening on %s\n", addr)
s := grpc.NewServer(opts...)
// .......
Install Chocolatey (https://chocolatey.org/install)
choco install make
You should then be able to use make command in the directory that contains the Makefile.
This is optional, since you can still build the .proto files by hand by running the following commands:
protoc -I${PROJECT}/proto --go_opt=module=${YOUR_MODULE} --go_out=. ${PROJECT}/proto/*.proto
where ${YOUR_MODULE}
is the name of your go module (you can find that in your go.mod file) and ${PROJECT}
is one of the projects name (greet, calculator, blog).
https://grpc.io/docs/guides/auth/
Failed to connect: grpc: no transport security set (use grpc.WithTransportCredentials(insecure.NewCredentials()) explicitly or set credentials)
Go into settings > Extensions > vscode-proto3 configuration
and then click Edit in settings.json
. (you can just edit .vscode/settings.json
too.)
After that, give --proto_path
options like below codes.
{
"protoc": {
"options": [
"--proto_path=<path of your proto files>"
]
}
}
- Manually gRPC API inspection
- To automate some tasks by scripting
// .......
s := grpc.NewServer()
pb.RegisterCalculatorServiceServer(s, &Server{})
reflection.Register(s)
// .......
evans --host localhost --post 50051 --reflection
- https://grpc.io/docs/languages/go/quickstart/
- https://husobee.github.io/golang/rest/grpc/2016/05/28/golang-rest-v-grpc.html
- https://www.slideshare.net/borisovalex/grpc-vs-rest-let-the-battle-begin-81800634
- https://learn.microsoft.com/en-us/aspnet/core/grpc/comparison?view=aspnetcore-3.0
- https://github.com/ktr0731/evans
- https://github.com/googleapis/googleapis/blob/master/google/pubsub/v1/pubsub.proto
- https://github.com/googleapis/googleapis/blob/master/google/spanner/v1/spanner.proto