Skip to main content

Middleware

Overview

Microservice middleware are software components used to support microservice architecture, they provide some basic functions and services for communication, collaboration and management between microservices, the following middleware is built in go-zero:

  • AuthorizeHandler
  • BreakerHandler
  • ContentSecurityHandler
  • CryptionHandler
  • GunzipHandler
  • LogHandler
  • MaxBytesHandler
  • MaxConnsHandler
  • MetricHandler
  • PrometheusHandler
  • RecoverHandler
  • SheddingHandler
  • TimeoutHandler
  • TraceHandler

The above intermediate is enabled by default. You can also control some of the intermediaries.

Middleware Configuration

type MiddlewaresConf struct {
Trace bool `json:",default=true"`
Log bool `json:",default=true"`
Prometheus bool `json:",default=true"`
MaxConns bool `json:",default=true"`
Breaker bool `json:",default=true"`
Shedding bool `json:",default=true"`
Timeout bool `json:",default=true"`
Recover bool `json:",default=true"`
Metrics bool `json:",default=true"`
MaxBytes bool `json:",default=true"`
Gunzip bool `json:",default=true"`
}
Configuration ItemNoteDefault value
TraceEnable link trackingtrue
LogEnable Log Middlewaretrue
PrometheusWhether to enable Prometheus middlewaretrue
MaxConnsEnable Limit Middlewaretrue
BreakerEnable breaker middlewaretrue
SheddingEnable Load Monitor Middlewaretrue
TimeoutEnable Timeout Middlewaretrue
RecoverEnable Panic Recovery Middlewaretrue
MetricsEnable metrics middlewaretrue
MaxBytesEnable ContentLength Administration Middlewaretrue
GunzipEnable compression management intermediatetrue

For example, if you want to disable indicator collection in the Hello World program, you can configure it as follows:

Name: HelloWorld.api
Host: 127.0.0.1
Port: 8080
Middlewares:
Metrics: false

Focus Middleware Introduction

TraceHandler

The framework integrates Opentelemetry by default to standardize link tracking.If you want to report the trace information to jaeger, or take the hello world program as an example, you can do the following:

Name: HelloWorld.api
Host: 127.0.0.1
Port: 8080
Middlewares:
Metrics: true
Telemetry:
Name: hello
Endpoint: http://127.0.0.1:14268/api/traces
Batcher: jaeger
Sampler: 1.0

Re-request http:///127.0.0.1:8080/helloto view link request information by opening the jaeger page and by defaulthttp.hosthttp.methodhttp.routehttp.status_codeand more.

LogHandler

默认每次 http 请求都会输出对应的请求日志,格式如下:

{
"@timestamp": "2023-03-05T21:53:44.244+08:00",
"caller": "handler/loghandler.go:160",
"content": "[HTTP] 200 - GET /hello - 127.0.0.1:51499 - Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
"duration": "0.0ms",
"level": "info"
}

Can disable log middleware by configuring it, enabled by default

Name: HelloWorld.api
Host: 127.0.0.1
Port: 8080
Middlewares:
Log: false

PrometheusHandler

http server 默认集成了 prometheus 指标监控,指标分别如下:

Requested time-consuming indicator type is Histogram, default label has path,buckets defined as 5, 10, 25, 50, 100, 250, 500, 1000

http_server_requests_duration_ms

Error statistics indicator type is counter, default label has paths, code

http_server_requests_code_total

Prometheus Middleware can be disabled by configuration, enabled by default

Name: HelloWorld.api
Host: 127.0.0.1
Port: 8080
Middlewares:
Prometheus: false

MaxConnsHandler

Use to limit the maximum number of requests sent by http:// when the number of concurrent requests exceeds the set value (default is 10,000), when exceeding the setting value returns http.StatusServiceUnav's status code

Disable is same with above

Custom Middleware

Use Use method to apply custom middleware

server := rest.MustNewServer(rest.RestConf{})
defer server.Stop()

server.Use(middleware)

func middleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Middleware", "static-middleware")
next(w, r)
}
}