// 绑定到全部网卡 或者 指定网卡
ChannelFuture future = serverBootstrap.bind(
new InetSocketAddress(transportConfig.getHost(), transportConfig.getPort()));
ChannelFuture channelFuture = future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("HTTP/2 Server bind to {}:{} success!",
transportConfig.getHost(), transportConfig.getPort());
}
} else {
LOGGER.error("HTTP/2 Server bind to {}:{} failed!",
transportConfig.getHost(), transportConfig.getPort());
stop();
}
}
});
try {
channelFuture.await();
if (channelFuture.isSuccess()) {
flag = Boolean.TRUE;
} else {
throw new SofaRpcRuntimeException("Server start fail!", future.cause());
}
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
return flag;
}
}
RestServer 提供Rest服务,底层通信实现具体可见SofaNettyJaxrsServer。
/**
* Rest服务端
*/
protected SofaNettyJaxrsServer httpServer;
@Override
public void init(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
httpServer = buildServer();
}
SofaNettyJaxrsServer中服务启动的具体代码
@Override
public void start() {
// CHANGE: 增加线程名字
boolean daemon = serverConfig.isDaemon();
boolean isEpoll = serverConfig.isEpoll();
NamedThreadFactory ioFactory = new NamedThreadFactory("SEV-REST-IO-" + port, daemon);
NamedThreadFactory bizFactory = new NamedThreadFactory("SEV-REST-BIZ-" + port, daemon);
eventLoopGroup = isEpoll ? new EpollEventLoopGroup(ioWorkerCount, ioFactory)
: new NioEventLoopGroup(ioWorkerCount, ioFactory);
eventExecutor = isEpoll ? new EpollEventLoopGroup(executorThreadCount, bizFactory)
: new NioEventLoopGroup(executorThreadCount, bizFactory);
// Configure the server.
bootstrap = new ServerBootstrap()
.group(eventLoopGroup)
.channel(isEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.childHandler(createChannelInitializer())
.option(ChannelOption.SO_BACKLOG, backlog)
.childOption(ChannelOption.SO_KEEPALIVE, serverConfig.isKeepAlive()); // CHANGE: setKeepAlive
for (Map.Entry<ChannelOption, Object> entry : channelOptions.entrySet()) {
bootstrap.option(entry.getKey(), entry.getValue());
}