https://github.com/bscpaz/poc-grpc-go
This is a POC (proof of concept) to understand better the behavior of gRPC with Go Lang.
https://github.com/bscpaz/poc-grpc-go
grpc grpc-go
Last synced: 2 months ago
JSON representation
This is a POC (proof of concept) to understand better the behavior of gRPC with Go Lang.
- Host: GitHub
- URL: https://github.com/bscpaz/poc-grpc-go
- Owner: bscpaz
- Created: 2021-11-09T19:57:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-10T14:16:28.000Z (over 4 years ago)
- Last Synced: 2024-06-20T19:52:32.841Z (about 2 years ago)
- Topics: grpc, grpc-go
- Language: Go
- Homepage:
- Size: 92.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
gRPC with Go
This is a POC (proof of concept) to understand better the behavior of gRPC with Go Lang.
#### Technologies:
* Go (https://golang.org/);
* _Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of **multicore** and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language._
* https://go.dev/ref/spec
* gRPC (https://grpc.io/);
* _In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services._

* Protocol Buffers (https://developers.google.com/protocol-buffers)
* _Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages._
#### How to get started:
```console
#.profile
export GOROOT=/usr/local/go
export GOPATH=$HOME/projects/go
export PROTOC=$GOPATH/bin
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT:$GOPATH:$PROTOC:$GOBIN
```
#### Initial settings
```console
mkdir pb
```
```console
mkdir proto
```
```console
sudo chown bscpaz:bscpaz pb proto
```
###### Creating a new go module.
```console
go mod init github.com/bscpaz/poc-grpc-go
```
###### Installing protocol buffers on machine.
```console
go get -u google.golang.org/grpc
```
```console
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
```
```console
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
```
###### Generating stubs in Go language
```console
protoc --proto_path=proto proto/*.proto --go_out=pb --go-grpc_out=pb
```
#### How to start gRPC server (stream server):
```console
go run cmd/server/server.go
```
#### How to call gRPC server:
```console
go run cmd/client/client.go
```
#### How to test gRPC calls:
Install and use the Evans project:
* https://github.com/ktr0731/evans
###### After extract evans program into "/usr/local/go/bin" folder, add evans into .profile file:
```console
export EVANS=$GOBIN/evans
export PATH=$PATH:$GOROOT:$GOPATH:$GOBIN:$EVANS
```
###### Create needed folder at user's home directory.
```console
sudo mkdir .cache/evans
```
```console
sudo chown -R bscpaz:bscpaz .cache/
```
###### From project's folder.
```console
evans -r repl --host localhost --port 50051
pb.UserService@localhost:50051> service UserService
pb.UserService@localhost:50051> call AddUser
id (TYPE_INT32) => 0
name (TYPE_STRING) => Bruno Paz
email (TYPE_STRING) => soujava@gmail.com
```
###### Below is the result of client call to server. Note the value of "id":
```console
{
"email": "soujava@gmail.com",
"id": 123456,
"name": "Bruno Paz"
}
pb.UserService@localhost:50051> exit
```
Known issues
```console
Issue:
could not import "google.golang.org/grpc"
Solution:
bscpaz@2am:/$ go get -u google.golang.org/grpc
```
```console
Issue:
bscpaz@2am:$ evans -r repl --host localhost --port 50051
evans: failed to run REPL mode: failed to instantiate a new spec: failed to instantiate the spec:
failed to list packages by gRPC reflection: failed to list services from reflection enabled gRPC server:
rpc error: code = Unimplemented desc = unknown service grpc.reflection.v1alpha.ServerReflection
Solution:
Add reflection mode into code:
reflection.Register(grpcServer)
```
```console
Issue:
Command 'go' not found, but can be installed with:
or
evans: command not found
Solution:
You've opened a new terminal and it requires a new refresh on .profile file.
bscpaz@2am:/$ cd ~
bscpaz@2am:/$ source .profile
```
```console
Issue:
protoc-gen-go: program not found or is not executable
Solution:
protoc-ge-go is installed into '~/projects/go/bin'. Add that path into .profile file
export PROTOC=$GOPATH/bin
bscpaz@2am:/$ source .profile
bscpaz@2am:/$ cd ~/projects/go/poc-grpc-go
bscpaz@2am:/$ sudo chown -R bscpaz:bscpaz poc-grpc-go/
```