• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.loadbalance;
18 
19 import io.grpc.Server;
20 import io.grpc.ServerBuilder;
21 import io.grpc.examples.helloworld.GreeterGrpc;
22 import io.grpc.examples.helloworld.HelloReply;
23 import io.grpc.examples.helloworld.HelloRequest;
24 import io.grpc.stub.StreamObserver;
25 
26 import java.io.IOException;
27 import java.util.concurrent.TimeUnit;
28 import java.util.logging.Logger;
29 
30 public class LoadBalanceServer {
31     private static final Logger logger = Logger.getLogger(LoadBalanceServer.class.getName());
32     static public final int serverCount = 3;
33     static public final int startPort = 50051;
34     private Server[] servers;
35 
start()36     private void start() throws IOException {
37         servers = new Server[serverCount];
38         for (int i = 0; i < serverCount; i++) {
39             int port = startPort + i;
40             servers[i] = ServerBuilder.forPort(port)
41                     .addService(new GreeterImpl(port))
42                     .build()
43                     .start();
44             logger.info("Server started, listening on " + port);
45         }
46         Runtime.getRuntime().addShutdownHook(new Thread(() -> {
47             System.err.println("*** shutting down gRPC server since JVM is shutting down");
48             try {
49                 LoadBalanceServer.this.stop();
50             } catch (InterruptedException e) {
51                 e.printStackTrace(System.err);
52             }
53             System.err.println("*** server shut down");
54         }));
55     }
56 
stop()57     private void stop() throws InterruptedException {
58         for (int i = 0; i < serverCount; i++) {
59             if (servers[i] != null) {
60                 servers[i].shutdown().awaitTermination(30, TimeUnit.SECONDS);
61             }
62         }
63     }
64 
blockUntilShutdown()65     private void blockUntilShutdown() throws InterruptedException {
66         for (int i = 0; i < serverCount; i++) {
67             if (servers[i] != null) {
68                 servers[i].awaitTermination();
69             }
70         }
71     }
72 
main(String[] args)73     public static void main(String[] args) throws IOException, InterruptedException {
74         final LoadBalanceServer server = new LoadBalanceServer();
75         server.start();
76         server.blockUntilShutdown();
77     }
78 
79     static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
80 
81         int port;
82 
GreeterImpl(int port)83         public GreeterImpl(int port) {
84             this.port = port;
85         }
86 
87         @Override
sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)88         public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
89             HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName() + " from server<" + this.port + ">").build();
90             responseObserver.onNext(reply);
91             responseObserver.onCompleted();
92         }
93     }
94 }
95