gRPC Java server with setReuseAddress(true)?

514 Views Asked by At

I am working on a gRPC server written in Java. It uses NettyServerBuilder to start/run the server. I am wondering, how to set setReuseAddress(true) for the server, so that it can restart quickly (either on crashes or maintenance restarts.)

This is the current startup:

import io.grpc.Server;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;

//...

private Server server;

//...

server = NettyServerBuilder
                .forPort(port)
                .permitKeepAliveWithoutCalls(true)
                .permitKeepAliveTime(5, TimeUnit.SECONDS)
                .addService(this.queryService)
                .addService(this.commandService)
                .build();
1

There are 1 best solutions below

2
On BEST ANSWER

This should do the trick:

Server server = NettyServerBuilder
            .forPort(port)
            // ...
            .withChildOption(ChannelOption.SO_REUSEADDR, true)
            .build();