zoukankan      html  css  js  c++  java
  • netty4简易教程

    近来有空,研究了一下netty。netty5还仅仅是beta版本号。官方建议使用netty4。

    这个实例中,Client通过控制台输入信息,与Server端通信。

    对于普通开发人员来说,netty4的经常使用接口有:SimpleChannelInboundHandler/SimpleChannelOutboundHandler、ChannelInitializer、encoder和decoder相关的类。

    server端:

    1、编写handler类

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;

    public class ServerHandler extends SimpleChannelInboundHandler {
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("server:" + "channelRead:" + msg);
    ctx.writeAndFlush("Received your message !");
    }
    }

    2、注冊服务。绑定port:

    import io.netty.channel.Channel;
    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.nio.NioServerSocketChannel;
    import io.netty.handler.codec.protobuf.ProtobufDecoder;
    import io.netty.handler.codec.protobuf.ProtobufEncoder;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;

    public class Server {


    public static void main(String[] args) {
    EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
    EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup();
    try {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
    ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new StringDecoder());
    ch.pipeline().addLast("handler", new ServerHandler());
    };
    });
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
    channelFuture.channel().closeFuture().sync();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    bossEventLoopGroup.shutdownGracefully();
    workerEventLoopGroup.shutdownGracefully();
    }
    }
    }


    client:

    1、编写handler类

    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;

    public class ClientHandler extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("client:" + "channelRead:" + msg);
    }
    }

    2、编写请求入口

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.Channel;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.protobuf.ProtobufDecoder;
    import io.netty.handler.codec.protobuf.ProtobufEncoder;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;

    public class Client {

    public static void main(String[] args) {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    try {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.handler(new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel ch) throws Exception {
    ch.pipeline().addLast("encoder", new StringEncoder());
    ch.pipeline().addLast("decoder", new StringDecoder());
    ch.pipeline().addLast("handler", new ClientHandler());
    };
    });

    Channel ch = bootstrap.connect("127.0.0.1", 8888).sync().channel();

    // 控制台输入
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    for (;;) {
    String line = in.readLine();
    if (line == null || "".equals(line)) {
    continue;
    }
    ch.writeAndFlush(line);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    eventLoopGroup.shutdownGracefully();
    }
    }
    }

    新手教程,欢迎拍砖~


  • 相关阅读:
    SAP C4C OBN(Object Based Navigation)不能工作的原因分析
    使用SAP C4C自定义BO association创建动态下拉列表
    如何使用SAP HANA Studio的PlanViz分析CDS view性能问题
    如何使用jMeter测试SAP OData服务并发访问时的性能
    OData服务在SAP CRM,Cloud for Customer和S/4HANA上的实现比较
    SAP UI5和Vue的数据双向绑定实现原理比较
    在SAP WebClient UI里显示倒数计时的UI
    【年度重磅】2020华为云社区年度技术精选合集,700页+免费下载!
    面试必问:如何实现Redis分布式锁
    聊聊架构模式的变迁:从分层架构到微服务架构
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7266819.html
Copyright © 2011-2022 走看看