Skip to content

Could one gRPC server run multiple same class of service?

Consider following code

Server server = ServerBuilder.forPort(8080)
    .addService(new AServiceImpl(argA))
    .addService(new AServiceImpl(argB))

I want this server to run two AService with different args, argA and argB, is it possible?

If possible, when a AStub call the method, which instance of service would it call?


I believe it is not possible, because each service is added to a map,

// Store per-service first, to make sure services are added/replaced atomically.
private final HashMap<String, ServerServiceDefinition> services =
    new LinkedHashMap<>();

by name,

Builder addService(ServerServiceDefinition service) {
  services.put(service.getServiceDescriptor().getName(), service);
  return this;

therefore new AServiceImpl(argB) will override the other.