|
|
@ -1,7 +1,65 @@
|
|
|
|
# GO GRPC BASIC
|
|
|
|
# GO GRPC BASIC
|
|
|
|
|
|
|
|
|
|
|
|
### Requirements ( Do It Letter )
|
|
|
|
## Requirements
|
|
|
|
|
|
|
|
check other branch to step by step create project
|
|
|
|
|
|
|
|
### Validate Go Installation
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
$ go version
|
|
|
|
|
|
|
|
go version go1.18.3 linux/amd64
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Validate GOPATH
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
echo $GOPATH
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Start Project in Go
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
cd $GOPATH/src
|
|
|
|
|
|
|
|
mkdir -p github.com/ajikamaludin/go-grpc_basic
|
|
|
|
|
|
|
|
cd github.com/ajikamaludin/go-grpc_basic
|
|
|
|
|
|
|
|
go mod init github.com/ajikamaludin/go-grpc_basic
|
|
|
|
|
|
|
|
go mod tidy
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Install Protoc
|
|
|
|
|
|
|
|
Linux : Download zip file, extract zip file to ~/.local/, add PATH ~/.local/bin
|
|
|
|
|
|
|
|
please read documentation from this link for more detail information
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
https://grpc.io/docs/protoc-installation/
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Validate Protoc Installation
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
$ protoc --version
|
|
|
|
|
|
|
|
libprotoc 3.21.2
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Install Protoc Dependecy for Golang
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
# protoc-gen-go
|
|
|
|
|
|
|
|
go install github.com/golang/protobuf/protoc-gen-go@latest
|
|
|
|
|
|
|
|
# proto (optional, execute in project dir)
|
|
|
|
|
|
|
|
go get google.golang.org/protobuf/proto@latest
|
|
|
|
|
|
|
|
# protoc-gen-grpc-gateway
|
|
|
|
|
|
|
|
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@latest
|
|
|
|
|
|
|
|
# protoc-gen-swagger
|
|
|
|
|
|
|
|
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@latest
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Add PATH
|
|
|
|
|
|
|
|
add to your ~/.bashrc or ~/.zshrc file
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
export PATH="$PATH:$GOPATH/bin"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
### Validate Protoc Dependency Golang Installation
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
~ ❯ ls $GOPATH/bin
|
|
|
|
|
|
|
|
dlv gopls protoc-gen-go protoc-gen-grpc-gateway revel
|
|
|
|
|
|
|
|
go-outline grpcurl protoc-gen-go-grpc protoc-gen-swagger
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Start Project
|
|
|
|
### Setup Project
|
|
|
|
### Setup Project
|
|
|
|
- create `proto` dir
|
|
|
|
- create `proto` dir
|
|
|
|
- create versioning dir and service dir `health`
|
|
|
|
- create versioning dir and service dir `health`
|
|
|
@ -25,16 +83,17 @@
|
|
|
|
- create `api/v1/health/status.go` as method implment from protobuf / pb file
|
|
|
|
- create `api/v1/health/status.go` as method implment from protobuf / pb file
|
|
|
|
- create `router` dir in root project
|
|
|
|
- create `router` dir in root project
|
|
|
|
- create `grpc.go` in router dir and implement NewGRPCServer and register health api service
|
|
|
|
- create `grpc.go` in router dir and implement NewGRPCServer and register health api service
|
|
|
|
- `go get github.com/soheilhy/cmux`, is for ?
|
|
|
|
- `go get github.com/soheilhy/cmux`, TODO: what is for ?
|
|
|
|
- create `router.go` in router dir and implement IgnoreErr, this is for ignore error so can be safely ignore
|
|
|
|
- create `router.go` in router dir and implement IgnoreErr, this is for ignore error so can be safely ignore
|
|
|
|
- `go get golang.org/x/sync/errgroup`, is for ?
|
|
|
|
- `go get golang.org/x/sync/errgroup`, TODO: what is for ?
|
|
|
|
- implement `main.go` to create grpc server from grpc.go with errgroup handler
|
|
|
|
- implement `main.go` to create grpc server from grpc.go with errgroup handler
|
|
|
|
- `go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest`
|
|
|
|
- `go run .`, run server grpc
|
|
|
|
|
|
|
|
- `go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest`, this tool is
|
|
|
|
- test `grpcurl -plaintext localhost:5566 list`, to show list name of services
|
|
|
|
- test `grpcurl -plaintext localhost:5566 list`, to show list name of services
|
|
|
|
- test `grpcurl -plaintext localhost:5566 list api.gogrpc.v1.health.HealthService`, to show list name of service methods
|
|
|
|
- test `grpcurl -plaintext localhost:5566 list api.gogrpc.v1.health.HealthService`, to show list name of service methods
|
|
|
|
- test `grpcurl -plaintext localhost:5566 api.gogrpc.v1.health.HealthService.Status`, to test method call in grpc
|
|
|
|
- test `grpcurl -plaintext localhost:5566 api.gogrpc.v1.health.HealthService.Status`, to test method call in grpc
|
|
|
|
- result
|
|
|
|
- result
|
|
|
|
```
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"success": true,
|
|
|
|
"success": true,
|
|
|
|
"code": "0000",
|
|
|
|
"code": "0000",
|
|
|
|