Netty框架:
Netty是一个基于JAVA NIO类库的异步通讯框架,它的架构特点是异步非阻塞,基于事件驱动,高性能,高可靠性和高可定制性。
Netty应用场景:
1.分布式开源框架dubbo,Zookeeper,RocketMQ底层rpc通讯使用就是netty。
2.游戏开发中,底层是用netty通讯。
为啥子选择netty:
NIO的类库和api繁杂,使用麻烦。需要熟悉掌握Selector,ServerSocketChannel SocketChannel ByteBuffer等;
需要具备其他额外的技能做铺垫,多线程编程,因为NIO编程涉及到Reactor模式,必须对多线程和网络编程非常熟悉,才能写出高质量的NIO程序。
可靠性能力补齐,工作量和难度非常大,例如客户端面临断链重连,网络闪断,半包读写,失败缓存,网络拥塞和异常码流的处理等等,NIO编程的特点即使功能开发相对容易,但是可靠性能力补齐工作量和难度非常大。
解决JDK的bug 。
Netty服务器端:
package com.itmayiedu.day04;
import org.jboss.netty.bootstrap.ServerBootstrap;
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.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.ExecutorService;
import java.util.concurrent.Executors;
/**
* Netty服务器端
*/
public class NettyDem01 {
public static void main(String[] args) {
test01();
}
public static void test01(){
//创建服务对象
ServerBootstrap serverBootstrap = new ServerBootstrap();
//创建两个线程池 一个监听端口号 一个监听NIO
ExecutorService boos = Executors.newCachedThreadPool();
ExecutorService work = Executors.newCachedThreadPool();
//线程池放入到工程组中
serverBootstrap.setFactory(new NioServerSocketChannelFactory(boos,work));
//谁管道工程
serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
//设置管道
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast("serverHandler",new ServerHandler());
return pipeline;
}
});
//绑定端口号
serverBootstrap.bind(new InetSocketAddress(8990));
System.out.println("Netty服务端启动");
while (true){
try{
Thread.sleep(1000);
System.out.println("每隔1秒打印一次");
}catch (Exception ex){
}
}
}
}
package com.itmayiedu.day04;
import org.jboss.netty.channel.*;
public class ServerHandler extends SimpleChannelHandler {
//接受客户端数据
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
System.out.println("================>>>>>>>>>+messageReceived");
System.out.println("================>>>>>>>>服务端的数据:"+e.getMessage());
}
//接受出现的异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
}
//必须建立连接,关闭通道是才会触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
}
//通道别关闭的时候触发
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
}
}
Netty客户端代码:
package com.itmayiedu.day04;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
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.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Netty 客户端
*/
public class NettyClient {
public static void main(String[] args) {
ClientBootstrap clientBootstrap= new ClientBootstrap();
ExecutorService book = Executors.newCachedThreadPool();
ExecutorService wook = Executors.newCachedThreadPool();
clientBootstrap.setFactory(new NioClientSocketChannelFactory(book,wook));
clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast("clientHandler",new ClientHandler());
return pipeline;
}
});
//绑定端口号
ChannelFuture connect = clientBootstrap.connect(new InetSocketAddress("127.0.0.1", 8990));
System.out.println("====================>>>>>>>>>>客户端已经启动。。");
Channel channel = connect.getChannel();
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("===============>>>>>>>>>>>>>>请输入内容");
channel.write(scanner.next());
}
}
}
ClientHandler
package com.itmayiedu.day04;
import org.jboss.netty.channel.*;
public class ClientHandler extends SimpleChannelHandler {
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelClosed(ctx, e);
}
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
System.out.println("=================>>>>>>>>:客户端发送的数据:"+e.getMessage());
ctx.getChannel().write("你好啊");
}
}
`` `
io和NIO区别:
NIO面向缓冲区,非阻塞(是通过选择器实现的)
IO面向流,是阻塞的。