Practise gRPC base on simplesteph/grpc-go-course.
- PubSub moby/pkg/pubsub
- Greeting Service
- Calculator Service
- Unary, Server Streaming, Client Streaming, BiDi Streaming
- Error Handling, Deadlines, SSL Encryption
- Blog API CRUD w/ MongoDB
- metadata (header)
# protobuf
brew install protobuf
go get -u google.golang.org/grpc
go get -u github.com/golang/protobuf/protoc-gen-go
# Use it to generate faster marshaling and unmarshaling go code for your protocol buffers.
go get -u github.com/gogo/protobuf/protoc-gen-gofast
# MongoDB Go Driver
go get -u go.mongodb.org/mongo-driver/mongo
go get -u gopkg.in/mgo.v2/bson
# docker pub/sub
go get -u github.com/moby/moby/pkg/pubsub
Evans has been created to use easier than other existing gRPC clients.
brew tap ktr0731/evans
brew install evans
# connect to gRPC server
evans -p 50051 -r
# commands
show package
show service
call Sum
https://www.mongodb.com/try/download/community
mv file to /usr/local and change name
cd /usr/local
mv ~/mongodb-macos-x86_64-4.4.6/ mongodb
add bin to path
vi ~/.zshrc
# add PATH
export PATH=/usr/local/mongodb/bin:$PATH
source ~/.zshrc
create data, log folder
# create data, log folder
sudo mkdir -p /usr/local/var/mongodb
sudo mkdir -p /usr/local/var/log/mongodb
# make sure folder permission
sudo chown kimiimac /usr/local/var/mongodb
sudo chown kimiimac /usr/local/var/log/mongodb
start MongoDB
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
# install
brew tap mongodb/brew
sudo chown -R $(whoami) /usr/local/var/log
chmod u+w /usr/local/var/log
brew install mongodb-community
# brew start
brew services start mongodb-community
# start MongoDB
mongod --config /usr/local/etc/mongod.conf --fork
make build_greet
make build_calculator
make build_blog
make build_gpubsub
cd ssl
./instructions.sh
conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
// error handling omitted
client := pb.NewGreeterClient(conn)
// ...
s := grpc.NewServer()
lis, _ := net.Listen("tcp", "localhost:50051")
// error handling omitted
s.Serve(lis)
creds, _ := credentials.NewClientTLSFromFile(certFile, "")
conn, _ := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))
// error handling omitted
client := pb.NewGreeterClient(conn)
// ...
creds, _ := credentials.NewServerTLSFromFile(certFile, keyFile)
s := grpc.NewServer(grpc.Creds(creds))
lis, _ := net.Listen("tcp", "localhost:50051")
// error handling omitted
s.Serve(lis)
pool, _ := x509.SystemCertPool()
// error handling omitted
creds := credentials.NewClientTLSFromCert(pool, "")
perRPC, _ := oauth.NewServiceAccountFromFile("service-account.json", scope)
conn, _ := grpc.Dial(
"greeter.googleapis.com",
grpc.WithTransportCredentials(creds),
grpc.WithPerRPCCredentials(perRPC),
)
// error handling omitted
client := pb.NewGreeterClient(conn)
// ...
go run greet/greet_server/server.go
go run greet/greet_client/client.go
go run calculator/calculator_server/server.go
go run calculator/calculator_client/client.go
go run blog/blog_server/server.go
go run blog/blog_client/client.go
go run gpubsub/gpubsub_server/server.go
# subscriber
go run gpubsub/gpubsub_client_sub/client.go
# publisher
go run gpubsub/gpubsub_client_pub/client.go
- https://developers.google.com/protocol-buffers/docs/gotutorial
- https://github.com/simplesteph/grpc-go-course
- https://github.com/gogo/protobuf
- https://grpc.io/docs/guides/auth/
- https://github.com/ktr0731/evans
- https://github.com/mongodb/mongo-go-driver
- https://grpc.io/
- https://jbrandhorst.com/post/gogoproto/
- https://github.com/gogo/grpc-example
- https://jbrandhorst.com/post/gogoproto/
- https://www.itread01.com/content/1587524882.html
- Robust gRPC communication on Google Cloud Run (but not only!)
數字1,2,3,4 是用來代表編號、唯一識別碼,好讓程式識別這個變數,因為到時大家都被壓縮成二進制 認不出誰是誰,有編號要認人比較方便。 在同一個message裡面識別碼不可重複,但不同message之間重複就沒關係了
message Blog {
string id = 1;
string author_id = 2;
string title = 3;
string content = 4;
}