博客
关于我
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/

你可能感兴趣的文章
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx Location配置总结
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>
nginx 后端获取真实ip
查看>>
Nginx 学习总结(17)—— 8 个免费开源 Nginx 管理系统,轻松管理 Nginx 站点配置
查看>>
nginx 常用配置记录
查看>>
Nginx 我们必须知道的那些事
查看>>
Nginx 的 proxy_pass 使用简介
查看>>
Nginx 的配置文件中的 keepalive 介绍
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>