多處摘抄或手打,為了十積分厚着臉皮標為原創,慚愧慚愧~本篇文章用於快速入門搭建一個簡單的netty
應用,如想稍微深入系統的了解,請參照本人下一篇博客,鏈接:
參考地址:
官方文檔:http://netty.io/wiki/user-guide-for-4.x.html
文案部分:
百度百科:https://baike.baidu.com/item/Netty/10061624?fr=aladdin
NIO:https://blog.csdn.net/skiof007/article/details/52873421
代碼部分:
https://blog.csdn.net/hangge110/article/details/51613988
更多參考:
各個對象以及常量的作用對照源碼:https://blog.csdn.net/CoffeeAndIce/article/details/78987542
源代碼學習1:https://www.cnblogs.com/katsura/p/5991428.html
源代碼學習2:https://www.cnblogs.com/stevenczp/p/7581940.html
demo02:https://blog.csdn.net/Howinfun/article/details/81283721
Netty
Netty是由JBOSS提供的一個java開源框架。Netty提供異步的、事件驅動的網絡應用程序框架和工具,用以快速開發高性能、高可靠性的網絡服務器和客戶端程序。
也就是說,Netty 是一個基於NIO的客戶、服務器端編程框架,使用Netty 可以確保你快速和簡單的開發出一個網絡應用,例如實現了某種協議的客戶、服務端應用。Netty相當於簡化和流線化了網絡應用的編程開發過程,例如:基於TCP和UDP的socket服務開發。
NIO基本概念:點擊打開鏈接
IO的方式通常分為幾種,同步阻塞的BIO、同步非阻塞的NIO、異步非阻塞的AIO......
DEMO(手打https://blog.csdn.net/hangge110/article/details/51613988):
1、服務端
package com.zx.test;
import com.lk.netty.mulchat.dome.ServerIniterHandler;
import com.lk.netty.mulchat.dome.ServerMain;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.omg.CORBA.PRIVATE_MEMBER;
/**
* 服務端生產者對象
* This is an netty Server
* @author ZX
* @date 2018年6月7日
* Netty中,通訊的雙方建立連接后,會把數據按照ByteBuf的方式進行傳輸,
* 例如http協議中,就是通過HttpRequestDecoder對ByteBuf數據流進行處理,轉換成http的對象。
* 深入學習:
* https://www.cnblogs.com/katsura/p/5991428.html
* https://www.cnblogs.com/stevenczp/p/7581940.html
*/
public class ServerTest {
/**
* 服務端口
*/
private int port=9999;
/**
* 開啟服務的方法
*/
public void StartNetty(){
/**創建兩個EventLoop的組,EventLoop 這個相當於一個處理線程,
是Netty接收請求和處理IO請求的線程。不理解的話可以百度NIO圖解*/
/*
相關資料:NioEventLoopGroup是一個處理I/O操作的多線程事件循環。
Netty為不同類型的傳輸提供了各種EventLoopGroup實現。
在本例中,我們正在實現一個服務器端應用程序,因此將使用兩個NioEventLoopGroup。
第一個,通常稱為“boss”,接受傳入的連接。
第二個,通常稱為“worker”,當boss接受連接並注冊被接受的連接到worker時,處理被接受連接的流量。
使用了多少線程以及如何將它們映射到創建的通道取決於EventLoopGroup實現,甚至可以通過構造函數進行配置。
*/
EventLoopGroup acceptor = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
//1、創建啟動類
ServerBootstrap bootstrap = new ServerBootstrap();
//2、配置啟動參數等
/**設置循環線程組,前者用於處理客戶端連接事件,后者用於處理網絡IO(server使用兩個參數這個)
*public ServerBootstrap group(EventLoopGroup group)
*public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
*/
bootstrap.group(acceptor,worker);
/**設置選項
* 參數:Socket的標准參數(key,value),可自行百度
* eg:
* bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
*bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
* */
bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
//用於構造socketchannel工廠
bootstrap.channel(NioServerSocketChannel.class);
/**
* 傳入自定義客戶端Handle(服務端在這里搞事情)
*/
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// 注冊handler
ch.pipeline().addLast(new SimpleServerHandler());
}
});
// 綁定端口,開始接收進來的連接
ChannelFuture f = bootstrap.bind(port).sync();
// 等待服務器 socket 關閉 。
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
acceptor.shutdownGracefully();
worker.shutdownGracefully();
}
}
public static void main(String[] args) {
new ServerTest().StartNetty();
}
}
2、服務端處理類
package com.zx.test;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SimpleServerHandler extends ChannelInboundHandlerAdapter {
/**
* 本方法用於讀取客戶端發送的信息
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("SimpleServerHandler.channelRead");
ByteBuf result = (ByteBuf) msg;
byte[] result1 = new byte[result.readableBytes()];
// msg中存儲的是ByteBuf類型的數據,把數據讀取到byte[]中
result.readBytes(result1);
String resultStr = new String(result1);
// 接收並打印客戶端的信息
System.out.println("Client said:" + resultStr);
// 釋放資源,這行很關鍵
result.release();
// 向客戶端發送消息
String response = "hello client!";
// 在當前場景下,發送的數據必須轉換成ByteBuf數組
ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
encoded.writeBytes(response.getBytes());
ctx.write(encoded);
ctx.flush();
}
/**
* 本方法用作處理異常
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 當出現異常就關閉連接
cause.printStackTrace();
ctx.close();
}
/**
* 信息獲取完畢后操作
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
}
3、客戶端
package com.zx.test;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* 客戶端消費者對象
* This is an netty Client
* @author ZX
* @date 2018年6月7日
*/
public class ClientTest {
public void connect(String host, int port) throws Exception {
EventLoopGroup worker = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
/**
*EventLoop的組
*/
b.group(worker);
/**
* 用於構造socketchannel工廠
*/
b.channel(NioSocketChannel.class);
/**設置選項
* 參數:Socket的標准參數(key,value),可自行百度
保持呼吸,不要斷氣!
* */
b.option(ChannelOption.SO_KEEPALIVE, true);
/**
* 自定義客戶端Handle(客戶端在這里搞事情)
*/
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new SimpleClientHandler());
}
});
/** 開啟客戶端監聽*/
ChannelFuture f = b.connect(host, port).sync();
/**等待數據直到客戶端關閉*/
f.channel().closeFuture().sync();
} finally {
worker.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
ClientTest client=new ClientTest();
client.connect("127.0.0.1", 9999);
}
}
4、客戶端處理類
package com.zx.test;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {
/**
* 本方法用於接收服務端發送過來的消息
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("SimpleClientHandler.channelRead");
ByteBuf result = (ByteBuf) msg;
byte[] result1 = new byte[result.readableBytes()];
result.readBytes(result1);
System.out.println("Server said:" + new String(result1));
result.release();
}
/**
* 本方法用於處理異常
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 當出現異常就關閉連接
cause.printStackTrace();
ctx.close();
}
/**
* 本方法用於向服務端發送信息
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String msg = "hello Server!";
ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());
encoded.writeBytes(msg.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
5、運行結果:
服務端:
SimpleServerHandler.channelRead
Client said:hello Server!
客戶端:
SimpleClientHandler.channelRead
Server said:hello client!
后話:
好了,看完整篇博文,復制代碼,跑起來了!然后你也有了解決問題的思路,不管對錯能解決問題了。但是每個對象,每個參數是什么意思能搞懂嗎?博主不才,雖然博主后來再次添加了很多注釋,但是對很多對象以及參數理解不深,也不知道自己的用法是否是出於設計者本意!
所以,下一篇博主會稍微深入一些介紹對象以及使用,會讓你更加了解自己使用的這個NIO中間件工具,若是想成為專家大牛,就自己去下載源碼分析吧~博主目前在拓展知識體系階段,還沒有如此多精力去深入,這個需要很多技巧。
廢話不多說了,下一篇地址:https://blog.csdn.net/the_fool_/article/details/80623299