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

 加一張調用原型圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM