Search in sources :

Example 1 with HealthStatusManager

use of io.grpc.services.HealthStatusManager in project grpc-java by grpc.

the class HostnameServer method main.

public static void main(String[] args) throws IOException, InterruptedException {
    int port = 50051;
    String hostname = null;
    if (args.length >= 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException ex) {
            System.err.println("Usage: [port [hostname]]");
            System.err.println("");
            System.err.println("  port      The listen port. Defaults to " + port);
            System.err.println("  hostname  The name clients will see in greet responses. ");
            System.err.println("            Defaults to the machine's hostname");
            System.exit(1);
        }
    }
    if (args.length >= 2) {
        hostname = args[1];
    }
    HealthStatusManager health = new HealthStatusManager();
    final Server server = ServerBuilder.forPort(port).addService(new HostnameGreeter(hostname)).addService(ProtoReflectionService.newInstance()).addService(health.getHealthService()).build().start();
    System.out.println("Listening on port " + port);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // Start graceful shutdown
            server.shutdown();
            try {
                // Wait for RPCs to complete processing
                if (!server.awaitTermination(30, TimeUnit.SECONDS)) {
                    // That was plenty of time. Let's cancel the remaining RPCs
                    server.shutdownNow();
                    // shutdownNow isn't instantaneous, so give a bit of time to clean resources up
                    // gracefully. Normally this will be well under a second.
                    server.awaitTermination(5, TimeUnit.SECONDS);
                }
            } catch (InterruptedException ex) {
                server.shutdownNow();
            }
        }
    });
    // This would normally be tied to the service's dependencies. For example, if HostnameGreeter
    // used a Channel to contact a required service, then when 'channel.getState() ==
    // TRANSIENT_FAILURE' we'd want to set NOT_SERVING. But HostnameGreeter has no dependencies, so
    // hard-coding SERVING is appropriate.
    health.setStatus("", ServingStatus.SERVING);
    server.awaitTermination();
}
Also used : HealthStatusManager(io.grpc.services.HealthStatusManager) Server(io.grpc.Server)

Example 2 with HealthStatusManager

use of io.grpc.services.HealthStatusManager in project grpc-java by grpc.

the class XdsHelloWorldServer method main.

public static void main(String[] args) throws IOException, InterruptedException {
    int port = 50051;
    String hostname = null;
    ServerCredentials credentials = InsecureServerCredentials.create();
    if (args.length >= 1 && "--xds-creds".equals(args[0])) {
        // The xDS credentials use the security configured by the xDS server when available. When xDS
        // is not used or when xDS does not provide security configuration, the xDS credentials fall
        // back to other credentials (in this case, InsecureServerCredentials).
        credentials = XdsServerCredentials.create(InsecureServerCredentials.create());
        args = Arrays.copyOfRange(args, 1, args.length);
    }
    if (args.length >= 1) {
        try {
            port = Integer.parseInt(args[0]);
        } catch (NumberFormatException ex) {
            System.err.println("Usage: [--xds-creds] [PORT [HOSTNAME]]");
            System.err.println("");
            System.err.println("  --xds-creds  Use credentials provided by xDS. Defaults to insecure");
            System.err.println("");
            System.err.println("  PORT      The listen port. Defaults to " + port);
            System.err.println("  HOSTNAME  The name clients will see in greet responses. ");
            System.err.println("            Defaults to the machine's hostname");
            System.exit(1);
        }
    }
    if (args.length >= 2) {
        hostname = args[1];
    }
    // Since the main server may be using TLS, we start a second server just for plaintext health
    // checks
    int healthPort = port + 1;
    final HealthStatusManager health = new HealthStatusManager();
    final Server server = XdsServerBuilder.forPort(port, credentials).addService(new HostnameGreeter(hostname)).addService(// convenient for command line tools
    ProtoReflectionService.newInstance()).addService(// allow management servers to monitor health
    health.getHealthService()).build().start();
    final Server healthServer = XdsServerBuilder.forPort(healthPort, InsecureServerCredentials.create()).addService(// allow management servers to monitor health
    health.getHealthService()).build().start();
    System.out.println("Listening on port " + port);
    System.out.println("Plaintext health service listening on port " + healthPort);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            health.setStatus("", ServingStatus.NOT_SERVING);
            // Start graceful shutdown
            server.shutdown();
            try {
                // Wait for RPCs to complete processing
                if (!server.awaitTermination(30, TimeUnit.SECONDS)) {
                    // That was plenty of time. Let's cancel the remaining RPCs
                    server.shutdownNow();
                    // shutdownNow isn't instantaneous, so give a bit of time to clean resources up
                    // gracefully. Normally this will be well under a second.
                    server.awaitTermination(5, TimeUnit.SECONDS);
                }
                healthServer.shutdownNow();
                healthServer.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException ex) {
                server.shutdownNow();
                healthServer.shutdownNow();
            }
        }
    });
    // This would normally be tied to the service's dependencies. For example, if HostnameGreeter
    // used a Channel to contact a required service, then when 'channel.getState() ==
    // TRANSIENT_FAILURE' we'd want to set NOT_SERVING. But HostnameGreeter has no dependencies, so
    // hard-coding SERVING is appropriate.
    health.setStatus("", ServingStatus.SERVING);
    server.awaitTermination();
}
Also used : HealthStatusManager(io.grpc.services.HealthStatusManager) Server(io.grpc.Server) ServerCredentials(io.grpc.ServerCredentials) InsecureServerCredentials(io.grpc.InsecureServerCredentials) XdsServerCredentials(io.grpc.xds.XdsServerCredentials)

Aggregations

Server (io.grpc.Server)2 HealthStatusManager (io.grpc.services.HealthStatusManager)2 InsecureServerCredentials (io.grpc.InsecureServerCredentials)1 ServerCredentials (io.grpc.ServerCredentials)1 XdsServerCredentials (io.grpc.xds.XdsServerCredentials)1