golang GRPC example
REF:
grpc golang example
Golang gRPC In-Depth Tutorial [Create Server & Client] This is a simple and good example.
grpcurl -import-path ./proto -proto todo.proto lis
grpcurl -proto ./proto/todo.proto list
grpcurl -proto ./proto/todo.proto describe
grpcurl -proto ./proto/todo.proto describe proto.TodoService
grpcurl -proto ./proto/todo.proto describe proto.TodoService.CreateTodo
grep ServiceName ./proto/todo_grpc.pb.go
grep FullMethod ./proto/todo_grpc.pb.go
grep "rpc " ./proto/todo.proto
grep "message.*NewTodo" -n3 ./proto/todo.proto
grpcurl -plaintext -d '{"name": "Code review", "description": "Review new feature code", "done": false}' \
-proto ./proto/todo.proto localhost:50051 proto.TodoService/CreateTodo
proto=./proto/todo.proto
service=proto.TodoService
method=proto.TodoService.CreateTodo
grpc_cli call localhost:50051 SayHello "name: 'world'" \
--protofiles=./proto/todo.proto
grpc_cli ls localhost:50051 --protofiles=./proto/todo.proto
grpc_cli --protofiles=$proto ls localhost:50051 $service -l
grpc_cli --protofiles=$proto ls localhost:50051 $method -l
How to create a gRPC server with Golang?
https://grpc.io/docs/languages/go/basics/
https://grpc.io/docs/languages/go/generated-code/
README: https://pkg.go.dev/google.golang.org/grpc
Go installation
we should better build in a golang container
# https://go.dev/dl/go1.20.2.linux-amd64.tar.gz wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz # wget https://golang.org/dl/go1.18.linux-amd64.tar.gz rm -rf /usr/local/go && tar -C /usr/local -xzf go1.20.2.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin # https://betterprogramming.pub/install-go-1-11-on-ubuntu-18-04-16-04-lts-8c098c503c5f echo 'export PATH=$PATH:/usr/local/go/bin' | tee -a $HOME/.profile or # https://www.fosslinux.com/68795/install-go-on-ubuntu.htm sudo apt install golang-go
install protobuf
https://grpc.io/docs/languages/go/quickstart/
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
https://grpc.io/docs/protoc-installation/
sudo apt install -y protobuf-compiler protoc --version # Ensure compiler version is 3+
Create a go module
https://go.dev/doc/tutorial/create-module
mkdir greetings; cd greetings
go mod init example/greetings
# go: to add module requirements and sums:
# go mod tidy
mkdir cmd
cat << EOF > cmd/greetings.go
package main
import "fmt" func main() { fmt.Println("Hello, World!") }
EOF
go run ./cmd/greetings.go
Add GRPC API
mkdir -p api/v1
create a proto file
cat << EOF > api/v1/activity.proto syntax = "proto3"; option go_package = "google.golang.org/grpc/example/hello"; package api.v1; import "google/protobuf/timestamp.proto"; message Activity { int32 id = 1; google.protobuf.Timestamp time = 2; string description = 3; } EOF
Gerenate GRPC code
protoc api/v1/*.proto \ --go_out=. \ --go_opt=paths=source_relative \ --go-grpc_out=. \ --go-grpc_opt=paths=source_relative \ --proto_path=.
Install grpcurl
how to install a specific version of grpcurl?
How to install the grpcurl binary on Linux?
go get github.com/fullstorydev/grpcurl/... go install github.com/fullstorydev/grpcurl/cmd/grpcurl
用法:
https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md
Testing your gRPC services using grpc_cli
sudo apt-get install build-essential autoconf libtool pkg-config
sudo apt-get install cmake
sudo apt-get install clang libc++-dev
git clone -b https://github.com/grpc/grpc
cd grpc
git submodule update --init
mkdir -p cmake/build
cd cmake/build
cmake -DgRPC_BUILD_TESTS=ON ../..
make grpc_cli