• Netty之服务端客户端demo搭建


    1、serverhandler

    package server;
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * @author liupengr
     * @date 2020/2/16 11:25
     */
    @Slf4j
    public class PRCServerHandler extends SimpleChannelInboundHandler<String> {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
            log.info("server channle read");
            log.info(ctx.channel().remoteAddress() + "to server: " + s);
            ctx.write("server wrrite " + s);
            ctx.flush();
        }
    }

    2、server

    package server;
    
    import client.DemoClient;
    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.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;
    import lombok.extern.slf4j.Slf4j;
    
    import java.net.InetSocketAddress;
    
    /**
     * @author liupengr
     * @date 2020/2/16 11:30
     */
    @Slf4j
    public class RPCServer {
    
        private String ip;
        private int port;
        private int ioThreads; // 用来处理网络流的读写线程
        private int workerThreads; // 用于业务处理的计算线程
    
        public RPCServer(String ip, int port, int ioThreads, int workerThreads) {
            this.ip = ip;
            this.port = port;
            this.ioThreads = ioThreads;
            this.workerThreads = workerThreads;
        }
    
        public void start() {
            //bossGroup, 用于处理客户端的连接请求
            EventLoopGroup bossGroup = new NioEventLoopGroup(ioThreads);
            // workerGroup, 用于处理与各个客户端连接的 IO 操作
            EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
            try {
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .localAddress(new InetSocketAddress(port))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new StringDecoder());
                                ch.pipeline().addLast(new StringEncoder());
                                ch.pipeline().addLast(new PRCServerHandler());
                            }
                        });
                bootstrap.option(ChannelOption.SO_BACKLOG, 100)  // 客户端套件字接受队列大小
                        .option(ChannelOption.SO_REUSEADDR, true) // reuse addr,避免端口冲突
                        .option(ChannelOption.TCP_NODELAY, true) // 关闭小流合并,保证消息的及时性
                        .childOption(ChannelOption.SO_KEEPALIVE, true); // 长时间没动静的链接自动关闭
                ChannelFuture future = bootstrap.bind(port).sync();
                log.info("Server start listen at " + port);
                future.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
    
            }
    
    
    
            /* bootstrap.bind(this.ip, this.port).addListener(future -> {
                 if (future.isSuccess()) {
                     log.info(" RPCserver startup success");
                 } else {
                     log.error(" PRCserver startup failed", future.cause());
                 }
             });*/
        }
    
        public static void main(String[] args) {
            RPCServer rpcServer = new RPCServer("127.0.0.1", 8090, 1, 1);
            rpcServer.start();
        }
    }

    3、clienthandler

    package client;
    
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.SimpleChannelInboundHandler;
    import lombok.extern.slf4j.Slf4j;
    
    /**
     * @author liupengr
     * @date 2020/2/16 16:11
     */
    @Slf4j
    public class DemoClientHandler extends SimpleChannelInboundHandler<String>{
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
            log.info("demoClientHandler read msg:"+s);
        }
    }

    4、client

    package client;
    
    import io.netty.bootstrap.Bootstrap;
    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 io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringDecoder;
    import io.netty.handler.codec.string.StringEncoder;
    import server.RPCServer;
    
    import static io.netty.handler.codec.http.HttpHeaders.Names.HOST;
    
    /**
     * @author liupengr
     * @date 2020/2/16 16:10
     */
    public class DemoClient {
    
        private String ip;
        private int port;
        private int workerThreads; // 用于业务处理的计算线程
    
        public DemoClient(String ip, int port, int workerThreads) {
            this.ip = ip;
            this.port = port;
            this.workerThreads = workerThreads;
        }
    
        public void start() {
            EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
            try {
                Bootstrap bootstrap = new Bootstrap();
                bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new StringDecoder());
                        p.addLast(new StringEncoder());
                        p.addLast(new DemoClientHandler());
                    }
                });
                bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                        .option(ChannelOption.SO_KEEPALIVE, true);
                ChannelFuture future = bootstrap.connect(ip, port).sync();
                future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
                future.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                workerGroup.shutdownGracefully();
            }
    
        }
        public static void main(String[] args) {
            DemoClient client=new DemoClient("127.0.0.1",8090,1);
            client.start();
        }
    }

     加一张调用原型图:

  • 相关阅读:
    PHP mysqli_sqlstate() 函数
    修改用户家目录
    mysql 我的学习
    mysql 表空间
    mysql cluster 运行的必备条件
    浅谈mysql集群
    RBAC权限管理
    mysql 恢复备份
    oracle10G/11G官方下载地址集合 直接迅雷下载
    MySQL 全文搜索支持, mysql 5.6.4支持Innodb的全文检索和类memcache的nosql支持
  • 原文地址:https://www.cnblogs.com/volare/p/12317680.html
Copyright © 2020-2023  润新知