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();
    }
}

 加一张调用原型图:

 

posted @ 2020-02-16 17:29  以梦为码  阅读(1215)  评论(0编辑  收藏  举报