Netty5 案例讲解

  • 分布式框架中通常使用Netty3.x
  • 游戏服务器通常使用Netty4.x、Netty5.x(4、5相差不多,与3相差比较大)
  • Netty5使用对象池取代线程池

服务端

public class Server {

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

try {
//设置线程池
bootstrap.group(boss, worker);

//设置socket工厂、
bootstrap.channel(NioServerSocketChannel.class);

//设置管道工厂
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参数
bootstrap.option(ChannelOption.SO_BACKLOG, 2048);//serverSocketchannel的设置,链接缓冲池的大小
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);//socketchannel的设置,维持链接的活跃,清除死链接
bootstrap.childOption(ChannelOption.TCP_NODELAY, true);//socketchannel的设置,关闭延迟发送

//绑定端口
ChannelFuture future = bootstrap.bind(10101);

System.out.println("start");

//等待服务端关闭
future.channel().closeFuture().sync();

} catch (Exception e) {
e.printStackTrace();
} finally{
//释放资源
boss.shutdownGracefully();
worker.shutdownGracefully();
}


}
}

客户端

public class Client {

public static void main(String[] args) {
//服务类
Bootstrap bootstrap = new Bootstrap();
//worker
EventLoopGroup worker = new NioEventLoopGroup();

try {
//设置线程池
bootstrap.group(worker);

//设置socket工厂、
bootstrap.channel(NioSocketChannel.class);

//设置管道
bootstrap.handler(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 ClientHandler());
}
});

ChannelFuture connect = bootstrap.connect("127.0.0.1", 10101);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("请输入:");
String msg = bufferedReader.readLine();
connect.channel().writeAndFlush(msg);
}

} catch (Exception e) {
e.printStackTrace();
} finally{
worker.shutdownGracefully();
}
}
}

单客户端多连接(两种方式)

  • 使用对象池实现多连接(当线程不安全,会产生阻塞时,采用对象池的方法)
  • 使用对象组实现多连接(当线程安全,不会产生阻塞时,采用对象组的方法)

在Netty中是线程安全的,所以使用使用对象组的方式实现多连接(线程池中线程是安全,并且任务是串行化执行的)

public class MultClient {

/**
* 服务类
*/
private Bootstrap bootstrap = new Bootstrap();

/**
* 会话
*/
private List<Channel> channels = new ArrayList<>();

/**
* 引用计数
*/
private final AtomicInteger index = new AtomicInteger();

/**
* 初始化
* @param count
*/
public void init(int count){

//worker
EventLoopGroup worker = new NioEventLoopGroup();

//设置线程池
bootstrap.group(worker);

//设置socket工厂、
bootstrap.channel(NioSocketChannel.class);

//设置管道
bootstrap.handler(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 ClientHandler());
}
});
//模拟同时连接多个
for(int i=1; i<=count; i++){
ChannelFuture future = bootstrap.connect("192.168.0.103", 10101);
channels.add(future.channel());
}
}

/**
* 获取会话
* @return
*/
public Channel nextChannel(){
return getFirstActiveChannel(0);
}


private Channel getFirstActiveChannel(int count){
Channel channel = channels.get(Math.abs(index.getAndIncrement() % channels.size()));
if(!channel.isActive()){
//重连
reconnect(channel);
if(count >= channels.size()){
throw new RuntimeException("no can use channel");
}
return getFirstActiveChannel(count + 1);
}
return channel;
}

/**
* 重连
* @param channel
*/
private void reconnect(Channel channel){
synchronized(channel){
if(channels.indexOf(channel) == -1){
return ;
}

Channel newChannel = bootstrap.connect("192.168.0.103", 10101).channel();
channels.set(channels.indexOf(channel), newChannel);
}
}

}

启动函数

public class Start {

public static void main(String[] args) {

MultClient client = new MultClient();
client.init(5);

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true){
try {
System.out.println("请输入:");
String msg = bufferedReader.readLine();
client.nextChannel().writeAndFlush(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

Netty5 案例讲解_Netty 实现