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 ¶
- Variables
- func AllPattern() gRuntime.Pattern
- func DefaultHTTPHandler(mux *gRuntime.ServeMux) http.Handler
- func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler
- func GetCtxValue(ctx context.Context, key CtxKey) interface{}
- func GetGRPCClientIP(ctx context.Context) (string, error)
- func GetIncomingMD(ctx context.Context) metadata.MD
- func GetOutgoingMD(ctx context.Context) metadata.MD
- func GetSliceFromMD(md metadata.MD, key CtxKey) []string
- func GetStringFromMD(md metadata.MD, key CtxKey) string
- func Md5(str string) string
- func RandInt64(min, max int64) int64
- func RndUUID() string
- func RndUUIDMd5() string
- func SetCtxValue(ctx context.Context, key CtxKey, val interface{}) context.Context
- func Uuid() string
- func WithRateLimit(limiter Limiter) grpc.UnaryServerInterceptor
- type AnnotatorFunc
- type CtxKey
- type HTTPHandlerFunc
- type HandlerFromEndpoint
- type Limiter
- type Logger
- type LoggerFunc
- type Option
- func WithAnnotator(annotator ...AnnotatorFunc) Option
- func WithErrorHandler(errorHandler gRuntime.ErrorHandlerFunc) Option
- func WithGRPCDialOption(dialOption ...grpc.DialOption) Option
- func WithGRPCNetwork(network string) Option
- func WithGRPCServerOption(serverOption ...grpc.ServerOption) Option
- func WithHTTPHandler(h HTTPHandlerFunc) Option
- func WithHTTPServer(server *http.Server) Option
- func WithHandlerFromEndpoint(reverseProxyFunc ...HandlerFromEndpoint) Option
- func WithInterruptSignal(signal os.Signal) Option
- func WithLogger(logger Logger) Option
- func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option
- func WithPreShutdownDelay(timeout time.Duration) Option
- func WithPrometheus(b bool) Option
- func WithRecovery(f func()) Option
- func WithRequestAccess(b bool) Option
- func WithRouteOpt(routes ...Route) Option
- func WithShutdownFunc(f func()) Option
- func WithShutdownTimeout(timeout time.Duration) Option
- func WithStaticAccess(b bool) Option
- func WithStaticDir(dir string) Option
- func WithStreamInterceptor(streamInterceptor ...grpc.StreamServerInterceptor) Option
- func WithUnaryInterceptor(unaryInterceptor ...grpc.UnaryServerInterceptor) Option
- type Route
- type Service
- func (s *Service) AddHandlerFromEndpoint(h ...HandlerFromEndpoint)
- func (s *Service) AddRoute(routes ...Route)
- func (s *Service) GetPid() int
- func (s *Service) RequestInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...) (reply interface{}, err error)
- func (s *Service) ServeFile(w http.ResponseWriter, r *http.Request, _ map[string]string)
- func (s *Service) Start(httpPort, grpcPort int) error
- func (s *Service) StartGRPCAndHTTPServer(port int) error
- func (s *Service) StartGRPCWithoutGateway(grpcPort int) error
- func (s *Service) Stop()
- func (s *Service) StopGRPCWithoutGateway()
Constants ¶
This section is empty.
Variables ¶
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") )
var InterruptSignals = []os.Signal{ syscall.SIGINT, syscall.SIGTERM, os.Interrupt, syscall.SIGHUP, syscall.SIGSTOP, syscall.SIGQUIT, }
InterruptSignals interrupt signals.
Functions ¶
func AllPattern ¶
AllPattern returns a pattern which matches any url
func DefaultHTTPHandler ¶
DefaultHTTPHandler is the default http handler which does nothing.
func GRPCHandlerFunc ¶
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 handler according to different request traffic types to process
func GetCtxValue ¶ added in v2.3.0
GetCtxValue returns ctx when you set key/value into ctx
func GetGRPCClientIP ¶
GetGRPCClientIP get client ip address from context
func GetIncomingMD ¶ added in v2.3.0
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
func GetOutgoingMD ¶ added in v2.3.0
GetOutgoingMD returns metadata.MD from outgoing ctx Use this method when you pass ctx to a downstream service
func GetSliceFromMD ¶ added in v2.3.0
GetSliceFromMD returns []string from md
func GetStringFromMD ¶ added in v2.3.0
GetSliceFromMD returns string from md
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 SetCtxValue ¶ added in v2.3.0
SetCtxValue returns ctx when you set key/value into ctx
func Uuid ¶ added in v2.3.0
func Uuid() string
Uuid uuid of version4 eg:eba1e8cd0460491049c644bdf3cf024d
func WithRateLimit ¶
func WithRateLimit(limiter Limiter) grpc.UnaryServerInterceptor
WithRateLimit rate limit
Types ¶
type AnnotatorFunc ¶
AnnotatorFunc is the annotator function is for injecting metadata from http request into gRPC context
type HTTPHandlerFunc ¶
HTTPHandlerFunc is the http middleware handler function.
type HandlerFromEndpoint ¶
type HandlerFromEndpoint func(ctx context.Context, mux *gRuntime.ServeMux, endpoint 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 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.ErrorHandlerFunc) 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 ¶
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 ¶
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 ¶
WithInterruptSignal returns an Option to append a interrupt signal
func WithMuxOption ¶
func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option
WithMuxOption returns an Option to append a mux option
func WithPreShutdownDelay ¶
WithPreShutdownDelay returns an Option to set the time waiting for running goroutines to finish their jobs before the shutdown starts
func WithRequestAccess ¶
WithRequestAccess request access log config.
func WithShutdownFunc ¶
func WithShutdownFunc(f func()) Option
WithShutdownFunc returns an Option to register a function which will be called when server shutdown
func WithShutdownTimeout ¶
WithShutdownTimeout returns an Option to set the timeout before the server shutdown abruptly
func WithStaticAccess ¶
WithStaticAccess enable static file access
func WithStaticDir ¶
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 Path string 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 NewServiceWithoutGateway ¶
NewServiceWithoutGateway new a service without http gw.
func (*Service) AddHandlerFromEndpoint ¶
func (s *Service) AddHandlerFromEndpoint(h ...HandlerFromEndpoint)
AddHandlerFromEndpoint add HandlerFromEndpoint.
func (*Service) RequestInterceptor ¶
func (s *Service) RequestInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (reply interface{}, err error)
RequestInterceptor request interceptor to record basic information of the request
func (*Service) Start ¶
Start starts the microservice with listening on the ports start grpc gateway and http server on different port
func (*Service) StartGRPCAndHTTPServer ¶
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 ¶
StartGRPCWithoutGateway start gRPC without gw.
func (*Service) StopGRPCWithoutGateway ¶
func (s *Service) StopGRPCWithoutGateway()
StopGRPCWithoutGateway stop the gRPC server gracefully
Source Files ¶
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. |