# README
gmicro
Golang grpc micro library.
Microservice prototype with gRPC + http +h2c+ gRPC gateway + logger + prometheus.
Require Go version >= v1.13.
Reference project:https://github.com/dakalab/micro
supported features
Golang grpc microservice scaffolding mainly encapsulates some components of grpc,
log tracking, link tracking, traffic h2c conversion, service monitoring prometheus
and other functions,as far as possible to maintain the kiss principle,
so that these components are pluggable of.
The framework supports 2 different methods such as http api, grpc server pb.
The protocol called by the client can be http or grpc pb format.
At the same time,it supports the same port, while providing
http services (supporting http1.x protocol requests) and the processing
capabilities of grpc server.
go version
if you use go version < 1.16,please use gmicro tag v1.1.1
else use gmicro tag v1.2.0 higher version.
installation
go get -v github.com/go-god/gmicro
quick start
please see example demo
grpc-go
https://github.com/grpc/grpc-go
grpc-gateway doc
https://grpc-ecosystem.github.io/grpc-gateway/
gmicro action
https://github.com/daheige/goapp
https://github.com/daheige/gmicro-demo
Old projects can continue to use the v1 version https://github.com/daheige/gmicro, and new projects can start to use the v2 version. Note that the gmicro address has changed, and the v2 version is https://github.com/go-god/gmicro
change log
options | desc | time |
---|---|---|
go mod update | update grpc mod | 2023-06-04 |
grpc metadata | fix request md for RequestInterceptor | 2023-06-04 |
add x-request-id | fix request x-request-id from md | 2023-06-04 |
options | desc | time |
---|---|---|
grpc tools | update docs | 2022-05-15 |
grpc dockerfile | update go grpc tools | 2022-05-15 |
options | desc | time |
---|---|---|
grpc gateway | upgrade grpc gateway to v2.10.0 | 2022-05-11 |
grpc | upgrade go grpc to v1.46.0 | 2022-05-11 |
protobuf | upgrade protobuf to v1.28.0 | 2022-05-11 |
grpc dockerfile | upgrade go to v1.16.15 and protoc v3.15.8 | 2022-05-11 |
options | desc | time |
---|---|---|
grpc gateway | upgrade grpc gateway to v2.4.0 | 2021-06-19 |
grpc | upgrade go grpc to v1.38.0 | 2021-06-19 |
protobuf | upgrade protobuf to v1.26.0 | 2021-06-19 |
grpc tools dockerfile
https://github.com/daheige/gmicro-grpc-tools
grpc tools
please do the following or see example/grpc_tools.sh
# go gRPC tools
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
#go validator
go get github.com/go-playground/validator/v10
#This will place four binaries in your $GOBIN;
# protoc-gen-grpc-gateway
# protoc-gen-openapiv2
# protoc-gen-go
# protoc-gen-go-grpc
# protoc inject tag
go install github.com/favadi/protoc-go-inject-tag
Google APIs
# googleapis link:https://github.com/googleapis/googleapis
api/rpc link: https://github.com/googleapis/googleapis/tree/master/google
============
Project: Google APIs
URL: https://github.com/google/googleapis
Revision: 3544ab16c3342d790b00764251e348705991ea4b
License: Apache License 2.0
Imported Files
---------------
- google/api/annotations.proto
- google/api/http.proto
- google/api/httpbody.proto
Generated Files
----------------
They are generated from the .proto files by protoc-gen-go.
- google/api/annotations.pb.go
- google/api/http.pb.go
License
MIT
# Packages
No description provided by the author
# Functions
AllPattern returns a pattern which matches any url.
DefaultHTTPHandler is the default http handler which does nothing.
GetCtxValue returns ctx when you set key/value into ctx.
GetGRPCClientIP get client ip address from context.
GetIncomingMD returns metadata.MD from incoming ctx get request metadata this method is mainly used at the server end to get the relevant metadata data.
GetOutgoingMD returns metadata.MD from outgoing ctx Use this method when you pass ctx to a downstream service.
GetSliceFromMD returns []string from md.
GetSliceFromMD returns string from md.
GRPCHandlerFunc uses the standard library h2c to convert http requests to http2 In this way, you can co-exist with go grpc and http services, and use one port to provide both grpc services and http services.
Md5 md5 func.
NewService creates a new microservice.
NewServiceWithoutGateway new a service without http gw.
RandInt64 crete a num [m,n].
RndUUID realizes unique uuid based on time ns and random number There is no duplication of uuid on a single machine If you want to generate non-duplicate uuid on a distributed architecture Just add some custom strings in front of rndStr Return format: eba1e8cd-0460-4910-49c6-44bdf3cf024d.
RndUUIDMd5 make an uuid.
SetCtxValue returns ctx when you set key/value into ctx.
Uuid uuid of version4 eg:eba1e8cd0460491049c644bdf3cf024d.
WithAnnotator returns an Option to append some annotator.
WithErrorHandler returns an Option to set the errorHandler.
WithGRPCDialOption returns an Option to append a gRPC dial option.
WithGRPCNetwork set gRPC start network type.
WithGRPCServerOption returns an Option to append a gRPC server option.
WithHandlerFromEndpoint add handlerFromEndpoint to http gw endPoint.
WithHTTPHandler returns an Option to set the httpHandler.
WithHTTPServer returns an Option to set the http server, note that the Addr and Handler will be reset in startGRPCGateway(), so you are not able to specify them.
WithInterruptSignal returns an Option to append a interrupt signal.
WithLogger uses the provided logger.
WithMuxOption returns an Option to append a mux option.
WithPreShutdownDelay returns an Option to set the time waiting for running goroutines to finish their jobs before the shutdown starts.
WithPrometheus enble prometheus config.
WithRateLimit rate limit.
WithRecovery service recover func.
WithRequestAccess request access log config.
WithRouteOpt adds additional routes.
WithShutdownFunc returns an Option to register a function which will be called when server shutdown.
WithShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly.
WithStaticAccess enable static file access.
WithStaticDir returns an Option to set the staticDir.
WithStreamInterceptor returns an Option to append some streamInterceptor.
WithUnaryInterceptor returns an Option to append some unaryInterceptor.
# Constants
GRPCClientIP grpc client_ip.
RequestMethod request method.
RequestURI request uri.
XRequestID request_id.
# Variables
ErrInvokeGRPCClientIP get grpc request client ip fail.
ErrPeerAddressNil gRPC peer address is nil.
InterruptSignals interrupt signals.
# Type aliases
AnnotatorFunc is the annotator function is for injecting metadata from http request into gRPC context.
CtxKey ctx key type.
HandlerFromEndpoint is the callback that the caller should implement to steps to reverse-proxy the HTTP/1 requests to gRPC handlerFromEndpoint http gw endPoint automatically dials to "endpoint" and closes the connection when "ctx" gets done.
HTTPHandlerFunc is the http middleware handler function.
LoggerFunc is a bridge between Logger and any third party logger.
Option is service functional option See this post about the "functional options" pattern: http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis.