基于Netty的RPC架构学习笔记(六):netty5案例学习

版权声明:本文为作者原创,转载请注明出处,联系qq:32248827 https://blog.csdn.net/dataiyangu/article/details/88261647

netty5服务端入门案例

Server.java

package com.server;

import io.netty.bootstrap.ServerBootstrap;
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.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**

- netty5服务端

- 
  *
   */
  public class Server {

  public static void main(String[] args) {
  	//服务类
  	ServerBootstrap bootstrap = new ServerBootstrap();
  	

  //boss和worker
  //在前面的3里面的例子这里是两个线程池,在5里面做了一次封装
  //EventLoopGroup这个类中还是包含线程池这个属性的
  EventLoopGroup boss = new NioEventLoopGroup();
  EventLoopGroup worker = new NioEventLoopGroup();
  
  try {
  	//设置线程池
  	bootstrap.group(boss, worker);
  	
  	//设置socket工厂、
  	//在3中 bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));
  
  	bootstrap.channel(NioServerSocketChannel.class);
  	
  	//设置管道工厂
  	//在3中bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
  	//管道最终是要放到channel中的,这边不是把管道传过来,而是直接把拥有管道的channel,传过来,
  	//我们自己去设置管道
  	bootstrap.childHandler(new ChannelInitializer<Channel>() {
  
  		@Override
  		protected void initChannel(Channel ch) throws Exception {
  			ch.pipeline().addLast(new StringDecoder());
  			ch.pipeline().addLast(new StringEncoder());
  			ch.pipeline().addLast(new ServerHandler());
  		}
  	});
  	
  	//netty3中对应设置如下
  	//bootstrap.setOption("backlog", 1024);
  	//bootstrap.setOption("tcpNoDelay", true);
  	//bootstrap.setOption("keepAlive", true);
  	//设置参数,TCP参数
  	//serverSocketchannel的设置,链接缓冲池的大小
  	//accept操作是从缓存队列里面拿到主机,加入有2048个主机连接进来,第2049个主机想再次连接进来
  	//就会被拒绝
  	bootstrap.option(ChannelOption.SO_BACKLOG, 2048);
  	//socketchannel的设置,维持链接的活跃,清除死链接
  	//加入有连接在很长一段时间既没有读也没有写,就会自动关掉这个连接
  	bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
  	//socketchannel的设置,关闭延迟发送
  	//tcp是有批量发送的算法的,这里设置为true,进行关闭
  	bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
  	
  	//绑定端口
  	ChannelFuture future = bootstrap.bind(10101);
  	
  	System.out.println("start");
  	
  	//等待服务端关闭
  	//这里的channel是serversocketchannel或者监听端口的channel
  	//.sync()就会阻塞在这里等待channel关闭之后再继续往下走。
  	future.channel().closeFuture().sync();
  } catch (Exception e) {
  	e.printStackTrace();
  } finally{
  	//释放资源
  	boss.shutdownGracefully();
  	worker.shutdownGracefully();
  }


  }
  }

ServerHandler.java

package com.server;

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

- 服务端消息处理

- 
  *
   */
  public class ServerHandler extends SimpleChannelInboundHandler<String> {

  @Override
  protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {

//得到客户端发送的数据
//3中 String s = (String) e.getMessage()
//这里可以拿来直接用
  System.out.println(msg);
  
  //下面两种回写都是可以的,他们两个调用的是同一个方法
  ctx.channel().writeAndFlush("hi");
  ctx.writeAndFlush("hi");


  }

  /**

  - 新客户端接入
    */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelActive");
    }

  /**

  - 客户端断开
    */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channelInactive");
    }

  /**

  - 异常
    */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    }

  

}

netty5客户端入门案例

单客户端多连接程序

猜你喜欢

转载自blog.csdn.net/dataiyangu/article/details/88261647