网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用

 网络编程 -- RPC实现原理 -- 目录

  啦啦啦

V1——Netty入门应用

  Class : NIOServerBootStrap

package lime.pri.limeNio.netty.netty01.server;

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NIOServerBootStrap {

    public static void main(String[] args) throws InterruptedException {
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        bootstrap.group(boss, worker);
        bootstrap.channel(NioServerSocketChannel.class);

        bootstrap.childHandler(new CustomServerChannelInitializer());

        ChannelFuture channelFuture = bootstrap.bind(new InetSocketAddress(9999)).sync();
        channelFuture.channel().closeFuture().sync();
        boss.shutdownGracefully();
        worker.shutdownGracefully();
    }
}

  Class : CustomServerChannelInitializer

package lime.pri.limeNio.netty.netty01.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class CustomServerChannelInitializer extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new CustomServerChannelHandlerAdapter());
    }

}

  Class : CustomServerChannelHandlerAdapter

package lime.pri.limeNio.netty.netty01.server;

import java.util.Date;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;

public class CustomServerChannelHandlerAdapter extends ChannelHandlerAdapter {


    /**
     * 服务器和客户端会话异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // TODO Auto-generated method stub
        super.exceptionCaught(ctx, cause);
    }

    /**
     * 当服务器与客户端联通时,通道被激活。
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelActive(ctx);
    }

    /**
     * 当服务器与客户端断开时,该方法被调用。
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelInactive(ctx);
    }

    /**
     * 通道读操作就绪,读取客户端消息
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buffer = (ByteBuf)msg;
        String request  = buffer.toString(CharsetUtil.UTF_8);
        System.out.println("客户端请求数据:" + request);

        buffer.clear();
        buffer.writeBytes(new Date().toString().getBytes());
        ChannelFuture channelFuture = ctx.writeAndFlush(buffer);
        channelFuture.addListener(ChannelFutureListener.CLOSE);
    }

}

  Class : 

package lime.pri.limeNio.netty.netty01.client;

import java.io.IOException;
import java.net.InetSocketAddress;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NIOBootStrap {

    public static void main(String[] args) throws IOException, InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup worker = new NioEventLoopGroup();
        bootstrap.group(worker);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.handler(new CustomClientChannelInitializer());
        ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 9999)).sync();
        channelFuture.channel().closeFuture().sync();
        worker.shutdownGracefully();
    }
}

  Class : CustomClientChannelInitializer

package lime.pri.limeNio.netty.netty01.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;

public class CustomClientChannelInitializer extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new CustomClientChannelHandlerAdapter());
    }

}

  Class : CustomClientChannelHandlerAdapter

package lime.pri.limeNio.netty.netty01.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;

public class CustomClientChannelHandlerAdapter extends ChannelHandlerAdapter {

    /**
     * 服务器和客户端会话异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // TODO Auto-generated method stub
        super.exceptionCaught(ctx, cause);
    }

    /**
     * 当服务器与客户端联通时,通道被激活。
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ByteBuf buffer = Unpooled.buffer();
        ctx.writeAndFlush(buffer.writeBytes("Query Date".getBytes()));
    }

    /**
     * 当服务器与客户端断开时,该方法被调用。
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        super.channelInactive(ctx);
    }

    /**
     * 通道多操作就绪,读取服务端消息
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务端响应数据:" + ((ByteBuf) msg).toString(CharsetUtil.UTF_8));
        // 会话关闭操作由服务端启动,客户端不主动关闭会话。
    }

}

  Console : Server

客户端请求数据:Query Date

  Console : Client

服务端响应数据:Sat Jun 24 17:43:41 CST 2017

啦啦啦

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/7073963.html