Go based grpc - grpc gateway micro service example

go-grpc-gateway-server

This repository provides an example for go based microservice. Go micro services developed based on gRPC protobuf's and also uses gRPC gateway as a reverse proxy to support JSON clients/requests.

Dev pre-requisites

  1. golang

    brew install go
    Note: Make sure to have go1.17.2 or latest
    
  2. Docker

    brew install docker
    

    or downlooad docker from here

  3. PgAdmin for Postgres DB, download and this is optional if you prefer cli

  4. Update env

    vi ~/.zshrc
    
    add below entries, save & exit.
    
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    export GONOPROXY="github.com,gopkg.in,go.uber.org"
    
    source ~/.zshrc
    

Install protoc

If you have Homebrew (which you can get from https://brew.sh), just run:

brew install protobuf

If you see any error messages, run brew doctor, follow any recommended fixes, and try again. If it still fails, try instead:

brew upgrade protobuf

Alternately, run the following commands:

PROTOC_ZIP=protoc-3.14.0-osx-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP```
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest \
google.golang.org/protobuf/cmd/protoc-gen-go@latest \
google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Note: Make sure protobuf's installed correctly by running protoc --version

Generate protobuf's

Note: Required this step only when you make changes to .proto file or introduce a new one

cd pkg/api

go generate ./...

(or) alternative try below in case you did not set GOPATH

env PATH="$HOME/go/bin:$PATH" go generate ./...

Following 3 files are auto generated ones with above command

api/test_api.pb.go
api/test_api.pb.gw.go
api/test_api_grpc.pb.go

Build and Run Docker

docker compose build

docker compose up

Instructions for Developers

How to add a new service & test?

This section demonstrates, how to add a new grpc gateway service, implement and test it in local.

Note: Follow naming standards from here: https://cloud.google.com/apis/design/naming_convention#method_names

####1. Add new proto changes Lets add a test api called SayHello to pkg/api/test_api.proto

// SayHello service generates a greeting message
rpc SayHello(SayHelloRequest) returns (SayHelloResponse) {
 option (google.api.http) = {
   post: "/v1/greet"
   body: "*"
 };
}

Add request SayHelloRequest & response SayHelloResponse objects declared above

// Request Message for greeting service
message SayHelloRequest {
string name = 1;
}

// Response Message for greeting service
message SayHelloResponse {
string msg = 1;
}

####2. generate protobufs

cd pkg/api

go generate ./...

(or) alternative try below in case you did not set GOPATH

env PATH="$HOME/go/bin:$PATH"

####3. Implement service handler

func (s *Server) SayHello(ctx context.Context, request *api.SayHelloRequest) (*api.SayHelloResponse, error) {

	s.Log.Info("Hit to SayHello service and Request=%s", request.Name)
	response := &api.SayHelloResponse{Msg: "Hello " + request.Name + "!!"}
	return response, nil
}

####4. Build, Deploy and Test

docker compose build

docker compose up
curl -X POST -d '{"name": "John Doe" }' -H 'Content-Type: application/json' 
http://localhost:9091/v1/greet

Response:

{"msg": "Hello John Doe!!"}
Similar Resources

Json to rpc example with envoy, go, grpc, nats

grpc-nats-envoy json to rpc example with envoy, go, grpc, redis This repo is a mirror of https://github.com/charlesonunze/grpc-redis-envoy-example It

Dec 7, 2021

The Go client to access APIs on the Micro Platform

This is the Go client to access APIs on the Micro Platform

Nov 1, 2022

An example go service that is being tested by nonnative.

Nonnative Example An example go service that is being tested by nonnative. Structure The project follows the structure in golang-standards/project-lay

Oct 31, 2021

The included device-simple example device service demonstrates basic usage of device-sdk-go

device-simple The included device-simple example device service demonstrates basic usage of device-sdk-go. Protocol Driver To make a functional Device

Oct 9, 2021

A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.

A Realtime API Gateway used with NATS to build REST, real time, and RPC APIs, where all your clients are synchronized seamlessly.

Realtime API Gateway Synchronize Your Clients Visit Resgate.io for guides, live demos, and resources. Resgate is a Go project implementing a realtime

Dec 31, 2022

Super fault-tolerant gateway for HTTP clusters, written in Go. White paper for reference - https://github.com/gptankit/serviceq-paper

Super fault-tolerant gateway for HTTP clusters, written in Go. White paper for reference - https://github.com/gptankit/serviceq-paper

ServiceQ ServiceQ is a fault-tolerant gateway for HTTP clusters. It employs probabilistic routing to distribute load during partial cluster shutdown (

Jul 16, 2022

Crank4go API Gateway Brief Introduction It is a Golang implementation of Crank4j

Crank4go API Gateway Brief Introduction It is a Golang implementation of Crank4j

Crank4go API Gateway Brief Introduction It is a Golang implementation of Crank4j, which derived from Cranker. the follow introduction is quoted from t

Dec 23, 2022

Internet connectivity for your VPC-attached Lambda functions without a NAT Gateway

Internet connectivity for your VPC-attached Lambda functions without a NAT Gateway

lambdaeip Internet connectivity for your VPC-attached Lambda functions without a NAT Gateway Background I occasionally have serverless applications th

Nov 9, 2022

Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Drop-in replacement for Go net/http when running in AWS Lambda & API Gateway

Package gateway provides a drop-in replacement for net/http's ListenAndServe for use in AWS Lambda & API Gateway, simply swap it out for gateway.Liste

Nov 24, 2022
Grpc-gateway-map-null - gRPC Gateway test using nullable values in map

Demonstrate gRPC gateway behavior with nullable values in maps Using grpc-gatewa

Jan 6, 2022
The Durudex gateway combines all durudex services so that it can be used through a single gateway.

The Durudex gateway combines all durudex services so that it can be used through a single gateway.

Dec 13, 2022
Sagadt - gRPC based micro services POC

Sagadt gRPC based micro service POC development Tools gRPC gRPC gateway buf Prot

Jan 4, 2022
Raft-grpc-demo - Some example code for how to use Hashicorp's Raft implementation with gRPC

raft-grpc-example This is some example code for how to use Hashicorp's Raft impl

Jan 4, 2022
golang grpc+go-micro.v2 wraper

gomicro golang grpc+go-micro.v2 wrapper install protoc and golang plugins binaries https://github.com/civet148/protoc-plugins install protoc from sour

Dec 20, 2021
Example GRPC service

Simple GRPC/REST API as a Knative Service. This repository contains a simple GRPC/REST API, which can be run as a Knative service. Local To run the se

May 26, 2022
Simple installation script for grpc-gateway

Grpc-Gateway-installation Grpc-gateway is a plugin of Google protocol buffer compiler, it reads the definitions from .proto file and generates a rever

Dec 16, 2021
GRPC - Creating a gRPC service from scratch

#Go gRPC services course Creating a gRPC service from scratch Command line colle

Jan 2, 2022
Service that calls uzma24/project1 service, takes input from .txt file and prints JSON output returned from the service.

Service that calls uzma24/project1 service, takes input from .txt file and prints JSON output returned from the service. Program can take large input files.

Feb 6, 2022
A service registry and service discovery implemention for kitex based on etcd

kitex etcd Introduction kitexetcd is an implemention of service registry and service discovery for kitex based on etcd. Installation go get -u github.

Feb 18, 2022