gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-grpc-middleware/README.md (about)

     1  # Go gRPC Middleware
     2  
     3  [![Travis Build](https://travis-ci.org/grpc-ecosystem/go-grpc-middleware.svg?branch=master)](https://travis-ci.org/grpc-ecosystem/go-grpc-middleware)
     4  [![Go Report Card](https://goreportcard.com/badge/github.com/grpc-ecosystem/go-grpc-middleware)](https://goreportcard.com/report/github.com/grpc-ecosystem/go-grpc-middleware)
     5  [![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware)
     6  [![SourceGraph](https://sourcegraph.com/github.com/grpc-ecosystem/go-grpc-middleware/-/badge.svg)](https://sourcegraph.com/github.com/grpc-ecosystem/go-grpc-middleware/?badge)
     7  [![codecov](https://codecov.io/gh/grpc-ecosystem/go-grpc-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/grpc-ecosystem/go-grpc-middleware)
     8  [![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
     9  [![quality: production](https://img.shields.io/badge/quality-production-orange.svg)](#status)
    10  [![Slack](slack.png)](https://join.slack.com/t/improbable-eng/shared_invite/enQtMzQ1ODcyMzQ5MjM4LWY5ZWZmNGM2ODc5MmViNmQ3ZTA3ZTY3NzQwOTBlMTkzZmIxZTIxODk0OWU3YjZhNWVlNDU3MDlkZGViZjhkMjc)
    11  
    12  [gRPC Go](https://github.com/grpc/grpc-go) Middleware: interceptors, helpers, utilities.
    13  
    14  ## Middleware
    15  
    16  [gRPC Go](https://github.com/grpc/grpc-go) recently acquired support for
    17  Interceptors, i.e. [middleware](https://medium.com/@matryer/writing-middleware-in-golang-and-how-go-makes-it-so-much-fun-4375c1246e81#.gv7tdlghs) 
    18  that is executed either on the gRPC Server before the request is passed onto the user's application logic, or on the gRPC client either around the user call. It is a perfect way to implement
    19  common patterns: auth, logging, message, validation, retries or monitoring.
    20  
    21  These are generic building blocks that make it easy to build multiple microservices easily.
    22  The purpose of this repository is to act as a go-to point for such reusable functionality. It contains
    23  some of them itself, but also will link to useful external repos.
    24  
    25  `grpc_middleware` itself provides support for chaining interceptors, here's an example:
    26  
    27  ```go
    28  import "gitee.com/ks-custle/core-gm/go-grpc-middleware"
    29  
    30  myServer := grpc.NewServer(
    31      grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
    32          grpc_ctxtags.StreamServerInterceptor(),
    33          grpc_opentracing.StreamServerInterceptor(),
    34          grpc_prometheus.StreamServerInterceptor,
    35          grpc_zap.StreamServerInterceptor(zapLogger),
    36          grpc_auth.StreamServerInterceptor(myAuthFunction),
    37          grpc_recovery.StreamServerInterceptor(),
    38      )),
    39      grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
    40          grpc_ctxtags.UnaryServerInterceptor(),
    41          grpc_opentracing.UnaryServerInterceptor(),
    42          grpc_prometheus.UnaryServerInterceptor,
    43          grpc_zap.UnaryServerInterceptor(zapLogger),
    44          grpc_auth.UnaryServerInterceptor(myAuthFunction),
    45          grpc_recovery.UnaryServerInterceptor(),
    46      )),
    47  )
    48  ```
    49  
    50  ## Interceptors
    51  
    52  *Please send a PR to add new interceptors or middleware to this list*
    53  
    54  #### Auth
    55     * [`grpc_auth`](auth) - a customizable (via `AuthFunc`) piece of auth middleware 
    56  
    57  #### Logging
    58     * [`grpc_ctxtags`](tags/) - a library that adds a `Tag` map to context, with data populated from request body
    59     * [`grpc_zap`](logging/zap/) - integration of [zap](https://github.com/uber-go/zap) logging library into gRPC handlers.
    60     * [`grpc_logrus`](logging/logrus/) - integration of [logrus](https://github.com/sirupsen/logrus) logging library into gRPC handlers.
    61  
    62  
    63  #### Monitoring
    64     * [`grpc_prometheus`⚡](https://github.com/grpc-ecosystem/go-grpc-prometheus) - Prometheus client-side and server-side monitoring middleware
    65     * [`otgrpc`⚡](https://github.com/grpc-ecosystem/grpc-opentracing/tree/master/go/otgrpc) - [OpenTracing](http://opentracing.io/) client-side and server-side interceptors
    66     * [`grpc_opentracing`](tracing/opentracing) - [OpenTracing](http://opentracing.io/) client-side and server-side interceptors with support for streaming and handler-returned tags
    67  
    68  #### Client
    69     * [`grpc_retry`](retry/) - a generic gRPC response code retry mechanism, client-side middleware
    70  
    71  #### Server
    72     * [`grpc_validator`](validator/) - codegen inbound message validation from `.proto` options
    73     * [`grpc_recovery`](recovery/) - turn panics into gRPC errors
    74  
    75  
    76  ## Status
    77  
    78  This code has been running in *production* since May 2016 as the basis of the gRPC micro services stack at [Improbable](https://improbable.io).
    79  
    80  Additional tooling will be added, and contributions are welcome.
    81  
    82  ## License
    83  
    84  `go-grpc-middleware` is released under the Apache 2.0 license. See the [LICENSE](LICENSE) file for details.