【Netty整理01-快速入門】Netty簡單使用Demo(已驗證)
多處摘抄或手打,為了十積分厚著臉皮標為原創,慚愧慚愧~本篇文章用於快速入門搭建一個簡單的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
Netty
Netty是由JBOSS提供的一個java開源框架。Netty提供非同步的、事件驅動的網路應用程式框架和工具,用以快速開發高效能、高可靠性的網路伺服器和客戶端程式。
也就是說,Netty 是一個基於NIO的客戶、伺服器端程式設計框架,使用Netty 可以確保你快速和簡單的開發出一個網路應用,例如實現了某種協議的客戶、
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