netty4.0.17 提供了基于c的epoll 采用ET工作模式 下面直接分享 基于接口 使用这一特性 目前已经替换在内网测试服务器上面 运行良好
以下是简单的替换:
public void runEpollProxy() throws Exception {
// Configure the bootstrap.
EpollEventLoopGroup BossEventLoopGroup=new EpollEventLoopGroup(0x1,new PriorityThreadFactory("@+监听连接线程",Thread.NORM_PRIORITY)); //mainReactor 1个线程
EpollEventLoopGroup WorkerEventLoopGroup=new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors()+ 0x1,new PriorityThreadFactory("@+I/O线程",Thread.NORM_PRIORITY)); //subReactor 线程数量等价于cpu个数+1
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(BossEventLoopGroup, WorkerEventLoopGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true) //重用地址
.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better
.childOption(ChannelOption.SO_RCVBUF, 1048576)
.childOption(ChannelOption.SO_SNDBUF, 1048576)
.childHandler(new ProxyServerChannelInitializer(remoteHost, remotePort)) ;
ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(localPort)).sync();
LOGGER.debug("proxy server监听端口:"+ localPort);
channelFuture.channel().closeFuture().sync();
} finally {
BossEventLoopGroup.shutdownGracefully();
WorkerEventLoopGroup.shutdownGracefully();
}
}
public void runProxy() throws Exception {
// Configure the bootstrap.
NioEventLoopGroup BossEventLoopGroup=new NioEventLoopGroup(0x1,new PriorityThreadFactory("@+监听连接线程",Thread.NORM_PRIORITY)); //mainReactor 1个线程
NioEventLoopGroup WorkerEventLoopGroup=new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()+ 0x1,new PriorityThreadFactory("@+I/O线程",Thread.NORM_PRIORITY)); //subReactor 线程数量等价于cpu个数+1
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(BossEventLoopGroup, WorkerEventLoopGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true) //重用地址
.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better
.childOption(ChannelOption.SO_RCVBUF, 1048576)
.childOption(ChannelOption.SO_SNDBUF, 1048576)
.childHandler(new ProxyServerChannelInitializer(remoteHost, remotePort)) ;
ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(localPort)).sync();
LOGGER.debug("proxy server监听端口:"+ localPort);
channelFuture.channel().closeFuture().sync();
} finally {
BossEventLoopGroup.shutdownGracefully();
WorkerEventLoopGroup.shutdownGracefully();
}
}
public class PriorityThreadFactory implements ThreadFactory {
private int _prio;
private String _name;
private AtomicInteger _threadNumber = new AtomicInteger(1);
private ThreadGroup _group;
/**
*
* @param name 线程池名
* @param priority 线程池优先级
*/
public PriorityThreadFactory(String name, int priority){
_prio = priority;
_name = name;
_group = new ThreadGroup(_name);
}
@Override
public Thread newThread(Runnable r){
Thread t = new Thread(_group, r);
t.setName(_name + "-"+"#-" + _threadNumber.getAndIncrement());
t.setPriority(_prio);
return t;
}
public ThreadGroup getGroup(){
return _group;
}
}
环境配置:
## RHEL/CentOS/Fedora:
#sudo yum install autoconf automake libtool glibc-devel.i686 glibc-devel libgcc.i686 make
## Debian/Ubuntu:
#sudo apt-get install autoconf automake libtool make gcc-multilib
就这样 就可以咯 有问题留言