Echo 框架: 自动添加 RequestId

原创
2021/11/22 00:34
阅读数 141

介绍

通过一个完整例子,在基于 Echo 框架中,为每一个 API 自动添加 RequestId 。

我们将会使用 rk-boot 来启动 Echo 微服务。

请访问如下地址获取完整教程:

安装

go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-echo

快速开始

开启了 meta 中间件之后,每一个请求都会自动包含如下值。

Header 键 详情
X-Request-Id 中间件会自动生成请求 ID。
X-[Prefix]-App 服务名称。
X-[Prefix]-App-Version 服务版本。
X-[Prefix]-App-Unix-Time 当前服务的 Unix 时间。
X-[Prefix]-Request-Received-Time 接收到请求的时间戳。

1.创建 boot.yaml

为了验证,我们启动了 commonService,commonService 里包含了一系列常用 API,例如 /rk/v1/healthy。

---
echo:
  - name: greeter                   # Required
    port: 8080                      # Required
    enabled: true                   # Required
    commonService:
      enabled: true                 # Optional, enable common service
    interceptors:
      meta:
        enabled: true               # Optional, enable meta middleware

2.创建 main.go

// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	_ "github.com/rookie-ninja/rk-echo/boot"
)

// Application entrance.
func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Bootstrap
	boot.Bootstrap(context.Background())

	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.启动 main.go

$ go run main.go

4.验证

$ curl -vs -X GET localhost:8080/rk/v1/healthy
  ...
  < HTTP/1.1 200 OK
  < Content-Type: application/json; charset=utf-8
  < X-Request-Id: 78c25a06-3b34-4ecb-b9dd-7197078873c7
  < X-Rk-App-Name: rk-demo
  < X-Rk-App-Unix-Time: 2021-11-21T00:24:49.662023+08:00
  < X-Rk-App-Version: master-2c9c6fd
  < X-Rk-Received-Time: 2021-11-21T00:24:49.662023+08:00
  ...
  {"healthy":true}

覆盖 requestId

如果我们希望自定义 requestId,需要添加一个 Header。

func Greeter(ctx echo.Context) error {
	// Override request id
	rkechoctx.SetHeaderToClient(ctx, rkechoctx.RequestIdKey, "request-id-override")
	// We expect new request id attached to logger
	rkechoctx.GetLogger(ctx).Info("Received request")

	return ctx.JSON(http.StatusOK, &GreeterResponse{
		Message: fmt.Sprintf("Hello %s!", ctx.QueryParam("name")),
	})
}

// Response.
type GreeterResponse struct {
	Message string
}

RequestId 会被覆盖。

$ curl -vs -X GET "localhost:8080/v1/greeter?name=rk-dev"
...
< X-Request-Id: request-id-override
< X-Rk-App-Name: rk-demo
< X-Rk-App-Unix-Time: 2021-11-21T00:37:16.514685+08:00
< X-Rk-App-Version: master-2c9c6fd
< X-Rk-Received-Time: 2021-11-21T00:37:16.514685+08:00
...
{"Message":"Hello rk-dev!"}

如果我们启动了日志中间件,那我们会看到如下的日志。

2021-11-22T00:30:39.442+0800    INFO    basic/main.go:46        Received request        {"requestId": "request-id-override"}
------------------------------------------------------------------------
endTime=2021-11-22T00:30:39.442963+08:00
startTime=2021-11-22T00:30:39.442746+08:00
elapsedNano=216828
timezone=CST
ids={"eventId":"request-id-override","requestId":"request-id-override"}
app={"appName":"rk-demo","appVersion":"master-5507ff5","entryName":"greeter","entryType":"EchoEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:52446
operation=/v1/greeter
resCode=200
eventStatus=Ended
EOE

覆盖 header 前缀

---
echo:
  - name: greeter
    ...
    interceptors:
      meta:
        enabled: true        # Enable meta middleware
        prefix: "Override"   # Override prefix which formed as X-[Prefix]-xxx
$ curl -vs -X GET localhost:8080/rk/v1/healthy
...
< X-Override-App-Name: rk-demo
< X-Override-App-Unix-Time: 2021-11-21T00:40:15.761993+08:00
< X-Override-App-Version: master-2c9c6fd
< X-Override-Received-Time: 2021-11-21T00:40:15.761993+08:00
< X-Request-Id: 1449deb5-464d-4b65-8430-985413c2671b
...
{"healthy":true}
展开阅读全文
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部