netty jni epoll

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
就这样 就可以咯 有问题留言






转载于:https://my.oschina.net/chenleijava/blog/203005

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值