首页 > 其他分享 >Netty入门

Netty入门

时间:2023-03-08 10:32:40浏览次数:41  
标签:Netty pipeline 入门 ctx System println new public


第二部分 Netty入门

netty可以运用在那些领域?
1分布式进程通信
例如: hadoop、dubbo、akka等具有分布式功能的框架,底层RPC通信都是基于netty实现的,这些框架使用的版本通常都还在用netty3.x

2、游戏服务器开发
最新的游戏服务器有部分公司可能已经开始采用netty4.x 或 netty5.x

服务器端

public class Server {

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

//boss线程监听端口,worker线程负责数据读写
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService worker = Executors.newCachedThreadPool();

//设置niosocket工厂
bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));

//设置管道的工厂
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

@Override
public ChannelPipeline getPipeline() throws Exception {

ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("helloHandler", new HelloHandler());
return pipeline;
}
});
bootstrap.bind(new InetSocketAddress(10101));
System.out.println("start!!!");

}

}

客户端

public class Client {

public static void main(String[] args) {

//服务类
ClientBootstrap bootstrap = new ClientBootstrap();

//线程池
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService worker = Executors.newCachedThreadPool();

//socket工厂
bootstrap.setFactory(new NioClientSocketChannelFactory(boss, worker));

//管道工厂
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("hiHandler", new HiHandler());
return pipeline;
}
});

//连接服务端
ChannelFuture connect = bootstrap.connect(new InetSocketAddress("127.0.0.1", 10101));
Channel channel = connect.getChannel();

System.out.println("client start");

Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("请输入");
channel.write(scanner.next());
}


}

}

消息封装处理类

无论客户端或者服务器端都存在消息处理类,通过继承SimpleChannelHandler抽象类来实现,这里以服务器端封装类举例:

public class HelloHandler extends SimpleChannelHandler {

/**
* 接收消息
*/
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

String s = (String) e.getMessage();
System.out.println(s);

//回写数据
ctx.getChannel().write("hi");
super.messageReceived(ctx, e);
}

/**
* 捕获异常
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
System.out.println("exceptionCaught");
super.exceptionCaught(ctx, e);
}

/**
* 新连接
*/
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelConnected");
super.channelConnected(ctx, e);
}

/**
* 必须是链接已经建立,关闭通道的时候才会触发
*/
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelDisconnected");
super.channelDisconnected(ctx, e);
}

/**
* channel关闭的时候触发
*/
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelClosed");
super.channelClosed(ctx, e);
}
}
  • messageReceived接收消息
  • channelConnected新连接,通常用来检测IP是否是黑名单
  • channelDisconnected已经建立好的连接关闭,可以再用户断线的时候清楚用户的缓存数据等
  • channelClosed无论连接是否成功都会调用关闭资源

Netty入门_bootstrap


标签:Netty,pipeline,入门,ctx,System,println,new,public
From: https://blog.51cto.com/u_15997399/6107691

相关文章