標籤:style blog http io os ar java strong sp
既然是入門,那我們就在這裡寫一個簡單的Demo,client發送一個字串到server端,server端接收字串後再發送回client。
2.1、配置開發環境
1.安裝JDK2.去官網下載jar包(或者通過pom構建)2.2、認識下Netty的Client和Server 一個Netty應用程式模型,例如以所看到的,但須要明確一點的是,我們寫的Server會自己主動處理多client請求,理論上講,處理並發的能力決定於我們的系統配置及JDK的極限。
- Client串連到Server端
- 建立連結發送/接收資料
- Server端處理全部Client請求
這裡有一個形象的比喻來形容Nettyclient和server端的互動模式,比方把你比作一個Client,把山比作一個Server,你走到山旁,就是和山建立了連結,你向山大喊了一聲,就代表向山發送了資料,你的喊聲經過山的反射形成了回聲,這個回聲就是server的響應資料。假設你離開,就代表斷開了連結,當然你也能夠再回來。好多人能夠同一時候向山大喊,他們的喊聲也一定會得到山的回應。2.3 寫一個Netty Server 一個NettyServer程式主要由兩部分組成:
- BootsTrapping:配置server端基本資料
- ServerHandler:真正的商務邏輯處理
2.3.1 BootsTrapping的過程:
package NettyDemo.echo.server;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;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 java.net.InetSocketAddress;import NettyDemo.echo.handler.EchoServerHandler;public class EchoServer {private static final int port = 8080;public void start() throws InterruptedException {ServerBootstrap b = new ServerBootstrap();// 引導輔助程式EventLoopGroup group = new NioEventLoopGroup();// 通過nio方式來接收串連和處理串連try {b.group(group);b.channel(NioServerSocketChannel.class);// 設定nio類型的channelb.localAddress(new InetSocketAddress(port));// 設定監聽連接埠b.childHandler(new ChannelInitializer<SocketChannel>() {//有串連到達時會建立一個channelprotected void initChannel(SocketChannel ch) throws Exception {// pipeline管理channel中的Handler,在channel隊列中加入一個handler來處理業務ch.pipeline().addLast("myHandler", new EchoServerHandler());}});ChannelFuture f = b.bind().sync();// 配置完畢,開始綁定server,通過調用sync同步方法堵塞直到綁定成功System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());f.channel().closeFuture().sync();// 應用程式會一直等待,直到channel關閉} catch (Exception e) {e.printStackTrace();} finally {group.shutdownGracefully().sync();//關閉EventLoopGroup,釋放掉全部資源套件含建立的線程}}public static void main(String[] args) {try {new EchoServer().start();} catch (InterruptedException e) {e.printStackTrace();}}} 1. 建立一個ServerBootstrap執行個體
2. 建立一個EventLoopGroup來處理各種事件,如處理連結請求,發送接收資料等。
3. 定義本地InetSocketAddress( port)好讓Server綁定
4. 建立childHandler來處理每個連結請求
5. 全部準備好之後調用ServerBootstrap.bind()方法綁定Server
2.3.2 商務邏輯ServerHandler:
要想處理接收到的資料,我們必須繼承ChannelInboundHandlerAdapter介面,重寫裡面的MessageReceive方法,每當有資料到達,此方法就會被調用(通常是Byte類型數組),我們就在這裡寫我們的商務邏輯:
package NettyDemo.echo.handler;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.channel.ChannelHandler.Sharable;/** * Sharable表示此對象在channel間共用 * handler類是我們的詳細業務類 * */@Sharable//註解@Sharable能夠讓它在channels間共用public class EchoServerHandler extends ChannelInboundHandlerAdapter{public void channelRead(ChannelHandlerContext ctx, Object msg) { System.out.println("server received data :" + msg); ctx.write(msg);//寫回資料,} public void channelReadComplete(ChannelHandlerContext ctx) { ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) //flush掉全部寫回的資料.addListener(ChannelFutureListener.CLOSE); //當flush完畢後關閉channel} public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) { cause.printStackTrace();//捕捉異常資訊ctx.close();//出現異常時關閉channel } }
2.3.3關於異常處理:
我們在上面程式中也重寫了exceptionCaught方法,這裡就是對當異常出現時的處理。
2.4 寫一個Netty Client
一般一個簡單的Client會扮演例如以下角色:
- 串連到Server
- 向Server寫資料
- 等待Server返回資料
- 關閉串連
4.4.1 BootsTrapping的過程:
和Server端類似,僅僅只是Client端要同一時候指定串連主機的IP和Port。
package NettyDemo.echo.client;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import java.net.InetSocketAddress;import NettyDemo.echo.handler.EchoClientHandler;public class EchoClient {private final String host;private final int port;public EchoClient(String host, int port) {this.host = host;this.port = port;}public void start() throws Exception {EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group);b.channel(NioSocketChannel.class);b.remoteAddress(new InetSocketAddress(host, port));b.handler(new ChannelInitializer<SocketChannel>() {public void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new EchoClientHandler());}});ChannelFuture f = b.connect().sync();f.addListener(new ChannelFutureListener() {public void operationComplete(ChannelFuture future) throws Exception {if(future.isSuccess()){System.out.println("client connected");}else{System.out.println("server attemp failed");future.cause().printStackTrace();}}});f.channel().closeFuture().sync();} finally {group.shutdownGracefully().sync();}}public static void main(String[] args) throws Exception {new EchoClient("127.0.0.1", 3331).start();}} 1. 建立一個ServerBootstrap執行個體
2. 建立一個EventLoopGroup來處理各種事件,如處理連結請求,發送接收資料等。
3. 定義一個遠程InetSocketAddress好讓client串連
4. 當串連完畢之後,Handler會被運行一次
5. 全部準備好之後調用ServerBootstrap.connect()方法串連Server4.4.2 商務邏輯ClientHandler:
我們相同繼承一個SimpleChannelInboundHandler來實現我們的Client,我們須要重寫當中的三個方法:
package NettyDemo.echo.handler;import io.netty.buffer.ByteBuf;import io.netty.buffer.ByteBufUtil;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.ChannelHandler.Sharable;import io.netty.util.CharsetUtil;@Sharablepublic class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {/** *此方法會在串連到server後被調用 * */public void channelActive(ChannelHandlerContext ctx) {ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));}/** *此方法會在接收到server資料後調用 * */public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {System.out.println("Client received: " + ByteBufUtil.hexDump(in.readBytes(in.readableBytes())));}/** *捕捉到異常 * */public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {cause.printStackTrace();ctx.close();}}
當中須要注意的是channelRead0()方法,此方法接收到的可能是一些資料片段,比方server發送了5個位元組資料,Client端不能保證一次所有收到,比方第一次收到3個位元組,第二次收到2個位元組。我們可能還會關心收到這些片段的順序是否可發送順序一致,這要看詳細是什麼協議,比方基於TCP協議的位元組流是能保證順序的。 另一點,在Client端我們的業務Handler繼承的是SimpleChannelInboundHandler,而在server端繼承的是ChannelInboundHandlerAdapter,那麼這兩個有什麼差別呢?最基本的差別就是SimpleChannelInboundHandler在接收到資料後會自己主動release掉資料佔用的Bytebuffer資源(自己主動調用Bytebuffer.release())。而為何server端不能用呢,由於我們想讓server把client請求的資料發送回去,而server端有可能在channelRead方法返回前還沒有寫完資料,因此不能讓它自己主動release。
Netty4具體解釋二:開發第一個Netty應用程式