通过同一连接访问多个gRPC服务 [英] Access multiple gRPC services over the same connection
问题描述
我正在运行在localhost:6000
上侦听的grpc
服务器,公开了2个grpc
服务:RegisterSubscriberServiceServer
和RegisterDropperServiceServer
.由于这两个服务都可以通过localhost:6000
进行访问,因此我只想从存根中拨打该地址.
I am running a grpc
server listening on localhost:6000
, exposing 2 grpc
services: RegisterSubscriberServiceServer
and RegisterDropperServiceServer
. Since both of these services are reachable from localhost:6000
, I'd like to only dial this address from the stub.
服务器如下:
func main() {
grpcServer := grpc.NewServer()
pb.RegisterSubscriberServiceServer(grpcServer, &subscriberServer{})
pb.RegisterDropperServiceServer(grpcServer, &dropperServer{})
l, err := net.Listen("tcp", ":6000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Println("Listening on tcp://localhost:6000")
grpcServer.Serve(l)
}
为什么每个grpc
服务都需要拨不同的套接字?
Why do I need to dial a different socket for each grpc
service?
type DropperRPC struct {
conn *grpc.ClientConn
client pb.DropperServiceClient
chunkSize int
}
type SubscriberRPC struct {
conn *grpc.ClientConn
client pb.SubscriberServiceClient
chunkSize int
}
func NewSubscriber() (c SubscriberRPC, err error) {
c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c.client = pb.NewSubscriberServiceClient(c.conn)
return
}
func NewDropper() (c DropperRPC, err error) {
c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c.client = pb.NewDropperServiceClient(c.conn)
return
}
由于代码基本上是重复的,以适应每种服务,所以我不能只使用Interface
来减少代码吗?
And since the code is basically duplicated to accommodate each service, can't I just use an Interface
to reduce the code?
func main() {
c1, err := NewSubscriber()
if err != nil {
log.Fatal(err)
}
c2, err := NewDropper()
if err != nil {
log.Fatal(err)
}
cc1 := &c1
cc2 := &c2
}
,然后使用此Interface
为每个服务实现客户端grpc
功能,而不是为每个服务创建新的struct
.我已经找到 cmux ,但是必须有一种无需使用外部库即可完成此操作的方法.
And then use this Interface
to implement the client side grpc
functions for each service, instead of creating a new struct
for each service. I have found cmux , but there must be a way to do this without using external libraries.
推荐答案
为什么每个grpc服务都需要拨一个不同的插座?
Why do I need to dial a different socket for each grpc service?
你不知道.您可以创建一个grpc.ClientConn
并将其传递给多个pb.New*Client()
函数,它们将共享相同的连接.
You don't. You can create one grpc.ClientConn
and pass it to multiple pb.New*Client()
functions, and they will share the same connection(s).
func main() {
cc, err := grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c1 := pb.NewSubscriberServiceClient(cc)
c2 := pb.NewDropperServiceClient(cc)
}
然后使用此接口为每个服务实现客户端grpc函数,而不是为每个服务创建新的结构
And then use this Interface to implement the client side grpc functions for each service, instead of creating a new struct for each service
pb.go
文件中生成的代码完成了执行RPC所需的一切.除非您有特殊的逻辑要在每次执行呼叫时自动发生,否则您不必实现任何客户端功能.
The generated code in the pb.go
file does everything you need to perform RPCs. You shouldn't have to implement anything client-side unless you have special logic you want to happen automatically every time you perform a call.
如果这两个服务具有唯一的方法名称,则可以将它们放在相同的结构中,这样就不必为了一个较小的方便而单独使用它们了:
If the two services have unique method names, you could put them into the same struct so you don't have to use them separately, as a minor convenience:
type SubscriberDropper struct {
pb.SubscriberServiceClient
pb.DropperServiceClient
}
func main() {
// ... as above ...
sd := &SubscriberDropper{c1, c2}
}
这篇关于通过同一连接访问多个gRPC服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!