This application implements a gRPC server with two services and a client to handle with requests, it also has a Server Streaming implementation.
- gRPC Secure connection with SSL/TLS
- gRPC Server
- gRPC Server streaming
- gRPC Service
- gRPC Client
- gRPC .proto
- Evans Client gRPC
- HTTP Server
- Docker + Docker Compose
$ git clone https://github.com/Sup3r-Us3r/grpc-go-example
$ cd grpc-go-example
The environment were configured using Docker Compose, to start the environment you must run:
$ docker-compose up -d
Access the application container:
$ docker exec -it go-grpc-app /bin/bash
$ go mod tidy
First for you configure gRPC connection with SSL/TLS you need to edit this generate.sh file, and change the following information:
/C
=BR is for country
/ST
=Minas Gerais is for state or province
/L
=Belo Horizonte is for locality name or city
/O
=Example is for organisation
/OU
=Education is for organisation unit
/CN
=*.test is for common name or domain name
/emailAddress
=test@gmail.com is for email address
Now run this command to generate the certificate, but it is important that you have openssl
installed, if you run this command inside the container the image already has openssl installed:
$ make cert
$ go run main.go
For each created service you can use the client that was implemented to perform the request:
$ go run grpc/client/main.go
As this is just an example, when executing the above command the request will be made to the three existing services, you can comment on any that you do not want to be executed in your tests in
grpc/client/main.go
in the functionmain
.
Open a new terminal and access the container app again, and run this command:
gRPC without security
$ evans -r repl
There is only 2 RPC in the ProductService
and 1 RPC in the SmartwatchService
.
To use RPC CreateProduct
run:
goGrpc.ProductService@127.0.0.1:50051> call CreateProduct
To use RPC ListProducts
run:
goGrpc.ProductService@127.0.0.1:50051> call ListProducts
To use RPC BeatsPerMinute
run:
goGrpc.SmartwatchService@127.0.0.1:50051> call BeatsPerMinute
For the .proto
files that are used to define the contract, in case you want to modify something, or need to create new files, it is necessary to use protoc
to generate the .pb
and grpc.pb
files.
Use the command below in the root of the project that uses protoc
internally:
$ make gen