grpc-spring icon indicating copy to clipboard operation
grpc-spring copied to clipboard

how to manual control grpc server start or not

Open celityowce opened this issue 4 years ago • 4 comments

sometimes i want to manual start the grpc-server.

celityowce avatar Mar 11 '21 08:03 celityowce

There is currently no way to do that directly. But there is one thing that you can do.

Recreate the bean(s) created from https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/autoconfigure/GrpcServerFactoryAutoConfiguration.java and just omit the server lifecycle. Then you have full control about starting and stopping the server.

Does this answer your question?

ST-DDT avatar Mar 11 '21 18:03 ST-DDT

Yes, I also hope that I can start it manually. Sometimes my own function has not been initialized yet, but it has been started automatically, which caused me some troubles.

zhouwenbin00 avatar Mar 12 '21 03:03 zhouwenbin00

Myabe you can take a look here: https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/grpc-server-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/server/serverfactory/GrpcServerLifecycle.java

/**
     * Creates and starts the grpc server.
     *
     * @throws IOException If the server is unable to bind the port.
     */
    protected void createAndStartGrpcServer() throws IOException {
        if (this.server == null) {
            final Server localServer = this.factory.createServer();
            this.server = localServer;
            localServer.start();
            log.info("gRPC Server started, listening on address: " + this.factory.getAddress() + ", port: "
                    + this.factory.getPort());

            // Prevent the JVM from shutting down while the server is running
            final Thread awaitThread = new Thread(() -> {
                try {
                    localServer.awaitTermination();
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
            awaitThread.setName("grpc-server-container-" + (serverCounter.incrementAndGet()));
            awaitThread.setDaemon(false);
            awaitThread.start();
        }
    }

xieyucan avatar Mar 12 '21 06:03 xieyucan

If I have some free time, I will check whether I can turn the autostart off (configurable).

ST-DDT avatar Mar 15 '21 21:03 ST-DDT