共计 7047 个字符,预计需要花费 18 分钟才能阅读完成。
明天是实战篇,没有实践,只是记录一下应用 Netty 搭建一个本地服务,实现客户端数据发送到服务端数据 Demo 的教程,实践篇以及更深层次学习 Netty 能够点个关注,保障后续更新不错过!!!
搭建 Netty 服务端
服务端
应用 main 办法的模式构建,启动在本地 8888
端口
package com.zuiyu.netty;
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;
/**
* @author @醉鱼
* @link https://github.com/TianPuJun
* @ClassName NettyService
* @Description
* @Date 22:05 2022/6/28
**/
public class NettyService {public static void main(String[] args) throws InterruptedException {
// 用于接管客户端链接的线程工作组
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
// 用于接管客户端链接读写操作的线程组
EventLoopGroup workGroup = new NioEventLoopGroup();
// 辅助类,帮忙创立 netty 服务
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap
// 绑定两个工作组
.group(eventLoopGroup,workGroup)
// 设置 nio 模式
.channel(NioServerSocketChannel.class)
// option 针对服务器端配置,childOption 针对客户端链接通道配置
// 设置 tcp 缓冲区
.option(ChannelOption.SO_BACKLOG,1024)
// 设置发送数据的缓存大小
.childOption(ChannelOption.SO_SNDBUF,32*1024)
// 设置读取数据的缓存大小
.childOption(ChannelOption.SO_RCVBUF,32*1024)
// 设置放弃长链接
.childOption(ChannelOption.SO_KEEPALIVE,true)
// 初始化绑定服务通道
.childHandler(new ChannelInitializer<SocketChannel>() {protected void initChannel(SocketChannel socketChannel) throws Exception {// 为通道进行初始化,数据传输过去的适宜会进行拦挡和执行(能够有多个拦截器)
socketChannel.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture sync = serverBootstrap.bind(8888).sync();
// 开释链接
sync.channel().closeFuture().sync();
workGroup.shutdownGracefully();
eventLoopGroup.shutdownGracefully();}
}
服务端监听
新建 ServerHandler
监听数据
package com.zuiyu.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import java.util.logging.Logger;
/**
* @author @醉鱼
* @link https://github.com/TianPuJun
* @ClassName ServerHandler
* @Description 服务回调
* @Date 22:15 2022/6/28
**/
public class ServerHandler extends ChannelInboundHandlerAdapter {public static final Logger LOGGER = Logger.getLogger("ServerHandler");
public ServerHandler() {super();
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {super.channelRegistered(ctx);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {super.channelUnregistered(ctx);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {LOGGER.info("====== 服务端通道激活 ======");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);
}
/**
* 当通道有数据读取时的监听
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
// 传输 buffer 对象
ByteBuf buf = (ByteBuf)msg;
// 定义 byte 数组
byte[] req =new byte[buf.readableBytes()];
// 从缓冲区获取数据到 req
buf.readBytes(req);
// 读取数据转为字符串
String body = new String(req,"utf-8");
LOGGER.info("服务端读取的数据:"+body);
// 响应给客户端的数据
ctx.writeAndFlush(Unpooled.copiedBuffer("netty server response data: you are beautiful".getBytes()))
// 增加 addListener 能够触发敞开通道监听事件,客户端短链接场景应用
// .addListener(ChannelFutureListener.CLOSE);
;
}catch (Exception e){e.printStackTrace();
}finally {ReferenceCountUtil.release(msg);
}
}
/**
* 当咱们读取实现数据时触发的操作
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {LOGGER.info("====== 服务端数据读取实现 ======");
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {super.userEventTriggered(ctx, evt);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {super.channelWritabilityChanged(ctx);
}
/**
* 当咱们读取数据异样的时候触发的监听
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {LOGGER.info("====== 服务端数据读取异样 ======");
LOGGER.info(cause.getMessage());
ctx.close();}
}
搭建 Netty 客户端
客户端
同样实用 main 办法的模式搭建测试,发送数据到本地 8888
端口
package com.zuiyu.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
/**
* @author @醉鱼
* @link https://github.com/TianPuJun
* @ClassName NettyClient
* @Description
* @Date 22:27 2022/6/28
**/
public class NettyClient {public static void main(String[] args) throws InterruptedException {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(eventExecutors)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {protected void initChannel(SocketChannel socketChannel) throws Exception {socketChannel.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture channelFuture = b.connect("127.0.0.1", 8888).syncUninterruptibly();
channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer("netty 客户端申请数据:zuiyu netty 服务发送数据测试".getBytes()));
// 开释链接
channelFuture.channel().closeFuture().sync();}
}
客户端监听
新建客户端监听ClientHandler
,发送数据时打印数据
package com.zuiyu.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import java.util.logging.Logger;
/**
* @author @醉鱼
* @link https://github.com/TianPuJun
* @ClassName ClientHandler
* @Description
* @Date 22:32 2022/6/28
**/
public class ClientHandler extends ChannelInboundHandlerAdapter {public static final Logger LOGGER = Logger.getLogger("ClientHandler");
public ClientHandler() {super();
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {super.channelRegistered(ctx);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {super.channelUnregistered(ctx);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {LOGGER.info("====== 客户端通道激活 ======");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {super.channelInactive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
// 传输 buffer 对象
ByteBuf buf = (ByteBuf)msg;
// 定义 byte 数组
byte[] req =new byte[buf.readableBytes()];
// 从缓冲区获取数据到 req
buf.readBytes(req);
// 读取数据转为字符串
String body = new String(req,"utf-8");
LOGGER.info("客户端读取的数据:"+body);
}catch (Exception e){e.printStackTrace();
}finally {ReferenceCountUtil.release(msg);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {LOGGER.info("====== 客户端读取数据结束 ======");
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {super.userEventTriggered(ctx, evt);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {super.channelWritabilityChanged(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {LOGGER.info("====== 客户端读取数据异样 ======");
ctx.close();}
}
验证打印输出
- 先启动
NettyServer#main
- 再启动
NettyClient#main
- 后果如下
Demo 教程到此结束,只为记录过程,代码具体地位 NettyServer
中都有正文,感觉有帮忙动动小手点个关注再走哦,点关注不迷路
Demo 地址 :
https://github.com/TianPuJun/spring-notes/tree/master/netty
本文由 mdnice 多平台公布
正文完