W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
元信息透傳是基于傳輸協(xié)議透傳一些額外的 RPC 信息給下游,同時讀取傳輸協(xié)議中上游透傳的信息,透傳字段需結(jié)合內(nèi)部的治理能力,建議使用者自行擴展實現(xiàn)。
// MetaHandler reads or writes metadata through certain protocol.
// The protocol will be a thrift.TProtocol usually.
type MetaHandler interface {
WriteMeta(ctx context.Context, msg Message) (context.Context, error)
ReadMeta(ctx context.Context, msg Message) (context.Context, error)
}
package transmeta
var ClientTTHeaderHandler remote.MetaHandler = &clientTTHeaderHandler{}
// clientTTHeaderHandler implement remote.MetaHandler
type clientTTHeaderHandler struct{}
// WriteMeta of clientTTHeaderHandler writes headers of TTHeader protocol to transport
func (ch *clientTTHeaderHandler) WriteMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
ri := msg.RPCInfo()
transInfo := msg.TransInfo()
hd := map[uint16]string{
transmeta.FromService: ri.From().ServiceName(),
transmeta.FromIDC: ri.From().DefaultTag(consts.IDC, ""),
transmeta.FromMethod: ri.From().Method(),
transmeta.ToService: ri.To().ServiceName(),
transmeta.ToMethod: ri.To().Method(),
transmeta.MsgType: strconv.Itoa(int(msg.MessageType())),
}
transInfo.PutTransIntInfo(hd)
if metainfo.HasMetaInfo(ctx) {
hd := make(map[string]string)
metainfo.SaveMetaInfoToMap(ctx, hd)
transInfo.PutTransStrInfo(hd)
}
return ctx, nil
}
// ReadMeta of clientTTHeaderHandler reads headers of TTHeader protocol from transport
func (ch *clientTTHeaderHandler) ReadMeta(ctx context.Context, msg remote.Message) (context.Context, error) {
ri := msg.RPCInfo()
remote := remoteinfo.AsRemoteInfo(ri.To())
if remote == nil {
return ctx, nil
}
transInfo := msg.TransInfo()
strInfo := transInfo.TransStrInfo()
ad := strInfo[transmeta.HeaderTransRemoteAddr]
if len(ad) > 0 {
// when proxy case to get the actual remote address
_ = remote.SetRemoteAddr(utils.NewNetAddr("tcp", ad))
}
return ctx, nil
}
cli, err := xxxservice.NewClient(targetService, client.WithMetaHandler(transmeta.ClientTTHeaderHandler))
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: