在不同主机上代理GRPC请求

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

Proxying GRPC requests on different hosts

问题

我们使用golang实现了GRPC API,并且现在需要一些机制来路由请求到两个不同的主机(prod/stage)。问题是路由规则必须依赖于grpc metadata,而不是头部信息。实际上,我们将iOS版本作为metadata参数传递。据我所知,例如Nginx无法做到这一点。我们没有k8s或其他东西,只有简单的API守护程序在某个端口上监听。

authConf := viper.GetStringMapString("auth")
creds, err := credentials.NewServerTLSFromFile(authConf["certfile"], authConf["keyfile"])
if err != nil {
    panic(err)
}
lis, err := net.Listen("tcp", ":"+viper.GetString("port"))
if err != nil {
    panic(err)
}
英文:

We implemented GRPC API using golang, and now need some mechanism to route requests on two different hosts (prod/stage). The problem is the routing rule must be depended on grpc metadata, not the header. Actually we pass iOS version at metadata parameter. And as far as i know Nginx for example cannot do this. We have no k8s or something, just the simple API daemon listening on some port.

authConf := viper.GetStringMapString("auth")
creds, err := credentials.NewServerTLSFromFile(authConf["certfile"], authConf["keyfile"])
if err != nil {
    panic(err)
}
lis, err := net.Listen("tcp", ":"+viper.GetString("port"))
if err != nil {
    panic(err)
}

答案1

得分: 1

你可以使用envoy,它是一个类似于NginX的代理。

它支持gRPC(可以查看grpc-web),允许你定义多个路由并将流量重定向到不同的主机。我正在使用它根据URL(其中包含gRPC包/函数名称)将流量重定向到不同的服务,但也可以通过头部或元数据进行相同的操作。

在StackOverflow上提供一个实际的envoy配置超出了范围。

英文:

You can use envoy, which is a proxy just like NginX.

It has support for gRPC (take a look at grpc-web), which allows you to define multiple routes and redirect traffic to different hosts. I'm using it to redirect to different services based on the URL (which contains the gRPC package/function name), but it should also be possible to do the same with header or meta data.

Providing an actual envoy configuration that does this is out-of-scope on StackOverflow though.

huangapple
  • 本文由 发表于 2021年8月3日 17:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/68633530.html
匿名

发表评论

匿名网友

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

确定