搭建第一個Netty服務(wù)器和客戶端

搭建第一個Netty應(yīng)用程序

本篇文章主要記錄如何搭建一個簡單的Netty服務(wù)器和客戶端。

依賴環(huán)境:

  • Java 8
  • Netty4
<dependencies>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.11.Final</version>
    </dependency>
</dependencies>

編寫Echo服務(wù)器

所有的Netty服務(wù)器都需要以下兩個部分。

  • 至少一個ChannelHandler:用來處理業(yè)務(wù)邏輯
  • 引導(dǎo):配置服務(wù)器啟動代碼,將服務(wù)器綁定到監(jiān)聽連接請求的端口上。

EchoServerHandler類:用來處理核心業(yè)務(wù)邏輯

@ChannelHandler.Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter{

    @Override
    public void channelRead(ChannelHandlerContext context, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("server received: " + in.toString(CharsetUtil.UTF_8));
        context.write(in);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext context, Throwable couse) {
        couse.printStackTrace();
        context.close();
    }
}

EchoServer類:配置服務(wù)器,并將入站消息通知給EchoServerHandler實例。

public class EchoServer {

    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        System.err.println("Usage: " + EchoServer.class.getSimpleName() + "<port>");
        int port = 8999;
        new EchoServer(port).start();
    }

    public void start() throws Exception{
        final EchoServerHandler serverHandler = new EchoServerHandler();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group).channel(NioServerSocketChannel.class)
             .localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    socketChannel.pipeline().addLast(serverHandler);
                }
            });
            ChannelFuture future = b.bind().sync();
            future.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
}

服務(wù)器主要的代碼組件

  • EchoServerHandler實現(xiàn)業(yè)務(wù)邏輯
  • main()引導(dǎo)服務(wù)器

引導(dǎo)過程:

  • 創(chuàng)建一個ServerBootstap實例以引導(dǎo)和綁定服務(wù)器;
  • 創(chuàng)建并分配一個NioEventLoopGroup實例以進行事件的處理,如接受新連接以及讀寫數(shù)據(jù);
  • 指定服務(wù)器綁定的本地的InetSocketAddress;
  • 使用一個Handler實例初始化新的Channel;
  • 調(diào)用ServerBootstap.bind()方法以綁定服務(wù)器;

編寫Echo客戶端

Echo客戶端:

  1. 連接服務(wù)器
  2. 發(fā)送一個或多個消息
  3. 對于每個消息,等待并接受從服務(wù)器發(fā)回的相同的消息
  4. 關(guān)閉連接

ChannelHandler類:實現(xiàn)客戶端邏輯

  • channelActive()和服務(wù)器建立連接后調(diào)用
  • channelRead()從服務(wù)器接受到一條信息后調(diào)用
  • exceptionCaught()處理過程中出現(xiàn)異常時被調(diào)用
@ChannelHandler.Sharable
public class EchoClientHandle extends SimpleChannelInboundHandler<ByteBuf>{

    @Override
    public void channelActive(ChannelHandlerContext context) {
        context.writeAndFlush(Unpooled.copiedBuffer("Netty rocks", CharsetUtil.UTF_8));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        System.out.println("Client received: " + byteBuf.toString(CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
        cause.printStackTrace();
        context.close();
    }
}

EchoClient類:引導(dǎo)客戶端

public class EchoClient {

    // 使用的是服務(wù)器的地址
    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)
             .channel(NioSocketChannel.class)
             .remoteAddress(new InetSocketAddress(host, port))
             .handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(SocketChannel socketChannel) throws Exception {
                     socketChannel.pipeline().addLast(new EchoClientHandle());
                 }
             });
            ChannelFuture future = b.connect().sync();
            future.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        new EchoClient("localhost", 8999).start();
    }
}

GitHub地址:https://github.com/huangbuhuan/netty-demo

參考連接

  • 《Netty In Action》
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容