下面列出了io.netty.channel.epoll.EpollChannelOption#io.netty.channel.epoll.EpollServerSocketChannel 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected ServerBootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup acceptEventGroup, EventLoopGroup ioEventGroup) throws Exception {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.group(acceptEventGroup, ioEventGroup)
.childHandler(channelHandler)
.option(ChannelOption.SO_REUSEADDR, serverConfig.isReuseAddress())
.option(ChannelOption.SO_RCVBUF, serverConfig.getSocketBufferSize())
.option(ChannelOption.SO_BACKLOG, serverConfig.getBacklog())
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.SO_SNDBUF, serverConfig.getSocketBufferSize())
.childOption(ChannelOption.TCP_NODELAY, serverConfig.isTcpNoDelay())
.childOption(ChannelOption.SO_KEEPALIVE, serverConfig.isKeepAlive())
.childOption(ChannelOption.SO_LINGER, serverConfig.getSoLinger())
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
return serverBootstrap;
}
protected ServerBootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup acceptEventGroup, EventLoopGroup ioEventGroup) throws Exception {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.group(acceptEventGroup, ioEventGroup)
.childHandler(channelHandler)
.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress())
.option(ChannelOption.SO_RCVBUF, config.getSocketBufferSize())
.option(ChannelOption.SO_BACKLOG, config.getBacklog())
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.SO_SNDBUF, config.getSocketBufferSize())
.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
.childOption(ChannelOption.SO_KEEPALIVE, config.isKeepAlive())
.childOption(ChannelOption.SO_LINGER, config.getSoLinger())
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
return serverBootstrap;
}
/**
* 配置
*
* @param bootstrap
* @param sslContext
*/
protected ServerBootstrap configure(final ServerBootstrap bootstrap, final SslContext sslContext) {
//io.netty.bootstrap.Bootstrap - Unknown channel option 'SO_BACKLOG' for channel
bootstrap.channel(Constants.isUseEpoll(url) ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.childHandler(new MyChannelInitializer(url, sslContext))
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, url.getPositiveInt(Constants.CONNECT_TIMEOUT_OPTION))
.option(ChannelOption.SO_REUSEADDR, url.getBoolean(Constants.SO_REUSE_PORT_OPTION))
.option(ChannelOption.SO_BACKLOG, url.getPositiveInt(Constants.SO_BACKLOG_OPTION))
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(url.getPositiveInt(Constants.WRITE_BUFFER_LOW_WATERMARK_OPTION),
url.getPositiveInt(Constants.WRITE_BUFFER_HIGH_WATERMARK_OPTION)))
.childOption(ChannelOption.SO_RCVBUF, url.getPositiveInt(Constants.SO_RECEIVE_BUF_OPTION))
.childOption(ChannelOption.SO_SNDBUF, url.getPositiveInt(Constants.SO_SEND_BUF_OPTION))
.childOption(ChannelOption.SO_KEEPALIVE, url.getBoolean(Constants.SO_KEEPALIVE_OPTION))
.childOption(ChannelOption.TCP_NODELAY, url.getBoolean(Constants.TCP_NODELAY))
.childOption(ChannelOption.ALLOCATOR, BufAllocator.create(url));
return bootstrap;
}
private ServerBootstrap createServerBootstrap() {
return this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupWorker)
.channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.SO_REUSEADDR, true)
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.SO_LINGER, 3)
.childOption(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
.option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
.localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(
new RpcxProcessHandler(nettyServerConfig.getServerChannelMaxIdleTimeSeconds(), NettyServer.this)
);
}
});
}
@Override
public void start() {
bossGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
workerGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.childHandler(new ServerChannelInitializer(this))
.option(ChannelOption.SO_BACKLOG, socketBacklog)
.childOption(ChannelOption.SO_KEEPALIVE, socketKeepAlive);
bootstrap.localAddress(this.getAddress(), this.getPort());
bootstrap.bind().addListener(future -> {
if (future.isSuccess()) {
log.info(CleanstoneServer.getMessage("net.netty.bind-successful",
protocol.getClass().getSimpleName(), getAddress(), getPort() + ""));
} else {
log.error(CleanstoneServer.getMessage("net.netty.bind-failure",
getAddress().getHostAddress(), getPort() + ""), future.cause());
}
});
running = true;
}
/**
* Create the Netty sockets.
*/
public void createSocket() {
int threads = Runtime.getRuntime().availableProcessors();
this.bossGroup = (Epoll.isAvailable()) ? new EpollEventLoopGroup(threads) : new NioEventLoopGroup(threads);
this.workerGroup = (Epoll.isAvailable()) ? new EpollEventLoopGroup(threads) : new NioEventLoopGroup(threads);
this.bootstrap.group(bossGroup, workerGroup)
.channel((Epoll.isAvailable()) ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.childHandler(new MusChannelInitializer(this))
.option(ChannelOption.SO_BACKLOG, BACK_LOG)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_RCVBUF, BUFFER_SIZE)
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(BUFFER_SIZE))
.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
}
/**
* Create the Netty sockets.
*/
public void createSocket() {
int threads = Runtime.getRuntime().availableProcessors();
this.bossGroup = (Epoll.isAvailable()) ? new EpollEventLoopGroup(threads) : new NioEventLoopGroup(threads);
this.workerGroup = (Epoll.isAvailable()) ? new EpollEventLoopGroup(threads) : new NioEventLoopGroup(threads);
this.bootstrap.group(bossGroup, workerGroup)
.channel((Epoll.isAvailable()) ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.childHandler(new NettyChannelInitializer(this))
.option(ChannelOption.SO_BACKLOG, BACK_LOG)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_RCVBUF, BUFFER_SIZE)
.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(BUFFER_SIZE))
.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
}
/**
* Constructs an netty/io event executor.
*
* @param name thread group name.
* @param count thread count.
* @return
*/
public static NettyExecutor create(String name, int count) {
if (Epoll.isAvailable()) {
LOG.info("Epoll is available. Using the native socket transport.");
return new NettyExecutor(
epollEventLoopGroup(count, name + "-%d-Thread"),
EpollServerSocketChannel.class,
EpollSocketChannel.class);
} else {
LOG.info("Epoll not available. Using nio socket transport.");
return new NettyExecutor(
nioEventLoopGroup(count, name + "-%d-Thread"),
NioServerSocketChannel.class,
NioSocketChannel.class);
}
}
public void run() throws Exception {
ServerBootstrap b = new ServerBootstrap();
try {
if (isEpollAvailable) {
b.group(new EpollEventLoopGroup(this.conf.getEventLoopThreadCount()))
.channel(EpollServerSocketChannel.class);
} else {
b.group(new NioEventLoopGroup(this.conf.getEventLoopThreadCount()))
.channel(NioServerSocketChannel.class);
}
b.childHandler(new DefaultServerInitializer(conf, context))
.option(ChannelOption.SO_BACKLOG, conf.getBacklog())
.option(ChannelOption.SO_REUSEADDR, true);
Channel ch = b.bind(conf.getPort()).sync().channel();
ch.closeFuture().sync();
} finally {
}
}
@Override
@SuppressWarnings("unchecked")
public <CHANNEL extends Channel> CHANNEL getChannel(Class<CHANNEL> channelClass) {
if (channelClass.equals(SocketChannel.class)) {
return (CHANNEL) new EpollSocketChannel();
}
if (channelClass.equals(ServerSocketChannel.class)) {
return (CHANNEL) new EpollServerSocketChannel();
}
if (channelClass.equals(DatagramChannel.class)) {
return (CHANNEL) new EpollDatagramChannel();
}
if (channelClass.equals(DomainSocketChannel.class)) {
return (CHANNEL) new EpollDomainSocketChannel();
}
if (channelClass.equals(ServerDomainSocketChannel.class)) {
return (CHANNEL) new EpollServerDomainSocketChannel();
}
throw new IllegalArgumentException("Unsupported channel type: " + channelClass.getSimpleName());
}
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new EpollEventLoopGroup(1);
EventLoopGroup workerGroup = new EpollEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(EpollServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new EchoServerHandler());
}
});
ChannelFuture f = b.bind(8888).sync();
f.channel().closeFuture().sync();
}
finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
@Override
public ServerBootstrap createBootstrap() {
bootstrap = new ServerBootstrap();
if (isEpollAvailable) {
this.parentGroup = new EpollEventLoopGroup();
this.childGroup = new EpollEventLoopGroup();
bootstrap.channel(EpollServerSocketChannel.class);
} else {
this.parentGroup = new NioEventLoopGroup();
this.childGroup = new NioEventLoopGroup();
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.group(parentGroup(), childGroup());
bootstrap.childHandler(newChannelInitializer());
bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
// bootstrap.option(ChannelOption.SO_REUSEADDR, true);
// bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
return bootstrap;
}
private void initEventLoopGroup() {
// try Epoll first and if that does work, use nio.
try {
clientGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-client-%d", log));
serverGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-server-%d", log));
serverChannelClass = EpollServerSocketChannel.class;
clientChannelClass = EpollSocketChannel.class;
return;
} catch (Throwable e) {
log.debug("Failed to initialize native (epoll) transport. "
+ "Reason: {}. Proceeding with nio.", e.getMessage());
}
clientGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-client-%d", log));
serverGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-server-%d", log));
serverChannelClass = NioServerSocketChannel.class;
clientChannelClass = NioSocketChannel.class;
}
private synchronized ServerBootstrap createServerBootstrap(final ChannelPipelineInitializer<?> initializer) {
final ServerBootstrap serverBootstrap = new ServerBootstrap();
if (Epoll.isAvailable()) {
serverBootstrap.channel(EpollServerSocketChannel.class);
serverBootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
} else {
serverBootstrap.channel(NioServerSocketChannel.class);
}
final ChannelHandler serverChannelHandler = BGPChannel.createServerChannelHandler(initializer);
serverBootstrap.childHandler(serverChannelHandler);
serverBootstrap.option(ChannelOption.SO_BACKLOG, SOCKET_BACKLOG_SIZE);
serverBootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
serverBootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WATER_MARK);
// Make sure we are doing round-robin processing
serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, RECV_ALLOCATOR);
if (serverBootstrap.config().group() == null) {
serverBootstrap.group(this.bossGroup, this.workerGroup);
}
return serverBootstrap;
}
public static Class<? extends ServerSocketChannel> getServerSocketChannel(){
if(SUPPORTS_EPOLL){
return EpollServerSocketChannel.class;
}else{
return NioServerSocketChannel.class;
}
}
private ServerBootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup acceptEventGroup, EventLoopGroup ioEventGroup) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.group(acceptEventGroup, ioEventGroup)
.childHandler(channelHandler)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
return serverBootstrap;
}
public Class<? extends ServerChannel> getServerChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollServerDomainSocketChannel.class : EpollServerSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueServerDomainSocketChannel.class : KQueueServerSocketChannel.class;
}
return NioServerSocketChannel.class;
}
public BrokerService(MqttConfig mqttConfig) {
this.mqttConfig = mqttConfig;
this.brokerRoom = new BrokerRoom(mqttConfig);
connectProcessor = new ConnectProcessor(brokerRoom);
disconnectProcessor = new DisconnectProcessor(brokerRoom);
pingProcessor = new PingProcessor();
publishProcessor = new PublishProcessor(brokerRoom);
pubRelProcessor = new PubRelProcessor(brokerRoom);
subscribeProcessor = new SubscribeProcessor(brokerRoom);
unSubscribeProcessor = new UnSubscribeProcessor(brokerRoom);
pubRecProcessor = new PubRecProcessor(brokerRoom);
pubAckProcessor = new PubAckProcessor(brokerRoom);
pubCompProcessor = new PubCompProcessor(brokerRoom);
if (!mqttConfig.isUseEpoll()) {
this.selectorGroup = new NioEventLoopGroup(mqttConfig.getSelectorThreadNum(),
new ThreadFactoryImpl("SelectorEventGroup"));
this.ioGroup = new NioEventLoopGroup(mqttConfig.getIoThreadNum(), new ThreadFactoryImpl("IOEventGroup"));
this.clazz = NioServerSocketChannel.class;
} else {
this.selectorGroup = new EpollEventLoopGroup(mqttConfig.getSelectorThreadNum(),
new ThreadFactoryImpl("SelectorEventGroup"));
this.ioGroup = new EpollEventLoopGroup(mqttConfig.getIoThreadNum(), new ThreadFactoryImpl("IOEventGroup"));
this.clazz = EpollServerSocketChannel.class;
}
}
public void start() throws InterruptedException {
InetSocketAddress inet = new InetSocketAddress(hostPort.host, hostPort.port);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(eventLoopGroup);
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
if (eventLoopGroup instanceof EpollEventLoopGroup) {
bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
bootstrap.channel(EpollServerSocketChannel.class);
} else if (eventLoopGroup instanceof NioEventLoopGroup) {
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.childHandler(new NettyRpcChannelInitializer(invokerFactory, serializer, filters));
bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
bootstrap.childOption(ChannelOption.SO_RCVBUF, 256 * 1024);
bootstrap.childOption(ChannelOption.SO_SNDBUF, 256 * 1024);
bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, //
new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
channel = bootstrap.bind(inet).sync().channel();
System.out.println("TurboRpcServer started. Listening on: " + hostPort);
}
public void start() throws InterruptedException {
InetSocketAddress inet = new InetSocketAddress(hostPort.host, hostPort.port);
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(eventLoopGroup);
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
if (eventLoopGroup instanceof EpollEventLoopGroup) {
bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
bootstrap.channel(EpollServerSocketChannel.class);
} else if (eventLoopGroup instanceof NioEventLoopGroup) {
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.childHandler(new NettyRestChannelInitializer(invokerFactory, jsonMapper, filters));
bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
bootstrap.childOption(ChannelOption.SO_RCVBUF, 256 * 1024);
bootstrap.childOption(ChannelOption.SO_SNDBUF, 256 * 1024);
bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, //
new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
channel = bootstrap.bind(inet).sync().channel();
System.out.println("NettyRestServer started. Listening on: " + hostPort);
}
/**
* Returns the correct {@link Class} to use with the given {@link EventLoopGroup}.
*
* @param group the {@link EventLoopGroup} for which the class is needed
* @param addressClass The class of the address that the server socket will be bound to.
* @return the class that should be used for bootstrapping
*/
public static Class<? extends ServerChannel> serverChannel(EventLoopGroup group,
Class<? extends SocketAddress> addressClass) {
if (useEpoll(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? EpollServerDomainSocketChannel.class :
EpollServerSocketChannel.class;
} else if (useKQueue(group)) {
return DomainSocketAddress.class.isAssignableFrom(addressClass) ? KQueueServerDomainSocketChannel.class :
KQueueServerSocketChannel.class;
} else {
return NioServerSocketChannel.class;
}
}
public static void main(String[] args) throws InterruptedException {
if(args != null && args.length > 1) {
if(args[0].equalsIgnoreCase("-p") || args[0].equalsIgnoreCase("-port")) {
ServerContains.port = Integer.valueOf(args[1]);
}
}
EventLoopGroup parentGroup = Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
EventLoopGroup childGroup = Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(parentGroup, childGroup)
.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(ServerContains.port))
.childHandler(new DefaultChannelInitializer());
ChannelFuture f = bootstrap .bind().sync();
SimplePrinter.serverLog("The server was successfully started on port " + ServerContains.port);
ServerContains.THREAD_EXCUTER.execute(() -> {
Timer timer=new Timer();
timer.schedule(new RoomClearTask(), 0L, 3000L);
});
f.channel().closeFuture().sync();
} finally {
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
}
}
/**
* 创建epoll线程组
*/
private void createEpollGroup(int boss, int work) {
bossGroup = new EpollEventLoopGroup(boss, new DefaultThreadFactory("[email protected]"));
workGroup = new EpollEventLoopGroup(work, new DefaultThreadFactory("[email protected]"));
bootstrap.group(bossGroup, workGroup)
.channel(EpollServerSocketChannel.class);
}
protected ChannelFactory<? extends ServerChannel> newServerChannelFactory() {
ChannelFactory<? extends ServerChannel> channelFactory;
if(enableEpoll){
channelFactory = EpollServerSocketChannel::new;
}else {
channelFactory = NioServerSocketChannel::new;
}
return channelFactory;
}
private static Class<? extends ServerSocketChannel> serverSocketChannelType(
final OptimalTransport optimalTransport,
final Logger logger) {
switch (optimalTransport) {
case Epoll:
logger.debug("HttpAgent using EpollServerSocketChannel");
return EpollServerSocketChannel.class;
case NIO:
default:
logger.debug("HttpAgent using NioServerSocketChannel");
return NioServerSocketChannel.class;
}
}
/**
* 服务开启
*/
public void start() {
synchronized (waitLock) {
initEventPool();
bootstrap.group(bossGroup, workGroup)
.channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
.option(ChannelOption.SO_REUSEADDR, serverBean.isReuseaddr())
.option(ChannelOption.SO_BACKLOG, serverBean.getBacklog())
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.option(ChannelOption.SO_RCVBUF, serverBean.getRevbuf())
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) {
initHandler(ch.pipeline(), serverBean);
}
})
.childOption(ChannelOption.TCP_NODELAY, serverBean.isNodelay())
.childOption(ChannelOption.SO_KEEPALIVE, serverBean.isKeepAlive())
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
bootstrap.bind(UniqueIpUtils.getHost(), serverBean.getWebPort()).addListener((ChannelFutureListener) channelFuture -> {
if (channelFuture.isSuccess()) {
log.info("服务端启动成功【" + UniqueIpUtils.getHost() + ":" + serverBean.getWebPort() + "】");
AutoConfig.address = UniqueIpUtils.getHost() + ":" + serverBean.getWebPort();
RedisConfig.getInstance();
} else {
log.info("服务端启动失败【" + UniqueIpUtils.getHost() + ":" + serverBean.getWebPort() + "】");
}
});
}
}
private void groupsEpoll(final ServerBootstrap bootstrap, final int workThreads) {
workerGroup = new EpollEventLoopGroup(workThreads);
bootstrap.group(bossGroup, workerGroup)
.channel(EpollServerSocketChannel.class)
.option(EpollChannelOption.TCP_CORK, true)
.option(EpollChannelOption.SO_KEEPALIVE, true)
.option(EpollChannelOption.SO_BACKLOG, 100)
.option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(nettyServerHandlerInitializer);
}
public Class<? extends ServerChannel> getServerChannel() {
if (useNativeIo && Epoll.isAvailable()) {
return uds ? EpollServerDomainSocketChannel.class : EpollServerSocketChannel.class;
} else if (useNativeIo && KQueue.isAvailable()) {
return uds ? KQueueServerDomainSocketChannel.class : KQueueServerSocketChannel.class;
}
return NioServerSocketChannel.class;
}
public static void main(String... args) throws Exception {
logger.info("starting server");
String host = System.getProperty("host", "0.0.0.0");
int port = Integer.getInteger("port", 8001);
boolean useEpoll = Boolean.getBoolean("usePoll");
Class channel;
if (useEpoll) {
channel = EpollServerSocketChannel.class;
} else {
channel = NioServerSocketChannel.class;
}
ThreadFactory tf = new DefaultThreadFactory("server-elg-", true /*daemon */);
NioEventLoopGroup boss = new NioEventLoopGroup(1, tf);
NioEventLoopGroup worker = new NioEventLoopGroup(0, tf);
NettyServerBuilder builder =
NettyServerBuilder.forPort(port)
.bossEventLoopGroup(boss)
.workerEventLoopGroup(worker)
.channelType(channel)
.addService(new DefaultService())
.directExecutor()
.maxConcurrentCallsPerConnection(Runtime.getRuntime().availableProcessors() * 256)
.flowControlWindow(NettyChannelBuilder.DEFAULT_FLOW_CONTROL_WINDOW * 10);
io.grpc.Server start = builder.build();
start.start();
logger.info("server started");
start.awaitTermination();
}
public static void main(String[] args) throws InterruptedException {
if (Epoll.isAvailable()) {
doRun(//
new EpollEventLoopGroup(), //
EpollServerSocketChannel.class, //
true);
} else {
doRun(//
new NioEventLoopGroup(), //
NioServerSocketChannel.class, //
false);
}
}