如何在Golang gRPC中获取客户端IP地址和用户代理?

我设置了一系列GRPC请求和响应,它们都可以正常工作,但是当我试图获取调用我的GRPCAPI的客户端IP地址和用户代理时,我陷入了困境。

我阅读了GRPC文档和其他来源,但没有找到多少有价值的信息。他们中很少有人在Golang中谈论GRPC。

在设置GRPCAPI时,是否应该设置一个键值来将IP地址存储在上下文中?

The way to get the IP address was already answered pretty well here: Correct way of getting Client's IP Addresses from http.Request (Golang)

As for the User-Agent header, you just need to parse the User-Agent header like such: userAgent := response.Header.Get("User-Agent")