title: How to make a grpc client to call multiple servers?
body: <p>So far I've built a simple client-server architecture and manage it to work. I am able to send a request and get back a valid response.</p>
  
  <p>My problem it's how do I make the client to have multiple grpc servers to be able to distribute the work. Suppose I have a master (client) which sends requests to mutiple servers which are on different machines (therefore different IP's. Is there something that might help me on the client side? Have a list of hosts and ports and create a load balancer / mannagedchannel factory?
  The thing is here that I want to have multiple machines connected to one master machine in a distributed system. Each request in grpc it's a file which the server (worker) computes it and return a response.</p>
  
  <p>Thank you. So far here is the code for the client/server with grpc which works very fine.</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Client side</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <pre><code>@Component
  public class DocumentParserClient {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(DocumentParserClient.class);
      private static final String GRPC_HOST = "localhost";
      private static final int GRPC_PORT_I1 = 6565;
      private static final int GRPC_PORT_I2 = 6566;
  
      private ManagedChannel managedChannel;
      private WikiDocParserGrpc.WikiDocParserBlockingStub wikiDocParserBlockingStub;
  
      @PostConstruct
      private void init() {
          this.managedChannel = ManagedChannelBuilder.forAddress(GRPC_HOST, GRPC_PORT_I1).usePlaintext(true).build();
          this.wikiDocParserBlockingStub = WikiDocParserGrpc.newBlockingStub(managedChannel);
      }
  
      public String parseDocument(WikiOWrapperDto wikiOWrapperDto) {
          long start = System.currentTimeMillis();
          LOGGER.info("client sending {}",start);
          WikiOWrapper wikiOWrapper = GrpcDtoTransform.wrapINPUTFromDtoToGRPC(wikiOWrapperDto);
          LOGGER.info("client parsing {}", System.currentTimeMillis() - start);
          TokenCompressorDoc tokenCompressorDoc = wikiDocParserBlockingStub.parseWikiDOC(wikiOWrapper);
          LOGGER.info("client received {}", System.currentTimeMillis() - start);
          return "Success";
      }
  
  }
  </code></pre>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>Server side</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <pre><code>@GRpcService
  public class WikiDocParserGrpcService extends WikiDocParserGrpc.WikiDocParserImplBase {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(WikiDocParserGrpcService.class);
      private JSoupParser jSoupParser;
  
      @Autowired
      public WikiDocParserGrpcService(JSoupParser jSoupParser) {
          this.jSoupParser = jSoupParser;
      }
  
      @Override
      public void parseWikiDOC(WikiOWrapper wikiOWrapper, StreamObserver&lt;TokenCompressorDoc&gt; responseObserver) {
          long start = System.currentTimeMillis();
          LOGGER.info("server received {}", start);
          WikiOWrapperDto wikiOWrapperDto = GrpcDtoTransform.wrapOUTPUTfromGrpcToDto(wikiOWrapper);
          TokenCompressorDocDto tokenCompressorDocDto = jSoupParser.parseJsonParseWrapperFromObject(wikiOWrapperDto);
          responseObserver.onNext(GrpcDtoTransform.wrapOUTPUTFromGRPCToDto(tokenCompressorDocDto));
          responseObserver.onCompleted();
          LOGGER.info("server responded in {}", System.currentTimeMillis()- start);
      }
  }
  </code></pre>