Documentation
¶
Index ¶
- Variables
- func CloseGRPCConnections()
- func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler
- func GetCtxValue(ctx context.Context, key ctxkeys.CtxKey) interface{}
- func GetGRPCClientIP(ctx context.Context) (string, error)
- func GetRPCRequestID(ctx context.Context) string
- func GetSliceFromMD(md metadata.MD, key ctxkeys.CtxKey) []string
- func GetStringFromMD(md metadata.MD, key ctxkeys.CtxKey) string
- func IncomingMD(ctx context.Context) metadata.MD
- func NewContext(ctx context.Context) context.Context
- func NewRPCContext(ctx context.Context, m ...map[string]string) context.Context
- func NewTimeoutContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- func OutgoingMD(ctx context.Context) metadata.MD
- func RegisterGRPCConnections(key string, conn *grpc.ClientConn)
- func SetCtxValue(ctx context.Context, key ctxkeys.CtxKey, val interface{}) context.Context
- func SetRPCRequestID(ctx context.Context) metadata.MD
- type AnnotatorFunc
- type HTTPHandlerFunc
- type HandlerFromEndpoint
- type Logger
- type LoggerFunc
- type Option
- func WithEnableGRPCShareAddress() Option
- func WithEnableHTTPGateway() Option
- func WithEnablePrometheus() Option
- func WithEnableRequestAccess() Option
- func WithEnableRequestValidator() Option
- func WithGRPCEndpointDialOptions(dialOption ...grpc.DialOption) Option
- func WithGRPCHTTPAddress(addr string) Option
- func WithGRPCHTTPErrorHandler(errorHandler gRuntime.ErrorHandlerFunc) Option
- func WithGRPCHTTPHandler(h HTTPHandlerFunc) Option
- func WithGRPCHTTPServer(server *http.Server) Option
- func WithGRPCNetwork(network string) Option
- func WithGRPCServerOption(serverOption ...grpc.ServerOption) Option
- func WithHandlerFromEndpoints(h ...HandlerFromEndpoint) Option
- func WithInterruptSignals(signal ...os.Signal) Option
- func WithLogger(logger Logger) Option
- func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option
- func WithRecovery(f func()) Option
- func WithRoutes(routes ...Route) Option
- func WithServerMetricsOptions(opts ...gPrometheus.ServerMetricsOption) Option
- func WithShutdownFunc(f func()) Option
- func WithShutdownTimeout(timeout time.Duration) Option
- func WithStreamInterceptor(streamInterceptor ...grpc.StreamServerInterceptor) Option
- func WithUnaryInterceptor(unaryInterceptor ...grpc.UnaryServerInterceptor) Option
- type Route
- type Service
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") )
Functions ¶
func CloseGRPCConnections ¶ added in v1.3.2
func CloseGRPCConnections()
CloseGRPCConnections When the program exits, after the grpc client call is completed, it is generally necessary to close it in the main method of the main.go file. It needs to be called use defer micro.CloseGRPCConnections() method.
func GRPCHandlerFunc ¶ added in v1.2.0
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. If a request is a h2c connection, it's hijacked and redirected to s.ServeConn. Otherwise the returned Handler just forwards requests to http.
func GetCtxValue ¶
GetCtxValue returns ctx when you set key/value into ctx
func GetGRPCClientIP ¶
GetGRPCClientIP get client ip address from context
func GetRPCRequestID ¶ added in v1.3.2
GetRPCRequestID returns request-id from grpc request metadata.FromIncomingContext
func GetSliceFromMD ¶
GetSliceFromMD returns []string from md
func GetStringFromMD ¶
GetStringFromMD returns string from md
func IncomingMD ¶
IncomingMD 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 NewContext ¶ added in v1.3.2
NewContext create a new context from request,eg:http request
func NewRPCContext ¶ added in v1.3.2
NewRPCContext new a context for grpc client call
func NewTimeoutContext ¶ added in v1.3.2
func NewTimeoutContext(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
NewTimeoutContext create a new context from request,eg:http request
func OutgoingMD ¶
OutgoingMD returns metadata.MD from outgoing ctx Use this method when you pass ctx to a downstream service
func RegisterGRPCConnections ¶ added in v1.3.2
func RegisterGRPCConnections(key string, conn *grpc.ClientConn)
RegisterGRPCConnections register grpc client connection If there are multiple grpc client calls in a project, it is recommended to manage them uniformly. Before the main function of the program exits, all of them can be closed by calling the defer micro.CloseGRPCConnections() method, thereby releasing the grpc connections.
func SetCtxValue ¶
SetCtxValue returns ctx when you set key/value into ctx
Types ¶
type AnnotatorFunc ¶ added in v1.2.0
AnnotatorFunc is the annotator function is for injecting metadata from http request into gRPC context
type HTTPHandlerFunc ¶ added in v1.2.0
HTTPHandlerFunc is the http middleware handler function.
type HandlerFromEndpoint ¶ added in v1.2.0
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 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 for grpc service option
func WithEnableGRPCShareAddress ¶ added in v1.2.0
func WithEnableGRPCShareAddress() Option
WithEnableGRPCShareAddress returns an Option to set the enableGRPCShareAddress
func WithEnableHTTPGateway ¶ added in v1.2.0
func WithEnableHTTPGateway() Option
WithEnableHTTPGateway enable http gateway
func WithEnablePrometheus ¶
func WithEnablePrometheus() Option
WithEnablePrometheus enable prometheus
func WithEnableRequestAccess ¶
func WithEnableRequestAccess() Option
WithEnableRequestAccess request access log config
func WithEnableRequestValidator ¶
func WithEnableRequestValidator() Option
WithEnableRequestValidator set request validator interceptor
func WithGRPCEndpointDialOptions ¶ added in v1.2.0
func WithGRPCEndpointDialOptions(dialOption ...grpc.DialOption) Option
WithGRPCEndpointDialOptions returns an Option to append a gRPC dial option
func WithGRPCHTTPAddress ¶ added in v1.2.0
WithGRPCHTTPAddress set gRPC HTTP Address,eg: 0.0.0.0:8080
func WithGRPCHTTPErrorHandler ¶ added in v1.2.0
func WithGRPCHTTPErrorHandler(errorHandler gRuntime.ErrorHandlerFunc) Option
WithGRPCHTTPErrorHandler returns an Option to set the errorHandler
func WithGRPCHTTPHandler ¶ added in v1.2.0
func WithGRPCHTTPHandler(h HTTPHandlerFunc) Option
WithGRPCHTTPHandler returns an Option to set gRPC HTTP Server Handler Customizing Gin/chi or gorilla/mux route as grpc gateway http handler
grpcHTTPHandler := micro.WithGRPCHTTPHandler(func(mux *runtime.ServeMux) http.Handler {
// Customizing Gin route
router := initHttpRouter()
// If the route address defined in the proto file cannot be found,
// the route rules defined by gin http will be followed.
router.NoRoute(func(c *gin.Context) {
mux.ServeHTTP(c.Writer, c.Request)
})
return router
})
func WithGRPCHTTPServer ¶ added in v1.2.0
WithGRPCHTTPServer returns an Option to set the http server
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 WithHandlerFromEndpoints ¶ added in v1.2.0
func WithHandlerFromEndpoints(h ...HandlerFromEndpoint) Option
WithHandlerFromEndpoints add HandlerFromEndpoint
func WithInterruptSignals ¶
WithInterruptSignals returns an Option to append a interrupt signal
func WithMuxOption ¶ added in v1.2.0
func WithMuxOption(muxOption ...gRuntime.ServeMuxOption) Option
WithMuxOption returns an Option to append a mux option
func WithRoutes ¶ added in v1.2.0
WithRoutes adds additional routes
func WithServerMetricsOptions ¶ added in v1.3.0
func WithServerMetricsOptions(opts ...gPrometheus.ServerMetricsOption) Option
WithServerMetricsOptions set prometheus server metrics options
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 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 ¶ added in v1.2.0
type Route struct {
Method string
Path string
Handler http.HandlerFunc
}
Route represents the route for mux