Netty+SpringBoot+FastDFS+Html5实现聊天App,项目介绍:https://segmentfault.com/a/11...javascript
Netty+SpringBoot+FastDFS+Html5实现聊天App,项目github连接:https://github.com/ShimmerPig...html
本章练习完整代码连接:https://github.com/ShimmerPig...java
IO编程模型在客户端较少的状况下运行良好,可是对于客户端比较多的业务来讲,单机服务端可能须要支撑成千上万的链接,IO模型可能就不太合适了。这是由于在传统的IO模型中,每一个链接建立成功以后都须要一个线程来维护,每一个线程包含一个while死循环,那么1w个链接对应1w个线程,继而1w个while死循环,这就带来以下几个问题:git
1.线程资源受限:线程是操做系统中很是宝贵的资源,同一时刻有大量的线程处于阻塞状态是很是严重的资源浪费,操做系统耗不起。github
2.线程切换效率低下:单机cpu核数固定,线程爆炸以后操做系统频繁进行线程切换,应用性能急剧降低。web
3.除了以上两个问题,IO编程中,咱们看到数据读写是以字节流为单位,效率不高。编程
为了解决这三个问题,JDK在1.4以后提出了NIO。下面简单描述一下NIO是如何解决以上三个问题的。segmentfault
NIO编程模型中,新来一个链接再也不建立一个新的线程,而是能够把这条链接直接绑定到某个固定的线程,而后这条链接全部的读写都由这个线程来负责。
这个过程的实现归功于NIO模型中selector的做用,一条链接来了以后,如今不建立一个while死循环去监听是否有数据可读了,而是直接把这条链接注册到selector上,而后,经过检查这个selector,就能够批量监测出有数据可读的链接,进而读取数据。浏览器
因为NIO模型中线程数量大大下降,线程切换效率所以也大幅度提升。服务器
NIO解决这个问题的方式是数据读写再也不以字节为单位,而是以字节块为单位。IO模型中,每次都是从操做系统底层一个字节一个字节地读取数据,而NIO维护一个缓冲区,每次能够从这个缓冲区里面读取一块的数据。
完整代码连接:https://github.com/ShimmerPig...
首先定义一对线程组——主线程bossGroup与从线程workerGroup。
bossGroup——用于接受客户端的链接,可是不作任何处理,跟老板同样,不作事。
workerGroup——bossGroup会将任务丢给他,让workerGroup去处理。
//主线程 EventLoopGroup bossGroup = new NioEventLoopGroup(); //从线程 EventLoopGroup workerGroup = new NioEventLoopGroup();
定义服务端的启动类serverBootstrap,须要设置主从线程,NIO的双向通道,与子处理器(用于处理workerGroup),这里的子处理器后面咱们会手动建立。
// netty服务器的建立, ServerBootstrap 是一个启动类 ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) // 设置主从线程组 .channel(NioServerSocketChannel.class) // 设置nio的双向通道 .childHandler(new HelloServerInitializer()); // 子处理器,用于处理workerGroup
启动服务端,绑定8088端口,同时设置启动的方式为同步的,这样咱们的Netty就会一直等待,直到该端口启动完毕。
ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
监听关闭的通道channel,设置为同步方式。
channelFuture.channel().closeFuture().sync();
将两个线程优雅地关闭。
bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully();
建立管道channel的子处理器HelloServerInitializer,用于处理workerGroup。
HelloServerInitializer里面只重写了initChannel方法,是一个初始化器,channel注册后,会执行里面相应的初始化方法。
在initChannel方法中经过SocketChannel得到对应的管道,经过该管道添加相关助手类handler。
HttpServerCodec是由netty本身提供的助手类,能够理解为拦截器,当请求到服务端,咱们须要作解码,响应到客户端作编码。
添加自定义的助手类customHandler,返回"hello netty~"
ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("HttpServerCodec", new HttpServerCodec()); pipeline.addLast("customHandler", new CustomHandler());
建立自定义的助手类CustomHandler继承SimpleChannelInboundHandler,返回hello netty~
重写channelRead0方法,首先经过传入的上下文对象ChannelHandlerContext获取channel,若消息类型为http请求,则构建一个内容为"hello netty~"的http响应,经过上下文对象的writeAndFlush方法将响应刷到客户端。
if (msg instanceof HttpRequest) { // 显示客户端的远程地址 System.out.println(channel.remoteAddress()); // 定义发送的数据消息 ByteBuf content = Unpooled.copiedBuffer("Hello netty~", CharsetUtil.UTF_8); // 构建一个http response FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); // 为响应增长数据类型和长度 response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain"); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); // 把响应刷到客户端 ctx.writeAndFlush(response); }
访问8088端口,返回"hello netty~"
完整代码连接:https://github.com/ShimmerPig...
定义主从线程与服务端的启动类
public class WSServer { public static void main(String[] args) throws Exception { EventLoopGroup mainGroup = new NioEventLoopGroup(); EventLoopGroup subGroup = new NioEventLoopGroup(); try { ServerBootstrap server = new ServerBootstrap(); server.group(mainGroup, subGroup) .channel(NioServerSocketChannel.class) .childHandler(new WSServerInitialzer()); ChannelFuture future = server.bind(8088).sync(); future.channel().closeFuture().sync(); } finally { mainGroup.shutdownGracefully(); subGroup.shutdownGracefully(); } } }
建立channel的子处理器WSServerInitialzer
加入相关的助手类handler
public class WSServerInitialzer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // websocket 基于http协议,因此要有http编解码器 pipeline.addLast(new HttpServerCodec()); // 对写大数据流的支持 pipeline.addLast(new ChunkedWriteHandler()); // 对httpMessage进行聚合,聚合成FullHttpRequest或FullHttpResponse // 几乎在netty中的编程,都会使用到此hanler pipeline.addLast(new HttpObjectAggregator(1024*64)); // ====================== 以上是用于支持http协议 ====================== // ====================== 如下是支持httpWebsocket ====================== /** * websocket 服务器处理的协议,用于指定给客户端链接访问的路由 : /ws * 本handler会帮你处理一些繁重的复杂的事 * 会帮你处理握手动做: handshaking(close, ping, pong) ping + pong = 心跳 * 对于websocket来说,都是以frames进行传输的,不一样的数据类型对应的frames也不一样 */ pipeline.addLast(new WebSocketServerProtocolHandler("/ws")); // 自定义的handler pipeline.addLast(new ChatHandler()); } }
建立自定义的助手类ChatHandler,用于处理消息。
TextWebSocketFrame:在netty中,是用于为websocket专门处理文本的对象,frame是消息的载体。
建立管道组ChannelGroup,用于管理全部客户端的管道channel。
private static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
重写channelRead0方法,经过传入的TextWebSocketFrame获取客户端传入的内容。经过循环的方法对ChannelGroup中全部的channel进行回复。
@Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { // 获取客户端传输过来的消息 String content = msg.text(); System.out.println("接受到的数据:" + content); // for (Channel channel: clients) { // channel.writeAndFlush( // new TextWebSocketFrame( // "[服务器在]" + LocalDateTime.now() // + "接受到消息, 消息为:" + content)); // } // 下面这个方法,和上面的for循环,一致 clients.writeAndFlush( new TextWebSocketFrame( "[服务器在]" + LocalDateTime.now() + "接受到消息, 消息为:" + content)); }
重写handlerAdded方法,当客户端链接服务端以后(打开链接),获取客户端的channle,而且放到ChannelGroup中去进行管理。
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { clients.add(ctx.channel()); }
重写handlerRemoved方法,当触发handlerRemoved,ChannelGroup会自动移除对应客户端的channel。
@Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { // 当触发handlerRemoved,ChannelGroup会自动移除对应客户端的channel // clients.remove(ctx.channel()); System.out.println("客户端断开,channle对应的长id为:" + ctx.channel().id().asLongText()); System.out.println("客户端断开,channle对应的短id为:" + ctx.channel().id().asShortText()); }
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <div>发送消息:</div> <input type="text" id="msgContent"/> <input type="button" value="点我发送" onclick="CHAT.chat()"/> <div>接受消息:</div> <div id="receiveMsg" style="background-color: gainsboro;"></div> <script type="application/javascript"> window.CHAT = { socket: null, init: function() { if (window.WebSocket) { CHAT.socket = new WebSocket("ws://192.168.1.4:8088/ws"); CHAT.socket.onopen = function() { console.log("链接创建成功..."); }, CHAT.socket.onclose = function() { console.log("链接关闭..."); }, CHAT.socket.onerror = function() { console.log("发生错误..."); }, CHAT.socket.onmessage = function(e) { console.log("接受到消息:" + e.data); var receiveMsg = document.getElementById("receiveMsg"); var html = receiveMsg.innerHTML; receiveMsg.innerHTML = html + "<br/>" + e.data; } } else { alert("浏览器不支持websocket协议..."); } }, chat: function() { var msg = document.getElementById("msgContent"); CHAT.socket.send(msg.value); } }; CHAT.init(); </script> </body> </html>