jedis

redis客户端jedis&spring-data-redis源码赏析

倾然丶 夕夏残阳落幕 提交于 2019-12-03 09:51:03
背景 本文主要对当下开源流行的redis客户端jedis和spring-data-redis的部分核心源码进行剖析,记录一下怎么去实现一个redis的java客户端以及在使用redis集群时客户端的操作需要注意的要点。 版本 jedis:v2.9.0、 spring-data-redis:v2.0.8.RELEASE 源代码的分析 先看最核心的入口类:org.springframework.data.redis.core.RedisTemplate 它继承于org.springframework.data.redis.core.RedisAccessor,主要是设置org.springframework.data.redis.connection.RedisConnectionFactory,并在spring bean初始化完毕对connectionFactory进行为空校验; 实现的核心接口org.springframework.data.redis.core.RedisOperations主要提供了一些基础操作,但这个接口并不经常使用,因为redis的数据结构比较复杂,更多具体的操作都封装在了ValueOperations、ListOperations等,以及BoundValueOperations、BoundListOperations等这两类操作中

redis集群下使用pipline进行批处理操作(spring/springboot+jedis)

泄露秘密 提交于 2019-12-03 09:50:52
我们都知道redis集群下对于mget、mset、pipeline、事务的支持不太好。 当然对于mget和mset有这么几种方法: 1、串行遍历key依次执行(这种就是把批量拆开了) 2、使用hash_tag包装key,在计算key的slot时候,如果key包含{},就会使用第一个{}内部的字符串作为hash key,这样就可以保证拥有同样{}内部字符串的key就会拥有相同slot(这种方式其实就是把一次批量操作的key全部放到了集群的一个节点进行操作,屏蔽掉多节点的问题)。 3、自己手动进行批量的key做处理,通过CRC16算法对所有的key进行分组(相同slot的分成一组),然后不同的分组keys,使用不同的集群节点进行处理。 本文就是适用了第三种方式,通过 pipeline来操作批处理,减少网络请求次数,加快处理速度。 使用jedis封装的工具类,源码也是分析的jedis。 1、通过源码看下原理 对于jedis,集群的操作使用的JedisCluster类,看下它的继承实现关系图: 通过继承实现管理可以看到,JedisCluster继承自 BinaryJedisCluster ,以及实现了其他接口。我们再查看下 BinaryJedisCluster源码。 在图中我们注意到:JedisClusterConnectionHandler 这个类,字面意思redis集群连接处理器

Cannot connect to redis using jedis

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Redis version: 3.2.0 Jedis version: 2.8.1 Below is my java code for connecting to redis: public class TestRedis { public static void main(String[] args) { String host = args[0]; int port = Integer.parseInt(args[1]); try (Jedis jedis = new Jedis(host, port)) { System.out.println("Connected to jedis " + jedis.ping()); } catch(Exception e){ e.printStackTrace(); } } } I am running this program in the machine where redis is installed. This machine's ip address is 192.168.1.57 If I provide host="localhost" and port = "6379" as arguments,

Jedi连接池

会有一股神秘感。 提交于 2019-12-03 07:04:23
简介 jedis用于创建和管理连接,并提供了对redis数据库的操作方法。Jedis相当于JDBC中的Connection,JedisPool相当于DBCP或者C3P0,而JedisCluster更像是Spring中的JDBCTemplate(将连接池和连接都给封装起来了,只提供给我们直接操作redis的api)。 JedisPool和JedisCluster在构造连接池对象的时候,需要传入JedisPoolConfig,host,port等。这些配置信息我们可以统一地定义在jedis.properties文件中,方便后续维护。但 jedis有个不足,就是不能读取properties文件,需要我们手动取值设置。 使用例子 需求 使用jedis连接池对redis进行String、Set、Sorted Set、List、Hash和Key的操作。(这里仅测试单机版,集群不涉及) 工程环境 JDK:1.8.0_201 maven:3.6.1 IDE:Spring Tool Suites4 for Eclipse redis:3.2.100(windows64) 主要步骤 读取jedis.properties文件,创建并设置JedisPoolConfig; 根据配置参数构造JedisPool; 调用JedisPool的getResource方法获取Jedis对象;

Jedis API操作redis数据库

丶灬走出姿态 提交于 2019-12-03 06:51:24
1、配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6379 redis.timeout=10000 redis.maxIdle=300 redis.maxTotal=600 # 毫秒 redis.maxWaitMillis=1000 redis.testOnBorrow=false View Code 新建属性文件工具类,用来读取redis.properties配置文件 /** * <p>属性文件工具类 * * @author xupeng * @date 2019/10/28 10:39 */ public class PropertyUtil { //加载property文件到io流里面 public static Properties loadProperties(String fileName) { Properties properties = new Properties(); try { InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName); properties.load(is); } catch

redis分布式锁

一个人想着一个人 提交于 2019-12-03 04:49:46
可靠性 首先,为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件: 互斥性。在任意时刻,只有一个客户端能持有锁。 不会发生死锁。即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户端能加锁。 具有容错性。只要大部分的Redis节点正常运行,客户端就可以加锁和解锁。 解铃还须系铃人。加锁和解锁必须是同一个客户端,客户端自己不能把别人加的锁给解了。 上锁: ublic class RedisTool { private static final String LOCK_SUCCESS = "OK"; private static final String SET_IF_NOT_EXIST = "NX"; private static final String SET_WITH_EXPIRE_TIME = "PX"; /** * 尝试获取分布式锁 * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @param expireTime 超期时间 * @return 是否获取成功 */ public static boolean tryGetDistributedLock(Jedis jedis, String lockKey, String requestId, int

利用Redis BitMap 统计用户活跃指标

青春壹個敷衍的年華 提交于 2019-12-03 04:16:25
bitMap原理 : 如下: index 从 0 到 9 ,依次对应到一个bit位上,如果index 代表用户id,bit位上的0 1分表 代表用户是否登录; 1 0 1 1 0 1 1 0 1 1 0 1 2 3 4 5 6 7 8 9 redis数据结构中 string 类型,包含了对bitmap的实现;在redis-cli中,可以通过setbit getbit 来对bit进行操作;本文通过jedis来对redis进行操作; BitSet工具类:实现对通过jedis.get(key)取出的byte[]值与BitSet的转换 public class BitSetUtils { /** * 将BitSet对象转化为ByteArray * @param bitSet * @return */ public static byte[] bitSet2ByteArray(BitSet bitSet) { byte[] bytes = new byte[bitSet.size() / 8]; for (int i = 0; i < bitSet.size(); i++) { int index = i / 8; int offset = 7 - i % 8; bytes[index] |= (bitSet.get(i) ? 1 : 0) << offset; } return bytes

Redis performance on a multi core CPU

那年仲夏 提交于 2019-12-03 03:59:09
问题 I am looking around redis to provide me an intermediate cache storage with a lot of computation around set operations like intersection and union. I have looked at the redis website, and found that the redis is not designed for a multi-core CPU. My question is, Why is it so ? Also, if yes, how can we make 100% utilization of CPU resources with redis on a multi core CPU's. 回答1: I have looked at the redis website, and found that the redis is not designed for a multi-core CPU. My question is,

在使用代码连接redis集群时报:no reachable node in cluster,解决办法

二次信任 提交于 2019-12-03 03:28:04
通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool 一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使用redis客户端可以连接集群(我使用的redis desktop manager) 在java中通过jedis连接redis单机也成功,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool, 我以命令行方式操作是没问题的,如下: 先贴代码: <!-- redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</version> </dependency> //相关代码如下: JedisPoolConfig config = new JedisPoolConfig(); config =new JedisPoolConfig(); config

Configure Jedis timeout

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having problems completing an .hgetall, here's what I've tried: Jedis jedis = new Jedis(REDIS_MASTER_NODE); jedis.connect(); jedis.configSet("timeout", "30"); Map<String, String> alreadyStored = jedis.hgetAll(redisTargetHash); and here's what I get: Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out at redis.clients.jedis.Protocol.process(Protocol.java:79) at redis.clients.jedis.Protocol.read(Protocol.java:131) at redis.clients.jedis.Connection