gRPC 中的錯(cuò)誤處理:構(gòu)建更健壯、更可靠的微服務(wù)!
gRPC 在設(shè)計(jì)上鼓勵將錯(cuò)誤處理作為服務(wù)的一部分,而不是將其隱藏在消息體中。每個(gè) gRPC 服務(wù)天生就擁有一個(gè)錯(cuò)誤返回值,作為專門的錯(cuò)誤傳輸通道。所有 gRPC 中的錯(cuò)誤返回值應(yīng)該要么是 nil,要么是 由 status.Status 生成的錯(cuò)誤。這樣可以確保調(diào)用方可以輕松識別錯(cuò)誤。
1. 基本用法
簡單地返回 Go 錯(cuò)誤并不能被下游客戶端識別。正確的做法是:
- 調(diào)用 status.New 方法并傳入合適的錯(cuò)誤代碼以生成 status.Status 對象。
- 調(diào)用 status.Err 方法生成調(diào)用方可以識別的錯(cuò)誤,然后返回。
st := status.New(codes.NotFound, "some description")
err := st.Err()
傳入的錯(cuò)誤代碼類型為 codes.Code?;蛘?,你可以使用 status.Error 方法,它可以避免手動轉(zhuǎn)換。
err := status.Error(codes.NotFound, "some description")
2. 高級用法
上述錯(cuò)誤有一個(gè)限制:codes.Code 定義的錯(cuò)誤代碼只涵蓋了某些場景,無法全面表達(dá)業(yè)務(wù)中遇到的各種錯(cuò)誤場景。
gRPC 提供了一種機(jī)制來補(bǔ)充錯(cuò)誤中的信息:status.WithDetails 方法。
客戶端可以通過將錯(cuò)誤轉(zhuǎn)換回 status.Status 并使用 status.Details 方法直接獲取內(nèi)容。
status.Details 返回一個(gè)切片,它是 interface{} 的切片。但是,Go 會自動執(zhí)行類型轉(zhuǎn)換,允許通過斷言直接使用。
服務(wù)器端示例
- 生成 status.Status 對象
- 填充額外的錯(cuò)誤信息
func ErrorWithDetails() error {
st := status.Newf(codes.Internal, fmt.Sprintf("something went wrong: %v", "api.Getter"))
v := &errdetails.PreconditionFailure_Violation{ //errDetails
Type: "test",
Subject: "12",
Description: "32",
}
br := &errdetails.PreconditionFailure{}
br.Violations = append(br.Violations, v)
st, _ = st.WithDetails(br)
return st.Err()
}
客戶端端示例
- 在 RPC 錯(cuò)誤后解析錯(cuò)誤信息
- 通過斷言直接獲取錯(cuò)誤詳細(xì)信息
resp, err := odinApp.CreatePlan(cli.StaffId.AssetId, gentRatePlanMeta(cli.StaffId))
if status.Code(err) != codes.InvalidArgument {
logger.Error("create plan error:%v", err)
} else {
for _, d := range status.Convert(err).Details() {
//
switch info := d.(type) {
case *errdetails.QuotaFailure:
logger.Info("Quota failure: %s", info)
case *errdetails.PreconditionFailure:
detail := d.(*errdetails.PreconditionFailure).Violations
for _, v1 := range detail {
logger.Info(fmt.Sprintf("details: %+v", v1))
}
case *errdetails.ResourceInfo:
logger.Info("ResourceInfo: %s", info)
case *errdetails.BadRequest:
logger.Info("ResourceInfo: %s", info)
default:
logger.Info("Unexpected type: %s", info)
}
}
}
logger.Infof("create plan success,resp=%v", resp)
原理
這些錯(cuò)誤是如何傳遞給調(diào)用方客戶端的呢?它們被放置在元數(shù)據(jù)中,然后在 HTTP 頭部中。元數(shù)據(jù)以鍵值對的形式存在。在錯(cuò)誤傳輸中,鍵是一個(gè)固定值:grpc-status-details-bin。值由 proto 編碼,并且是二進(jìn)制安全的。大多數(shù)語言都實(shí)現(xiàn)了這種機(jī)制。
圖片
注意
gRPC 對響應(yīng)頭有限制,最大為 8K,因此錯(cuò)誤不能太大。
參考
- Protocol Buffers Tutorial[1]
- errdetails[2]
總結(jié)
gRPC 提供了靈活的錯(cuò)誤處理機(jī)制,允許你以結(jié)構(gòu)化的方式傳遞錯(cuò)誤信息,幫助你構(gòu)建更健壯、更可靠的微服務(wù)。通過正確使用 status.Status 和 status.WithDetails 方法,你可以確保你的錯(cuò)誤信息清晰易懂,并能被客戶端輕松理解和處理。
參考資料
[1] Protocol Buffers Tutorial: https://protobuf.dev/getting-started/gotutorial/
[2] errdetails: https://pkg.go.dev/google.golang.org/genproto/googleapis/rpc/errdetails