(1)netty是一主多从 多线程模型
(2)netty一个基本的demo
package io.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
/**
* 自定义一个handler 需要继续 Netty规定好的某个Handler实例器
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
/**
* 读取客户端的数据
* @param ctx 可以获取 pipeline socketChannel 地址
* @param msg 客户端发送的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//case2
//这个里面有很耗时的业务操作 异步执行,提交到该channel对应的NioEventLoopGroup 的 taskQueue
//针对耗时的事件 需要放到队列中执行
//异步提交 不阻塞
// taskQueue使用同一个线程进行异步执行任务
ctx.channel().eventLoop().execute(()->{
try {
Thread.sleep(100*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端",CharsetUtil.UTF_8));
});
//针对耗时的事件 需要放到队列中执行
//异步提交 不阻塞
ctx.channel().eventLoop().execute(()->{
try {
Thread.sleep(200*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端",CharsetUtil.UTF_8));
});
//用户自定义定时任务提交到 提交到该channel对应的NioEventLoopGroup 的 scheduleTaskQueue
ctx.channel().eventLoop().schedule(()->{
try {
Thread.sleep(500*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端",CharsetUtil.UTF_8));
},5, TimeUnit.SECONDS);
//case1
System.out.println("服务器读取线程"+Thread.currentThread().getName());
System.out.println("server ctx="+ctx);
System.out.println("server channel="+ctx.channel());
System.out.println("server pipeline="+ctx.pipeline());
//将msg 转换为 ByteBuf
ByteBuf byteBuffer=(ByteBuf)msg;
System.out.println("客户端发送消息为:"+byteBuffer.toString(CharsetUtil.UTF_8));
System.out.println("客户端地址为:"+ctx.channel().remoteAddress());
}
/**
*
* 数据读取完毕回调
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//将数据写入到缓冲并刷新
//一般对发送的数据进行编码
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端",CharsetUtil.UTF_8));
}
/**
* 发生异常如何处理
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 将通道进行关闭
ctx.close();
}
}
package io.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.nio.charset.Charset;
/**
* 自定义一个handler 需要继续 Netty规定好的某个Handler实例器
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
/**
* 读取客户端的数据
* @param ctx 可以获取 pipeline socketChannel 地址
* @param msg 客户端发送的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("server ctx="+ctx);
//将msg 转换为 ByteBuf
ByteBuf byteBuffer=(ByteBuf)msg;
System.out.println("客户端发送消息为:"+byteBuffer.toString(CharsetUtil.UTF_8));
System.out.println("客户端地址为:"+ctx.channel().remoteAddress());
}
/**
*
* 数据读取完毕回调
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
//将数据写入到缓冲并刷新
//一般对发送的数据进行编码
ctx.writeAndFlush(Unpooled.copiedBuffer("hello 客户端",CharsetUtil.UTF_8));
}
/**
* 发生异常如何处理
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 将通道进行关闭
ctx.close();
}
}
package io.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
/**
* 客户端与服务端连接成功
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client"+ctx);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server 小麦", CharsetUtil.UTF_8));
}
/**
* 读取服务端返回的消息
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf=(ByteBuf)msg;
System.out.println("server say "+ new String(byteBuf.toString(CharsetUtil.UTF_8)));
System.out.println("server address"+ctx.channel().remoteAddress());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
package io.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
//客户端需要一个事件循环组
NioEventLoopGroup boosGroup=new NioEventLoopGroup(1);
//创建一个客户端启动对象
Bootstrap bootstrap=new Bootstrap();
//相关参数设置
bootstrap.group(boosGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyClientHandler());
}
});
System.out.println("client is ok");
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
channelFuture.channel().closeFuture().sync();
boosGroup.shutdownGracefully();
}
}