元数据,header相关

    用处:可以自定义自己的header

    如:

    2022/04/08 16:31:09 http2: Framer 0xc000074000: wrote HEADERS flags=END_HEADERS stream=7 len=65 2022/04/08 16:31:09 http2: Framer 0xc000074000: read HEADERS flags=END_HEADERS stream=7 len=23 2022/04/08 16:31:09 http2: decoded hpack field header field “:status” = “200” 2022/04/08 16:31:09 http2: decoded hpack field header field “content-type” = “application/grpc” 2022/04/08 16:31:09 http2: decoded hpack field header field “location” = “MTV” 2022/04/08 16:31:09 http2: decoded hpack field header field “timestamp” = “Apr 8 16:31:09.295013400”

    用法:

    1. // 获取服务端header
    2. md, ok := metadata.FromIncomingContext(ctx)
    3. // 自定义header
    4. header := metadata.New(map[string]string{"location": "MTV", "timestamp": time.Now().Format(timestampFormat)})
    5. grpc.SendHeader(ctx, header)

    客户端调用时候使用:

        var header, trailer metadata.MD
        r, err := c.UnaryEcho(ctx, &pb.EchoRequest{Message: message}, grpc.Header(&header), grpc.Trailer(&trailer))