Skip to content

kimi0230/practise-gRPC-go

Repository files navigation

Practise gRPC Golang

Practise gRPC base on simplesteph/grpc-go-course.

Content

  • 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)

Installation

MAC

# 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
installation

Evans has been created to use easier than other existing gRPC clients.

brew tap ktr0731/evans
brew install evans
usage
# connect to gRPC server
evans -p 50051 -r

# commands
show package
show service
call Sum

MongoDB

1. Offical Install Package

download

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

2. Brew Install

# 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

Generate protobuf

greet

make build_greet

calculator

make build_calculator

blog

make build_blog

gpubsub

make build_gpubsub

SSL

generate SSL Key

cd ssl
./instructions.sh  

code sample

1. Base case - no encryption or authentication

Client:
conn, _ := grpc.Dial("localhost:50051", grpc.WithInsecure())
// error handling omitted
client := pb.NewGreeterClient(conn)
// ...
Server:
s := grpc.NewServer()
lis, _ := net.Listen("tcp", "localhost:50051")
// error handling omitted
s.Serve(lis)

2. With server authentication SSL/TLS

Client:
creds, _ := credentials.NewClientTLSFromFile(certFile, "")
conn, _ := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))
// error handling omitted
client := pb.NewGreeterClient(conn)
// ...
Server:
creds, _ := credentials.NewServerTLSFromFile(certFile, keyFile)
s := grpc.NewServer(grpc.Creds(creds))
lis, _ := net.Listen("tcp", "localhost:50051")
// error handling omitted
s.Serve(lis)

3. Authenticate with Google

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)
// ...

Demo

greet

go run greet/greet_server/server.go
go run greet/greet_client/client.go

calculator

go run calculator/calculator_server/server.go
go run calculator/calculator_client/client.go

blog

go run blog/blog_server/server.go
go run blog/blog_client/client.go

gpubsub

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

Reference

Notes

數字1,2,3,4 是用來代表編號、唯一識別碼,好讓程式識別這個變數,因為到時大家都被壓縮成二進制 認不出誰是誰,有編號要認人比較方便。 在同一個message裡面識別碼不可重複,但不同message之間重複就沒關係了

message Blog {
  string id = 1;
  string author_id = 2;
  string title = 3;
  string content = 4;
}

TODO