netty 框架使用
因为netty3和4的差别不是很大
1 、netty3
1.1 pom文件
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.9.0.Final</version>
</dependency>
1.2 server代码
package com.liu.netty3.server;
import com.liu.netty3.handler.MsgHandler;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Administrator on 2019/8/28 0028.
*/
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();
// 注意这里是为了前台测试的时候,我们使用的是GBK 的格式发送,所以要讲解码器 也设置成GBK
// 为了能够是的客户端,不乱吗,所以要将编码器也设置成GBK格式
pipeline.addLast("decoder",new StringDecoder(Charset.forName("GBK")));
pipeline.addLast("encoder",new StringEncoder(Charset.forName("GBK")));
pipeline.addLast("MsgHandler",new MsgHandler());
return pipeline;
}
});
bootstrap.bind(new InetSocketAddress(10030));
System.out.println("服务端已经启动!!!");
}
}
1.3 消息处理类
package com.liu.netty3.handler;
import org.jboss.netty.channel.*;
import java.nio.charset.Charset;
/**
* Created by Administrator on 2019/8/28 0028.
*/
public class MsgHandler extends SimpleChannelHandler{
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelConnected");
super.channelConnected(ctx, e);
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
String s = (String)e.getMessage();
System.out.println(s);
ctx.getChannel().write("我已经收到消息");
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 channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelDisconnected");
super.channelDisconnected(ctx, e);
}
@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("channelClosed");
super.channelClosed(ctx, e);
}
}
1.4 测试客户端
这个测试工具在我的上一篇博客中有写:https://blog.csdn.net/shi860715/article/details/100114583
2、netty5
2.1 pom 文件
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha1</version>
</dependency>
2.2 server代码
package com.liu.netty5.server;
import com.liu.netty5.handler.ServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
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;
import java.nio.charset.Charset;
/**
* Created by Administrator on 2019/8/28 0028.
*/
public class Server {
public static void main(String[] args) {
ServerBootstrap bootstrap = new ServerBootstrap();
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
// 设置 工作线程
bootstrap.group(boss,worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));
channel.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
channel.pipeline().addLast(new ServerHandler());
}
});
bootstrap.option(ChannelOption.SO_BACKLOG,1024);// 服务端排队队列
bootstrap.option(ChannelOption.TCP_NODELAY,true);// TCP 无延时
bootstrap.option(ChannelOption.SO_KEEPALIVE,true);// 清除死链接,维持活跃的
// 绑定通讯端口
ChannelFuture future = bootstrap.bind(10040);
// sout
System.out.println("服务端启动!!!");
future.channel().closeFuture().sync();
}catch (Exception e){
}finally {
// 优雅关闭
boss.shutdownGracefully();
// 优雅关闭
worker.shutdownGracefully();
}
}
}
2.3 消息处理类
package com.liu.netty5.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* Created by Administrator on 2019/8/28 0028.
*/
public class ServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
// 接收到的消息
System.out.println(msg);
// 返回数据
ctx.writeAndFlush("给你回话");
}
}