You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/08/24 14:29:58 UTC

[GitHub] [dubbo-go] lethexixin commented on issue #1410: grpc Context failed to transfer data

lethexixin commented on issue #1410:
URL: https://github.com/apache/dubbo-go/issues/1410#issuecomment-904694063


   @xavier-niu 谢谢,查资料才发现在 grpc 中,client 与 server 之间通过 context 传递上下文数据的时候,不能使用 context.WithValue,因为 server 端无法获取到 client 自定义的 key, 不过我们可以使用 grpc 提供的 medadata 接口:
   ```golang
   //客户端:   
   func main() {
           req := &demo.Req{}
   	res := &demo.Res{}
   	ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("socket.addr", "127.0.0.1:12788"))
   	err := consumer.GetContextImpl().FContext(ctx, req, res)
   }
   
   //服务端:
   func (g *Provider) FContext(ctx context.Context, req *demo.Req) (res *demo.Res, err error) {
   	if md, ok := metadata.FromIncomingContext(ctx); ok {
   		xIds := md.Get("socket.addr")
   		fmt.Println("xIds:",xIds)
   	}
   	return
   }
   ```
   **如此,服务端就可以拿到context的值了,不过这个metadata方式只能传string类型**  
   
   
   
   参考:https://mp.weixin.qq.com/s/cwkfYA2rgNIbHom5Yoeizg
   ![image](https://user-images.githubusercontent.com/46220496/130634071-7e49c399-642b-480b-aab5-69ef649cc56c.png)
   
   
   **最后:或许可以在 [dubbo-go-samples/context](https://github.com/apache/dubbo-go-samples/tree/master/context) 模块的readme.md 文件中描述一下grpc中关于context的问题及解决方案,避免别的小伙伴入坑😳**


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org