[golang] Multiple servers (and clients) over the same (grpc) connection

927 views
Skip to first unread message

r.ki...@gmail.com

unread,
Apr 11, 2017, 1:39:21 AM4/11/17
to grpc.io
I have two services in my .proto file and I wonder if I can serve them on the same ip:port:

 lis, _ := net.Listen("tcp", ":8000")
 grpcServer
:= grpc.NewServer()
 pb
.RegisterService1Server(grpcServer, &service1Server{})
 pb
.RegisterService2Server(grpcServer, &service2Server{})
 grpcServer
.Serve(lis)

Is this approach ok?

Also (on a client side) I want to use single connection to a server and call method defined in both services:

conn, _ := grpc.Dial(serverAddr)
cli1
:= pb.NewService1Client(conn)
cli2
:= pb.NewService2Client(conn)

Would it work?

Thanks a lot.

mmu...@google.com

unread,
Apr 19, 2017, 7:30:16 PM4/19/17
to grpc.io, r.ki...@gmail.com
Yes, this should work. 

Suchismita Sengupta

unread,
Jun 23, 2022, 6:45:08 AMJun 23
to grpc.io
Is multiple services equivalent to multiple servers in golang? 
I am trying to create a client with multiple servers in grpc in golang.
Can someone help? I have just started with golang.

Amit Saha

unread,
Jun 23, 2022, 5:51:52 PMJun 23
to grpc.io
On Thursday, June 23, 2022 at 8:45:08 PM UTC+10 Suchismita Sengupta wrote:
Is multiple services equivalent to multiple servers in golang? 
I am trying to create a client with multiple servers in grpc in golang.
Can someone help? I have just started with golang.

Think about it as a gRPC server which is capable of hosting one ore more gRPC services. When you initialise a gRPC server, you can register one or more gRPC services on it and similarly when you write a gRPC client, you can communicate with only one or all of the services registered on the gRPC server. 

An example of a Go server which registers two services along with some tests - they will hopefully help you to write a client as well:


Reply all
Reply to author
Forward
0 new messages