My test is based on examples/python/multiplex from grpc repo:
Service:
class _GreeterServicer(helloworld_pb2_grpc.GreeterServicer):
def __init__(self, greeter):
self.greeter = greeter
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(
message='Hello, {}! This is {}!'.format(request.name, self.greeter))
Server:
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("John"),
server)
helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("Jim"),
server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
Client:
def run():
for _ in range(10):
with grpc.insecure_channel('localhost:50051') as channel:
greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
greeter_response = greeter_stub.SayHello(
helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + greeter_response.message)
Since I'm opening a new channel for each iteration, I was expecting to get an output with a mix of "Hello, you! This is Jim!" and "Hello, you! This is John!", but instead I'm getting only:
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is John!
that is, the first service of GreeterServicer I added to the server, which supposedly ignore the second servicer instance.
So, my question is if it's even possible to do something like this on a
single server and if there is a best practice to handle such scenarios
where I would want two mostly identical services, parameterized
differently: different servers load balanced (thus, following https://grpc.io/blog/grpc-load-balancing best practices)? Multiple threads implemented by me within the service?
In my particular scenario, the parameters I'd like to pass are some
credentials to be used within the service implementation, and my goal
would be, then, to have these multiple identical services running
concurrently, with different credentials, so that the end user has no idea there are multiple
instances, when making requests.
Thank you very much,
Federico