netty之实现一个redis的客户端

写在前面

本文看下如何使用redis来实现一个类似于redis官方提供的redis-cli.exe的客户端工具。

1:用到的模块

主要需要用到netty针对redis的编解码模块,可以解析redis的协议,从而可以实现和redis交互的功能。
在这里插入图片描述

2:正文

首先来定义客户端类:

package com.dahuyou.netty.redis.cli;import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.GenericFutureListener;import javax.swing.plaf.synth.SynthRadioButtonMenuItemUI;
import java.io.BufferedReader;
import java.io.InputStreamReader;public class RedisClient {String host;    //   目标主机int port;       //   目标主机端口public RedisClient(String host,int port){this.host = host;this.port = port;}public void start() throws Exception{EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new RedisClientInitializer());Channel channel = bootstrap.connect(host, port).sync().channel();System.out.println(" connected to host : " + host + ", port : " + port);System.out.println(" type redis's command to communicate with redis-server or type 'quit' to shutdown ");BufferedReader in = new BufferedReader(new InputStreamReader(System.in));ChannelFuture lastWriteFuture = null;for (;;) {Thread.sleep(1000);RedisClientHandler.serialNum = 0;System.out.println(host + ":" + port + ">");String s = in.readLine();if(s.equalsIgnoreCase("quit")) {break;}
//                System.out.print(">");lastWriteFuture = channel.writeAndFlush(s);lastWriteFuture.addListener(new GenericFutureListener<ChannelFuture>() {@Overridepublic void operationComplete(ChannelFuture future) throws Exception {if (!future.isSuccess()) {System.err.print("write failed: ");future.cause().printStackTrace(System.err);}}});}if (lastWriteFuture != null) {lastWriteFuture.sync();}System.out.println(" bye ");}finally {group.shutdownGracefully();}}public static void main(String[] args) throws Exception{
//        RedisClient client = new RedisClient("192.168.56.10",6379);RedisClient client = new RedisClient("127.0.0.1",6379);client.start();}}

这里redis server其实就是一个tcp server的角色了。
在启动类中同一个for (;;) {的死循环来等待用户录入信息,类似于redis-cli.exe的如下功能:
在这里插入图片描述
另外,通过RedisClientInitializer设置协议解析的编解码器,如下:

package com.dahuyou.netty.redis.cli;import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.redis.RedisArrayAggregator;
import io.netty.handler.codec.redis.RedisBulkStringAggregator;
import io.netty.handler.codec.redis.RedisDecoder;
import io.netty.handler.codec.redis.RedisEncoder;public class RedisClientInitializer extends ChannelInitializer<Channel> {@Overrideprotected void initChannel(Channel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new RedisDecoder());pipeline.addLast(new RedisBulkStringAggregator());pipeline.addLast(new RedisArrayAggregator());pipeline.addLast(new RedisEncoder());pipeline.addLast(new RedisClientHandler());}
}

RedisDecoder,RedisBulkStringAggregator,RedisArrayAggregator,RedisEncoder这几个类都是redis codec模块提供的编解码类,如下:
在这里插入图片描述
RedisClientHandler是我们自定义的业务处理类,源码如下:

package com.dahuyou.netty.redis.cli;import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.redis.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;import java.util.ArrayList;
import java.util.List;public class RedisClientHandler extends ChannelDuplexHandler {// 发送 redis 命令@Overridepublic void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {String[] commands = ((String) msg).split("\\s+");List<RedisMessage> children = new ArrayList<>(commands.length);for (String cmdString : commands) {children.add(new FullBulkStringRedisMessage(ByteBufUtil.writeUtf8(ctx.alloc(), cmdString)));}RedisMessage request = new ArrayRedisMessage(children);ctx.write(request, promise);}// 接收 redis 响应数据@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) {RedisMessage redisMessage = (RedisMessage) msg;// 打印响应消息printAggregatedRedisResponse(redisMessage);// 是否资源ReferenceCountUtil.release(redisMessage);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {System.err.print("exceptionCaught: ");cause.printStackTrace(System.err);ctx.close();}private static void printAggregatedRedisResponse(RedisMessage msg) {if (msg instanceof SimpleStringRedisMessage) {System.out.println(((SimpleStringRedisMessage) msg).content());} else if (msg instanceof ErrorRedisMessage) {System.out.println(((ErrorRedisMessage) msg).content());} else if (msg instanceof IntegerRedisMessage) {System.out.println(((IntegerRedisMessage) msg).value());} else if (msg instanceof FullBulkStringRedisMessage) {System.out.println(getString((FullBulkStringRedisMessage) msg));} else if (msg instanceof ArrayRedisMessage) {for (RedisMessage child : ((ArrayRedisMessage) msg).children()) {printAggregatedRedisResponse(child);}} else {throw new CodecException("unknown message type: " + msg);}}public static int serialNum = 0;private static String getString(FullBulkStringRedisMessage msg) {if (msg.isNull()) {return "(null)";}return ++serialNum + ") " +msg.content().toString(CharsetUtil.UTF_8);}}

运行测试:
在这里插入图片描述

写在后面

参考文章列表

redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。 。

netty之导入源码到idea 。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/59220.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

防重方案-订单防重方案笔记

订单防重设计 订单重复提交概念解决方案前端防重机制后端防重机制利用Token机制基于数据库的唯一索引 Token机制方案介绍 其他 订单重复提交概念 重复提交指&#xff0c;连点按钮进行重复提交操作&#xff0c;不包括刷新后的重新下单&#xff0c;重新下单已非同一订单的概念。…

Vision - 开源视觉分割算法框架 Grounded SAM2 配置与推理 教程 (1)

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/143388189 免责声明&#xff1a;本文来源于个人知识与公开资料&#xff0c;仅用于学术交流&#xff0c;欢迎讨论&#xff0c;不支持转载。 Ground…

【C++刷题】力扣-#697-数组的度

题目描述 给定一个非空且只包含非负数的整数数组 nums&#xff0c;数组的 度 的定义是指数组里任一元素出现频数的最大值。 你的任务是在 nums 中找到与 nums 拥有相同大小的度的最短连续子数组&#xff0c;返回其长度。 示例 示例 1 输入&#xff1a;nums [1,2,2,3,1] 输出…

LocalDate 类常用方法详解(日期时间类)

LocalDate 类常用方法详解 LocalDate 是 Java 8 引入的日期时间API中的一个类&#xff0c;用于表示不含时间和时区的日期&#xff08;年、月、日&#xff09;。以下是一些常用的 LocalDate 方法&#xff1a; 创建 LocalDate 实例 now()&#xff1a;获取当前日期 LocalDate t…

一些常用的react hooks以及各自的作用

一些常用的react hooks以及各自的作用 一、React Hooks是什么二、一些常用的Hooks以及各自的作用1、useState2、useEffect3、useContext4、useMemo5、useCallback6、useReducer7、useRef 一、React Hooks是什么 Hook 是 React 16.8 的新增特性。它可以让你在不编写 class 的情…

不用买PSP,画质甚至更好,这款免费神器让你玩遍经典游戏

作为掌机游戏爱好者的福音&#xff0c;PPSSPP模拟器为玩家带来了前所未有的PSP游戏体验&#xff0c;彻底改变了掌机游戏的体验方式。这款精湛的软件不仅完美复刻了PSP主机的游戏体验&#xff0c;更通过先进的模拟技术&#xff0c;将经典游戏提升到了全新的高度。对于那些珍藏PS…

lua学习笔记---面向对象

在 Lua 中&#xff0c;封装主要通过元表&#xff08;metatable&#xff09;来实现。元表可以定义 __index、__newindex、__call 等元方法来控制对表的访问和赋值行为。 __index 元方法&#xff1a;当尝试访问一个不存在的键时&#xff0c;Lua 会查找元表的 __index 字段。如果 …

第15课 算法(下)

掌握冒泡排序、选择排序、插入排序、顺序查找、对分查找的的基本原理&#xff0c;并能使用这些算法编写简单的Python程序。 一、冒泡排序 1、冒泡排序的概念 冒泡排序是最简单的排序算法&#xff0c;是在一列数据中把较大&#xff08;或较小&#xff09;的数据逐次向右推移的…

golang通用后台管理系统03(登录校验,并生成token)

代码 package serviceimport ("fmt"//"fmt""gin/common""gin/config"sysEntity "gin/system/entity"sysUtil "gin/system/util""github.com/gin-gonic/gin""log" )func Login(c *gin.Contex…

Java环境下配置环境(jar包)并连接mysql数据库

目录 jar包下载 配置 简单连接数据库 一、注册驱动&#xff08;jdk6以后会自动注册&#xff09; 二、连接对应的数据库 以前学习数据库就只是操作数据库&#xff0c;根本不知道该怎么和软件交互&#xff0c;将存储的数据读到软件中去&#xff0c;最近学习了Java连接数据库…

快速遍历包含合并单元格的Word表格

Word中的合并表格如下&#xff0c;现在需要根据子类&#xff08;例如&#xff1a;果汁&#xff09;查找对应的品类&#xff0c;如果这是Excel表格&#xff0c;那么即使包含合并单元格&#xff0c;也很容易处理&#xff0c;但是使用Word VBA进行查找&#xff0c;就需要一些技巧。…

「C/C++」C/C++标准库 之 #include<ctime> 时间日期库

✨博客主页何曾参静谧的博客&#x1f4cc;文章专栏「C/C」C/C程序设计&#x1f4da;全部专栏「VS」Visual Studio「C/C」C/C程序设计「UG/NX」BlockUI集合「Win」Windows程序设计「DSA」数据结构与算法「UG/NX」NX二次开发「QT」QT5程序设计「File」数据文件格式「PK」Parasoli…

写论文随想(整理我自己的感悟)(不断更新中,废案按照删除号标记)

写论文随想&#xff08;整理我自己的感悟)&#xff08;不断更新中&#xff0c;废案按照删除号标记&#xff09; 论文的所有内容&#xff0c;都是为了服务于自己的创新点&#xff0c;只要整个文章围绕这个创新点讲好了&#xff0c;一篇自己满意的文章就成了。这也就是我现在的目…

使用wordcloud与jieba库制作词云图

目录 一、WordCloud库 例子&#xff1a; 结果&#xff1a; 二、Jieba库 两个基本方法 jieba.cut() jieba.cut_for_serch() 关键字提取&#xff1a; jieba.analyse包 extract_tags() 一、WordCloud库 词云图&#xff0c;以视觉效果提现关键词&#xff0c;可以过滤文本…

深入解析缓存模式下的数据一致性问题

今天&#xff0c;我们来聊聊常见的缓存模式和数据一致性问题。 常见的缓存模式有&#xff1a;Cache Aside、Read Through、Write Through、Write Back、Refresh Ahead、Singleflight。 缓存模式 Cache Aside 在 Cache Aside 模式中&#xff0c;是把缓存当做一个独立的数据源…

第四篇: 用Python和SQL在BigQuery中进行基础数据查询

用Python和SQL在BigQuery中进行基础数据查询 在大数据分析领域&#xff0c;Google BigQuery 提供了一种快速且经济高效的数据处理方式。对于想要使用SQL查询大规模数据的读者来说&#xff0c;BigQuery的公共数据集资源丰富、操作简便&#xff0c;是学习和实践SQL基础操作的理想…

Spring学习笔记_19——@PostConstruct @PreDestroy

PostConstruct && PreDestroy 1. 介绍 PostConstruct注解与PreDestroy注解都是JSR250规范中提供的注解。 PostConstruct注解标注的方法可以在创建Bean后在为属性赋值后&#xff0c;初始化Bean之前执行。 PreDestroy注解标注的方法可以在Bean销毁之前执行。 2. 依赖…

11.4模拟赛总结

文章目录 时间安排成绩反思 时间安排 7 : 40 − 8 : 00 7:40 - 8:00 7:40−8:00 开题。把题都看了一遍。 T 1 T1 T1 看起来有点神秘。 T 2 T2 T2 想很难的构造。 T 3 T3 T3 看起来像比较正常的计数题。 T 4 T4 T4 应该是扫描线 8 : 00 − 9 : 20 8:00 - 9:20 8:00−9:20 尝试…

ffmpeg视频滤镜:膨胀操作-dilation

滤镜介绍 dilation 官网链接 > FFmpeg Filters Documentation 膨胀滤镜会使图片变的更亮&#xff0c;会让细节别的更明显。膨胀也是形态学中的一种操作&#xff0c;在opencv中也有响应的算子。此外膨胀结合此前腐蚀操作&#xff0c;可以构成开闭操作。 开操作是先腐蚀…

多线程和线程同步基础篇学习笔记(Linux)

大丙老师教学视频&#xff1a;10-线程死锁_哔哩哔哩_bilibili 目录 大丙老师教学视频&#xff1a;10-线程死锁_哔哩哔哩_bilibili 线程概念 为什么要有线程 线程和进程的区别 在处理多任务的时候为什么线程数量不是越多越好? Linux提供的线程API 主要接口 线程创建 pth…