grpc header/cookie in Go

huangapple go评论88阅读模式
英文:

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/metadata package and metadata.NewContext() to send metadata from the client-side.
  • Use grpc.SendHeader() and grpc.SetTrailer() to send metadata from the server-side.
  • Use the grpc.Header() and grpc.Trailer() CallOptions for receiving the Metadata on the client-side.
  • Use metadata.FromContext() for receiving metadata on the server-side.

huangapple
  • 本文由 发表于 2015年7月13日 17:40:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/31379995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定