From f67e653abee77a23dfd74281fa76cc87cc8f6ee8 Mon Sep 17 00:00:00 2001 From: linwenxiang <991154416@qq.com> Date: Fri, 29 Jan 2021 14:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86requestId=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=85=BC=E5=AE=B9istio=E7=9A=84header=E8=BF=94?= =?UTF-8?q?=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/utils.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/tools/utils.go b/tools/utils.go index 3313e30..4dabe71 100644 --- a/tools/utils.go +++ b/tools/utils.go @@ -3,17 +3,17 @@ package tools import ( "errors" "fmt" - "gorm.io/gorm" "log" "runtime" "strconv" "github.com/gin-gonic/gin" - "github.com/google/uuid" - "github.com/spf13/cast" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) +const TrafficKey = "X-Request-Id" + func CompareHashAndPassword(e string, p string) (bool, error) { err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p)) if err != nil { @@ -56,15 +56,12 @@ func HasError(err error, msg string, code ...int) { // GenerateMsgIDFromContext 生成msgID func GenerateMsgIDFromContext(c *gin.Context) string { - var msgID string - data, ok := c.Get("msgID") - if !ok { - msgID = uuid.New().String() - c.Set("msgID", msgID) - return msgID + requestId := c.GetHeader(TrafficKey) + if requestId == "" { + fmt.Println("no", requestId) + c.Header(TrafficKey, requestId) } - msgID = cast.ToString(data) - return msgID + return requestId } // GetOrm 获取orm连接