英文:
grpc header/cookie in Go
问题
我想在服务器上创建一个应用程序,可以同时被Go APP和Java应用程序调用。
由于某些原因,这里有一个cookie身份验证和oAuth机制,所以我想将一个Go应用程序设置为身份验证微服务,用于身份验证目的。
由于GRPC是建立在HTTP2之上的,所以头部和cookie都在协议中。但是我没有找到如何在Go中实现在RPC发生时携带头部和cookie的方法。在GitHub上,我只找到了关于头部的JAVA实现,链接如下:
https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/header
有人可以给我一些关于在Go中实现这个目的的指导吗?
英文:
I want to do place on server application which can be called by Go APP and Java app both.
for some reason ,there's a cookie authentication and oAuth mechanism ,so I want to set one Go app as Auth Micro-service for the authentication purpose.
As GRPC is built on the HTTP2 ,so The headers and cookies are on the protocol.but I did not find out how to carry on header and cookie when the rpc occurs,implemented by Go, on GitHub I only found the JAVA-Implementation for headers at :
https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/header
Can anybody give me some direction of Go implementation for this purpose?
答案1
得分: 25
gRPC中的头部被称为"Metadata"。客户端只能发送"headers",而服务器可以发送"headers"和"trailers"。
你想要:
- 使用
google.golang.org/grpc/metadata包和metadata.NewContext()从客户端发送元数据。 - 使用
grpc.SendHeader()和grpc.SetTrailer()从服务器端发送元数据。 - 使用
grpc.Header()和grpc.Trailer()的CallOptions来接收客户端端的Metadata。 - 使用
metadata.FromContext()来接收服务器端的元数据。
英文:
Headers in gRPC are called "Metadata." Clients can only send "headers". Servers can send both "headers" and "trailers."
You want to:
- Use the
google.golang.org/grpc/metadatapackage andmetadata.NewContext()to send metadata from the client-side. - Use
grpc.SendHeader()andgrpc.SetTrailer()to send metadata from the server-side. - Use the
grpc.Header()andgrpc.Trailer()CallOptionsfor receiving theMetadataon the client-side. - Use
metadata.FromContext()for receiving metadata on the server-side.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论