`
flychao88
  • 浏览: 743563 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用JAVA操作netty框架

 
阅读更多
之前使用过MINA框架,感觉效率非常好,使用长连接可以支持10万次以上的并发。
今天尝试使用了Netty框架,感觉使用上也非常方便,具体效率问题,在接下来的博客会详细解读:

NioServerSocketChannelFactory创建服务端的ServerSocketChannel,采用多线程执行非阻塞IO,和Mina的设计

模式一样,都采用了Reactor模式。其中bossExecutor、workerExecutor是两个线程池,bossExecutor用来接收客户端连接,workerExecutor用来执行非阻塞的IO操作,主要是read,write。





package netty;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

/**
 * Created by IntelliJ IDEA.
 * User: flychao88
 * Date: 12-6-6
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class DiscardServer {
    public static void main(String[] args) throws Exception {
        ChannelFactory factory = new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
        ServerBootstrap bootstrap = new ServerBootstrap (factory);
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                 ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("encode",new StringEncoder());
                pipeline.addLast("decode",new StringDecoder());
                pipeline.addLast("handler",new DiscardServerHandler());
                return pipeline;
            }
        });
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.bind(new InetSocketAddress(8080));
    }
}


package netty;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;

/**
 * Created by IntelliJ IDEA.
 * User: flychao88
 * Date: 12-6-6
 * Time: 上午10:10
 * To change this template use File | Settings | File Templates.
 */
public class DiscardServerHandler extends SimpleChannelUpstreamHandler  {
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
       System.out.println("服务器接收1:"+e.getMessage());
    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();
        Channel ch = e.getChannel();
        ch.close();
    }
}




package netty;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

/**
 * Created by IntelliJ IDEA.
 * User: flychao88
 * Date: 12-6-6
 * Time: 上午10:21
 * To change this template use File | Settings | File Templates.
 */
public class TimeClient {
    public static void main(String[] args) throws Exception {
        
        ChannelFactory factory = new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
        ClientBootstrap bootstrap = new ClientBootstrap(factory);
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("encode",new StringEncoder());
                pipeline.addLast("decode",new StringDecoder());
                pipeline.addLast("handler",new TimeClientHandler());
                return pipeline;
            }
        });
        bootstrap.setOption("tcpNoDelay" , true);
        bootstrap.setOption("keepAlive", true);
        bootstrap.connect (new InetSocketAddress("127.0.0.1", 8080));
    }
}




package netty;

/**
 * Created by IntelliJ IDEA.
 * User: flychao88
 * Date: 12-6-6
 * Time: 上午10:22
 * To change this template use File | Settings | File Templates.
 */
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;

import java.util.Date;


public class TimeClientHandler extends SimpleChannelUpstreamHandler  {
    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        e.getChannel().write("abcd");
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        e.getChannel().close();
    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();
        e.getChannel().close();
    }
}




分享到:
评论
15 楼 萨琳娜啊 2018-07-10  
Java读源码之Netty深入剖析
网盘地址:https://pan.baidu.com/s/11kYm2fhJg5CkxIkv0Etvbw 密码: kipg
备用地址(腾讯微云):https://share.weiyun.com/5Bs3HcR 密码:uu95be

JavaCoder如果没有研究过Netty,那么你对Java语言的使用和理解仅仅停留在表面水平,如果你要进阶,想了解Java服务器的深层高阶知识,Netty绝对是一个必须要过的门槛。

本课程带你从一个Socket例子入手,一步步深入探究Netty各个模块的源码,深入剖析Netty的工作流程和源码设计,让你不但“真懂”也要“会用”
14 楼 小灯笼 2018-06-06  
Netty源码剖析视频教程
网盘地址:https://pan.baidu.com/s/1bvqEWNLisEcJ6OCHYhlcsw 密码: f4e4
备用地址(腾讯微云):http://url.cn/5R2xYI4 密码:OmbopD
13 楼 heng123 2017-10-03  
Netty视频教程https://www.douban.com/note/619228582/
12 楼 kongdong88 2017-08-16  
Netty简单应用与线上服务器部署
课程学习地址:http://www.xuetuwuyou.com/course/198
课程出自学途无忧网:http://www.xuetuwuyou.com

一、开发环境
4.1.11.Final 
jdk1.8
maven 3.2
Spring 4.3.9


二、适合人群
①想深入学习java ClassLoader
②想在线上linux服务器上运行netty或Springboot服务

三、课程目标
①掌控ClassLoader
②学会编写shell脚本
③了解jvm内存分配
④熟悉线上服务器部署


四、课程目录
1、系统架构技术介绍
2、netty服务器编写
3、netty的编码和解码
4、Netty客户端编写
5、与spring整合
6、netty服务器待解决的问题
7、Classloader类加载器
8、自定义类加载器
9、第二种类加载器
10、热部署
11、类加载器加载外部配置文件
12、项目运行时加载依赖jar
13、项目运行时加载依赖jar第二种方案
14、linux服务器编写通用shell脚本启动JVM
15、优化Shell脚本
16、Shell脚本中加上JVM堆栈内存参数以及垃圾回收机制
17、Netty服务器加上配置信息




Netty课程整合推荐:

Netty简单应用与线上服务器部署
课程观看地址:http://www.xuetuwuyou.com/course/198

深入浅出Netty源码剖析
课程观看地址:http://www.xuetuwuyou.com/course/157

Netty实战高性能分布式RPC
课程观看地址:http://www.xuetuwuyou.com/course/171

NIO+Netty5各种RPC架构实战演练
课程观看地址:http://www.xuetuwuyou.com/course/52

物联网核心技术之Netty入门到精通课程
课程观看地址:http://www.xuetuwuyou.com/course/14

Netty三部曲,夜行侠老师带你玩转netty
课程观看地址:http://www.xuetuwuyou.com/course/175

Netty物联网高并发系统第一季
课程观看地址:http://www.xuetuwuyou.com/course/178
11 楼 smart152829 2017-07-01  

很不错的文章,最近看了看了夜行侠老师讲的Netty深入浅出源码剖析,里面用到了长连接高并发,在测试的时候,性能真的比之前的tomcat那些容器好几十倍
10 楼 xingxing 2016-11-18  
有jar包吗?
9 楼 darren_nizna 2016-09-07  
http://gitlore.com/darren/netty_in_action/index.html  Netty 实战(精髓)
8 楼 ustcdqk 2016-01-21  
[flash=200,200][/flash]
7 楼 Balena 2015-11-28  
NIO+Netty5各种RPC架构实战演练,我讲了一堂比较深入的视频,叫NIO+Netty5各种RPC架构实战演练,想要的小伙伴,可以加群511029656,也可以直接进入www.xuetuwuyou.com/course/52直接观看
6 楼 紫皇林 2015-07-10  
5 楼 逃荒形象 2015-02-08  
为什么服务端和客户端都启动后,服务端那边的消息是server ,客户端的client,服务端和客户端收到的是自己的。
4 楼 Jong88 2013-04-06  
flychao88 写道
ljl_ss 写道
貌似NioClientSocketChannelFactory和NioServerSocketChannelFactory位置放反了

哪里反了?这个我还测试过??望指教

类图关系反了
3 楼 flychao88 2012-08-09  
ljl_ss 写道
貌似NioClientSocketChannelFactory和NioServerSocketChannelFactory位置放反了

哪里反了?这个我还测试过??望指教
2 楼 flychao88 2012-08-09  
哪里反了?这个我还测试过?指教啊
1 楼 ljl_ss 2012-07-20  
貌似NioClientSocketChannelFactory和NioServerSocketChannelFactory位置放反了

相关推荐

Global site tag (gtag.js) - Google Analytics