gmicro

package module
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2022 License: MIT Imports: 31 Imported by: 2

README

gmicro

Golang grpc micro library.
Microservice prototype with gRPC + http +h2c+ 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.

gmicro action

see example or see https://github.com/daheige/goapp
github.com/daheige/gmicro-demo

upgrade go grpc tools

# go gRPC tools
go get -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -v github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -v github.com/golang/protobuf/proto

go get -v google.golang.org/protobuf/cmd/protoc-gen-go

# if you can't use it, please use go get below
# go get -v github.com/golang/protobuf/protoc-gen-go

go get -v google.golang.org/grpc/cmd/protoc-gen-go-grpc

# go validator
go get -v github.com/go-playground/validator/v10

# after successful execution, 3 binary files will be generated under the $GOBIN directory.
# protoc-gen-grpc-gateway
# protoc-gen-grpc-swagger
# protoc-gen-go
# google api link: github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis

# protoc inject tag
go get -v github.com/favadi/protoc-go-inject-tag  

Google APIs

https://github.com/google/googleapis

============

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

Documentation

Overview

Package gmicro Grpc Microservices components. Copyright 2020 daheige. All rights reserved. Use of this source code is governed by a MIT style license that can be found in the LICENSE file.

Package gmicro Grpc Microservices components.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvokeGRPCClientIP get grpc request client ip fail.
	ErrInvokeGRPCClientIP = errors.New("invoke from context failed")

	// ErrPeerAddressNil gRPC peer address is nil.
	ErrPeerAddressNil = errors.New("peer address is nil")
)

InterruptSignals interrupt signals.

Functions

func AllPattern

func AllPattern() gRuntime.Pattern

AllPattern returns a pattern which matches any url

func DefaultHTTPHandler

func DefaultHTTPHandler(mux *gRuntime.ServeMux) http.Handler

DefaultHTTPHandler is the default http handler which does nothing.

func GRPCHandlerFunc

func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler

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. In June 2018, the golang.org/x/net/http2/h2c standard library representing the "h2c" logo was officially merged in, and since then we can use the official standard library (h2c) This standard library implements the unencrypted mode of HTTP/2, so we can use the standard library to provide both HTTP/1.1 and HTTP/2 functions on the same port The h2c.NewHandler method has been specially processed, and h2c.NewHandler will return an http.handler The main internal logic is to intercept all h2c traffic, then hijack and redirect it to the corresponding Hander according to different request traffic types to process

func GetGRPCClientIP

func GetGRPCClientIP(ctx context.Context) (string, error)

GetGRPCClientIP get client ip address from context

func Md5

func Md5(str string) string

Md5 md5 func

func PathPattern

func PathPattern(path string) gRuntime.Pattern

PathPattern returns a pattern which matches exactly with the path

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 crete a num [m,n]

func RndUUID

func RndUUID() string

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

func RndUUIDMd5

func RndUUIDMd5() string

RndUUIDMd5 make an uuid

func WithRateLimit

func WithRateLimit(limiter Limiter) grpc.UnaryServerInterceptor

WithRateLimit rate limit

Types

type AnnotatorFunc

type AnnotatorFunc func(context.Context, *http.Request) metadata.MD

AnnotatorFunc is the annotator function is for injecting meta data from http request into gRPC context

type CtxKey

type CtxKey string

CtxKey ctx key type.

const (
	// XRequestID request_id
	XRequestID CtxKey = "x-request-id"

	// GRPCClientIP grpc client_ip
	GRPCClientIP CtxKey = "grpc_client_ip"

	// RequestMethod request method
	RequestMethod CtxKey = "request_method"

	// RequestURI request uri
	RequestURI CtxKey = "request_uri"
)

type HTTPHandlerFunc

type HTTPHandlerFunc func(*gRuntime.ServeMux) http.Handler

HTTPHandlerFunc is the http middleware handler function.

type HandlerFromEndpoint

type HandlerFromEndpoint func(ctx context.Context, mux *gRuntime.ServeMux,
	grpcAddressAndPort string, opts []grpc.DialOption) error

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.

type Limiter

type Limiter interface {
	Limit() bool
}

Limiter defines the interface to perform request rate limiting. If Limit function return true, the request will be rejected. Otherwise, the request will pass.

type Logger

type Logger interface {
	Printf(string, ...interface{})
}

Logger is logger interface.

type LoggerFunc

type LoggerFunc func(string, ...interface{})

LoggerFunc is a bridge between Logger and any third party logger.

func (LoggerFunc) Printf

func (f LoggerFunc) Printf(msg string, args ...interface{})

Printf implements Logger interface.

type Option

type Option func(s *Service)

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

func WithAnnotator

func WithAnnotator(annotator ...AnnotatorFunc) Option

WithAnnotator returns an Option to append some annotator

func WithErrorHandler

func WithErrorHandler(errorHandler gRuntime.ProtoErrorHandlerFunc) Option

WithErrorHandler returns an Option to set the errorHandler

func WithGRPCDialOption

func WithGRPCDialOption(dialOption ...grpc.DialOption) Option

WithGRPCDialOption returns an Option to append a gRPC dial option

func WithGRPCNetwork added in v1.2.0

func WithGRPCNetwork(network string) Option

WithGRPCNetwork set gRPC start network type.

func WithGRPCServerOption

func WithGRPCServerOption(serverOption ...grpc.ServerOption) Option

WithGRPCServerOption returns an Option to append a gRPC server option

func WithHTTPHandler

func WithHTTPHandler(h HTTPHandlerFunc) Option

WithHTTPHandler returns an Option to set the httpHandler

func WithHTTPServer

func WithHTTPServer(server *http.Server) Option

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

func WithHandlerFromEndpoint

func WithHandlerFromEndpoint(reverseProxyFunc ...HandlerFromEndpoint) Option

WithHandlerFromEndpoint add handlerFromEndpoint to http gw endPoint

func WithInterruptSignal

func WithInterruptSignal(signal os.Signal) Option

WithInterruptSignal returns an Option to append a interrupt signal

func WithLogger

func WithLogger(logger Logger) Option

WithLogger uses the provided logger

func WithMuxOption

func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option

WithMuxOption returns an Option to append a mux option

func WithPreShutdownDelay

func WithPreShutdownDelay(timeout time.Duration) Option

WithPreShutdownDelay returns an Option to set the time waiting for running goroutines to finish their jobs before the shutdown starts

func WithPrometheus

func WithPrometheus(b bool) Option

WithPrometheus enble prometheus config.

func WithRecovery

func WithRecovery(f func()) Option

WithRecovery service recover func.

func WithRequestAccess

func WithRequestAccess(b bool) Option

WithRequestAccess request access log config.

func WithRouteOpt

func WithRouteOpt(routes ...Route) Option

WithRouteOpt adds additional routes

func WithShutdownFunc

func WithShutdownFunc(f func()) Option

WithShutdownFunc returns an Option to register a function which will be called when server shutdown

func WithShutdownTimeout

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly

func WithStaticAccess added in v1.2.3

func WithStaticAccess(b bool) Option

WithStaticAccess enable static file access

func WithStaticDir

func WithStaticDir(dir string) Option

WithStaticDir returns an Option to set the staticDir

func WithStreamInterceptor

func WithStreamInterceptor(streamInterceptor ...grpc.StreamServerInterceptor) Option

WithStreamInterceptor returns an Option to append some streamInterceptor

func WithUnaryInterceptor

func WithUnaryInterceptor(unaryInterceptor ...grpc.UnaryServerInterceptor) Option

WithUnaryInterceptor returns an Option to append some unaryInterceptor

type Route

type Route struct {
	Method  string
	Pattern gRuntime.Pattern
	Handler gRuntime.HandlerFunc
}

Route represents the route for mux

type Service

type Service struct {
	GRPCServer *grpc.Server // gRPC server
	HTTPServer *http.Server // if you need gRPC gw,please use it
	// contains filtered or unexported fields
}

Service represents the microservice.

func NewService

func NewService(opts ...Option) *Service

NewService creates a new microservice

func NewServiceWithoutGateway

func NewServiceWithoutGateway(opts ...Option) *Service

NewServiceWithoutGateway new a service without http gw.

func (*Service) AddHandlerFromEndpoint

func (s *Service) AddHandlerFromEndpoint(h ...HandlerFromEndpoint)

AddHandlerFromEndpoint add HandlerFromEndpoint.

func (*Service) AddRoute

func (s *Service) AddRoute(routes ...Route)

AddRoute add some route to routes

func (*Service) GetPid

func (s *Service) GetPid() int

GetPid gets the process id of server

func (*Service) RequestInterceptor

func (s *Service) RequestInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler) (res interface{}, err error)

RequestInterceptor request interceptor to record basic information of the request

func (*Service) ServeFile added in v1.2.5

func (s *Service) ServeFile(w http.ResponseWriter, r *http.Request, _ map[string]string)

ServeFile serves a file if file does not exist, then a 404 error will be returned.

func (*Service) Start

func (s *Service) Start(httpPort int, grpcPort int) error

Start starts the microservice with listening on the ports start grpc gateway and http server on different port

func (*Service) StartGRPCAndHTTPServer

func (s *Service) StartGRPCAndHTTPServer(port int) error

StartGRPCAndHTTPServer grpc server and grpc gateway port share a port error: rpc error: code = Unavailable desc = all SubConns are in TransientFailure, latest connection error: timed out waiting for server handshake please set this gRPC var. export GRPC_GO_REQUIRE_HANDSHAKE=off

func (*Service) StartGRPCWithoutGateway

func (s *Service) StartGRPCWithoutGateway(grpcPort int) error

StartGRPCWithoutGateway start gRPC without gw.

func (*Service) Stop

func (s *Service) Stop()

Stop stops the microservice gracefully.

func (*Service) StopGRPCWithoutGateway

func (s *Service) StopGRPCWithoutGateway()

StopGRPCWithoutGateway stop the gRPC server gracefully

Directories

Path Synopsis
example
clients/go/pb
Package pb is a reverse proxy.
Package pb is a reverse proxy.
pb
Package pb is a reverse proxy.
Package pb is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL