博客
关于我
Netty框架用法介绍&&RPC应用实践
阅读量:177 次
发布时间:2019-02-28

本文共 5266 字,大约阅读时间需要 17 分钟。

Netty技术文档

一、I/O机制简介

计算机中的信息交换主要通过网络I/O和本地I/O实现。网络I/O负责数据在不同计算机之间的传输,本地I/O则负责数据在本地设备(如磁盘、内存)之间的读写。理解I/O机制的演变对于掌握Netty框架至关重要。

I/O的演变历史

I/O的实现方式经历了多次演变:

  • BIO:传统的阻塞I/O模型,单线程处理所有连接,效率较低。
  • NIO:引入了多线程模型,可同时处理多个I/O操作,提升了性能。
  • AIO:基于异步I/O模型,进一步提升了非阻塞操作的效率。
常用框架的通信机制

Netty作为高性能框架,常用于以下系统的通信:

  • Dubbo、RocketMQ、Spring、ElasticSearch、GRPC
  • Spark、Hadoop、Flink

二、环境准备

Netty的依赖管理非常简单,基本配置如下:

io.netty
netty-all
4.1.36.Final

三、Netty RPC示范代码

以下是一个简单的Netty RPC客户端和服务器实现:

服务器代码
public class MyServer {    public static void main(String[] args) throws Exception {        EventLoopGroup bossGroup = new NioEventLoopGroup();        EventLoopGroup workerGroup = new NioEventLoopGroup();        try {            ServerBootstrap serverBootstrap = new ServerBootstrap();            serverBootstrap.group(bossGroup, workerGroup)                         .channel(NioServerSocketChannel.class)                         .childHandler(new MyServerInitializer())                         .localAddress(8899)                         .option(ChannelOption.SO_BACKLOG, 128)                         .childOption(ChannelOption.SO_KEEPALIVE, true);            ChannelFuture channelFuture = serverBootstrap.bind().sync();            channelFuture.channel().closeFuture().sync();        } finally {            bossGroup.shutdownGracefully();            workerGroup.shutdownGracefully();        }    }}class MyServerHandler extends SimpleChannelInboundHandler {    @Override    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {        System.out.println(ctx.channel().remoteAddress() + ", " + msg);        // 业务逻辑处理        Student student = ServiceUtil.query(msg.toString());        IJacksonSerializer serializer = new JacksonSerializer();        byte[] serialize = serializer.serialize(student);        ctx.channel().writeAndFlush(serialize);    }    // 其他事件处理方法可以根据需求添加}class MyServerInitializer extends ChannelInitializer
{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new ByteArrayEncoder()); pipeline.addLast(new MyServerHandler()); }}
客户端代码
public class MyClient {    public static void main(String[] args) throws Exception {        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();        try {            Bootstrap bootstrap = new Bootstrap();            bootstrap.group(eventLoopGroup)                    .channel(NioSocketChannel.class)                    .remoteAddress(new InetSocketAddress("localhost", 8899))                    .handler(new MyClientInitializer());            ChannelFuture channelFuture = bootstrap.connect().sync();            channelFuture.channel().closeFuture().sync();        } finally {            eventLoopGroup.shutdownGracefully().sync();        }    }}class MyClientHandler extends SimpleChannelInboundHandler {    @Override    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {        System.out.println("client output: " + msg + " time: " + System.currentTimeMillis());        ctx.writeAndFlush("C");    }    @Override    public void channelActive(ChannelHandlerContext ctx) throws Exception {        ctx.writeAndFlush("客户端与服务端通道已激活:" + ctx.channel().localAddress());    }}class MyClientInitializer extends ChannelInitializer
{ @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new JacksonDecoder(Student.class)); pipeline.addLast(new MyClientHandler()); }}

四、Netty RPC支持的编解码

Netty提供了丰富的编解码组件,支持多种协议:

自定义编解码

以下是基于Jackson的自定义编解码实现:

public class JacksonDecoder extends MessageToMessageDecoder {    private Class
clazz; public JacksonDecoder(Class
clazz) { this.classtype = clazz; } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List list) throws Exception { byte[] data = new byte[msg.readableBytes()]; msg.getBytes(0, data); IJacksonSerializer serializer = new JacksonSerializer(); Object obj = serializer.deserialize(data, clazz); list.add(obj); }}public class JacksonEncoder extends MessageToMessageEncoder { public JacksonEncoder() {} @Override protected void encode(ChannelHandlerContext ctx, byte[] data, List list) throws Exception { // TODO 实现具体的数据编码逻辑 }}

五、ByteBuf介绍

Netty框架内置了高效的数据缓冲类ByteBuf,支持零拷贝操作。以下是常用的操作示例:

byte[] array = new byte[byteBuf.readableBytes()];byteBuf.getBytes(0, array);

六、线程池机制

Netty的EventLoopGroup基于ServerSocketChannel实现,适用于Linux的epoll和Windows的io.wildcards模型。以下是线程池的使用示例:

EventLoopGroup group = new NioEventLoopGroup();// 创建多个事件循环组以应对不同类型的连接

七、总结

Netty框架以其高效的I/O处理能力和灵活的扩展性,成为现代网络应用的首选工具。通过合理配置和自定义编解码,开发者可以根据具体需求实现高性能的网络通信方案。

转载地址:http://kosn.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>