jedis

Redis 分布式锁的正确实现方式(Java版)[转]

▼魔方 西西 提交于 2019-12-04 11:07:53
本文来源: https://www.cnblogs.com/linjiqin/p/8003838.html 前言 分布式锁一般有三种实现方式: 数据库乐观锁; 基于Redis的分布式锁; 基于ZooKeeper的分布式锁 本篇博客将介绍第二种方式,基于Redis实现分布式锁。 虽然网上已经有各种介绍Redis分布式锁实现的博客,然而他们的实现却有着各种各样的问题,为了避免误人子弟,本篇博客将详细介绍如何正确地实现Redis分布式锁。 可靠性 首先,为了确保分布式锁可用,我们至少要确保锁的实现同时满足以下四个条件: 互斥性 。在任意时刻,只有一个客户端能持有锁。 不会发生死锁 。即使有一个客户端在持有锁的期间崩溃而没有主动解锁,也能保证后续其他客户端能加锁。 具有容错性 。只要大部分的Redis节点正常运行,客户端就可以加锁和解锁。 解铃还须系铃人 。加锁和解锁必须是同一个客户端,客户端自己不能把别人加的锁给解了。 代码实现 组件依赖 首先我们要通过Maven引入Jedis开源组件,在pom.xml文件加入下面的代码: < dependency> < groupId>redis.clientsgroupId> < artifactId>jedisartifactId> < version>2.9.0version> > 加锁代码 正确姿势 Talk is cheap, show me

Redis/Jedis - Delete by pattern?

醉酒当歌 提交于 2019-12-04 10:32:29
问题 Normally, I get the key set then use a look to delete each key/value pair. Is it possible to just delete all keys via pattern? ie: Del sample_pattern:* 回答1: It seems, for Jedis, to "delete by pattern" is basically getting all the keys of a specific pattern then loop through it. ie Set<String> keys = jedis.keys(pattern); for (String key : keys) { jedis.del(key); } 回答2: KEYS is not recommended to use due to its inefficiencies when used in production. Please see https://redis.io/commands/keys.

Jedis连接redis

可紊 提交于 2019-12-04 09:09:19
今天与大家分享下,Jedis连接池使用。先看一段JAVA 代码: JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(100); JedisPool pool = new JedisPool(config, "ip地址", 6379); return pool.getResource(); 这段代码是最简单连接redis的连接池代码,单机连接,存在单点故障。不过也看这个IP是否是VIP, redis可以做成HA模式。架构如下图: redis 两台服务器,通过专业的HA软件实现主从管理,对外通过VIP提供服务,但主机宕机,HA会切换到从机运行,同时改变从机的角色为: master.。 这种架构不适合做读写分离,只有一台机器ONLINE 状态。 Redis 分片架构,先看图。典型的分片架构如下图: 该架构适合做并发较高,访问量大,如果采用jedis连接,可以实现Hash 一致性数据分布。具体代码如下: JedisPoolConfig config = new JedisPoolConfig(); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo("10.20.15.236"

Jedis使用

孤者浪人 提交于 2019-12-04 08:48:28
一.jedis使用 jedis是Redis官方首选的java客户端开发包 1.下载jedis的jar包 common-pools2-2.3.jar jedis-2.7.0.jar 2.eclipse下新建java工程,lib下添加添加jedis的jar包(也可以在pom.xml文件中添加jedis的依赖包) 3.新建java类,进行jedis测试 1)单实例测试 2)连接池连接 public void JedisDemo1(){ @Test /*** 使用连接池方式连接 ***/ public void demo1(){ //获得连接池的配置对象 JedisPoolConfig config = new JedisPoolConfig(); // 获得最大连接数 config.SetMaxTotal(30); // 获得最大空闲连接数 config.setMaxIdle(10); // 获得连接池: JedisPool jedisPool = new JedisPool(config,XXX.XXX.XX.XX,6379); //获得核心对象 Jedis jedis = null; try{ //通过连接池获得连接 jedis = jedisPool.getResource(); jedis.set("name","卧龙boy"); String value = jedis.get

beanFactory 设计模式 Bean 生命周期

倾然丶 夕夏残阳落幕 提交于 2019-12-04 08:28:54
写在前面的话 适用读者:有一定经验的,本文不适合初学者,因为可能不能理解我在说什么 文章思路:不会一开始就像别的博客文章那样,Bean 的生命周期,源码解读(给你贴一大堆的源码)。个人觉得应该由问题驱动,为什么为出现 BeanFactory ,为什么会有生命周期。 正文 一开始我们使用 bean 都是简单bean,如 vo ,po,entity,dto,我们是这么玩的 XXEntity xxEntity = new XXEntity(); xxEntity.setPropA("字符串"); 后面可能出现了某个比较复杂的 bean ,它有一个对象做为属性,需要在构造时或构造后设置值(示例而已,不要较真),如 // 构建序列化实例,这里 Serializable 是接口,使用接口的好处是在使用别的序列化时,不需要修改 jedis 类 Serializable fastJsonSerizlizable = new FastJsonSerizlizable(); // 构建目标 jedis 实例 ,需要先构建序列化对象 Jedis jedis = new Jedis(); jedis.setSerializable(fastJsonSerizlizable); 这时来了 serviceA 类和 serviceB 类,它们都需要使用 redis,我不可能在每个类里面都去把 jedis

大白话布隆过滤器

梦想的初衷 提交于 2019-12-04 07:50:00
大白话布隆过滤器 https://www.cnblogs.com/CodeBear/p/10911177.html 本文是站在小白的角度去讨论布隆过滤器,如果你是科班出身,或者比较聪明,又或者真正想完全搞懂布隆过滤器的可以移步。 不知道从什么时候开始,本来默默无闻的布隆过滤器一下子名声大燥,仿佛身在互联网,做着开发的,无人不知,无人不晓,哪怕对技术不是很关心的小伙伴也听过它的名号。我也花了不少时间去研究布隆过滤器,看了不少博客,无奈不是科班出身,又没有那么聪明的头脑,又比较懒...经过“放弃,拿起,放弃,拿起”的无限轮回,应该算是了解了布隆过滤器的核心思想,所以想给大家分享下。 布隆过滤器的应用 我们先来看下布隆过滤器的应用场景,让大家知道神奇的布隆过滤器到底能做什么。 缓存穿透 我们经常会把一部分数据放在Redis等缓存,比如产品详情。这样有查询请求进来,我们可以根据产品Id直接去缓存中取数据,而不用读取数据库,这是提升性能最简单,最普遍,也是最有效的做法。一般的查询请求流程是这样的:先查缓存,有缓存的话直接返回,如果缓存中没有,再去数据库查询,然后再把数据库取出来的数据放入缓存,一切看起来很美好。但是如果现在有大量请求进来,而且都在请求一个不存在的产品Id,会发生什么?既然产品Id都不存在,那么肯定没有缓存,没有缓存,那么大量的请求都怼到数据库,数据库的压力一下子就上来了

Connect Redis cluster with jedis

試著忘記壹切 提交于 2019-12-04 07:33:16
Since a single redis instance doesn't meet my requirements, I went for redis cluster. I formed cluster with three nodes and populated data into the cluster. When I get data from cluster using JedisCluster it takes more time than the single instance. So, what's the proper way to connect jedis with redis cluster. How can I make use of connection pool to connect jedis with redis cluster? It's normal that you observe latency when you retrieve datas in a jedis cluster depending of the clustering strategy that you have because you will bonce from one jedis to another in order to take the datas. The

Redis中的键值过期操作

廉价感情. 提交于 2019-12-04 06:50:05
1.过期设置 Redis 中设置过期时间主要通过以下四种方式: expire key seconds:设置 key 在 n 秒后过期; pexpire key milliseconds:设置 key 在 n 毫秒后过期; expireat key timestamp:设置 key 在某个时间戳(精确到秒)之后过期; pexpireat key millisecondsTimestamp:设置 key 在某个时间戳(精确到毫秒)之后过期; 下面分别来看以上这些命令的具体实现。 1)expire:N 秒后过期 127.0.0.1:6379> set key value OK 127.0.0.1:6379> expire key 100 (integer) 1 127.0.0.1:6379> ttl key (integer) 97 其中命令 ttl 的全称是 Time To Live 表示此键值在 n 秒后过期。例如,上面的结果 97 表示 key 在 97s 后过期。 2)pexpire:N 毫秒后过期 127.0.0.1:6379> set key2 value2 OK 127.0.0.1:6379> pexpire key2 100000 (integer) 1 127.0.0.1:6379> pttl key2 (integer) 94524 其中 pexpire key2

Redis中的键值过期操作

∥☆過路亽.° 提交于 2019-12-04 06:49:43
1.过期设置 Redis 中设置过期时间主要通过以下四种方式: expire key seconds:设置 key 在 n 秒后过期; pexpire key milliseconds:设置 key 在 n 毫秒后过期; expireat key timestamp:设置 key 在某个时间戳(精确到秒)之后过期; pexpireat key millisecondsTimestamp:设置 key 在某个时间戳(精确到毫秒)之后过期; 下面分别来看以上这些命令的具体实现。 1)expire:N 秒后过期 127.0.0.1:6379> set key value OK 127.0.0.1:6379> expire key 100 (integer) 1 127.0.0.1:6379> ttl key (integer) 97 其中命令 ttl 的全称是 Time To Live 表示此键值在 n 秒后过期。例如,上面的结果 97 表示 key 在 97s 后过期。 2)pexpire:N 毫秒后过期 127.0.0.1:6379> set key2 value2 OK 127.0.0.1:6379> pexpire key2 100000 (integer) 1 127.0.0.1:6379> pttl key2 (integer) 94524 其中 pexpire key2

Can redis pipeline multiple commands that depend on previous ones?

荒凉一梦 提交于 2019-12-04 06:18:12
I'm very very new to redis and still playing around with it. I want to test to see if its relevant to my project but I'm not sure about a specific command I'm running. The users on SO have got me convinced of the performance benefits of using pipelines and transactions so I thought I'd ask how to do this. Basically I have two statements that I just want to issue and not have to wait for the result(seems like a good candidate for pipe lining. It looks like this: Does valueX exist? If it does insert valueY Its pretty simple but so far all the ways I have been looking into it seem to wait for a