jedis

Redis实现分布式锁(设计模式应用实战)

风格不统一 提交于 2020-07-26 21:35:23
笔者看过网络上各种各样使用redis实现分布式锁的代码,要么错误,要么片段化,没有一个完整的例子,借这个周末给大家总结一下redis实现分布式锁的两种机制 自旋锁和排他锁 鉴于实现锁的方式不同,那么这里使用策略模式来组织代码 一、自旋锁 分布式锁抽象策略接口 package com.srr.lock; /** * @Description 分布式锁的接口 */ abstract public interface DistributedLock { /** * 获取锁 */ boolean lock(); /** * 解锁 */ void unlock(); } 自旋锁策略抽象类,使用模板方法模式构建 package com.srr.lock; /** * 自旋锁策略模板 */ public abstract class SpinRedisLockStrategy implements DistributedLock { private static final Integer retry = 50; // 默认重试5次 private static final Long sleeptime = 100L ; protected String lockKey; protected String requestId; protected int expireTime; private

基于Redis的分布式锁实现(转)

喜夏-厌秋 提交于 2020-07-24 11:21:11
前言 本篇文章主要介绍基于Redis的分布式锁实现到底是怎么一回事,其中参考了许多大佬写的文章,算是对分布式锁做一个总结 分布式锁概览 在多线程的环境下,为了保证一个代码块在同一时间只能由一个线程访问,Java中我们一般可以使用synchronized语法和ReetrantLock去保证,这实际上是本地锁的方式。但是现在公司都是流行分布式架构,在分布式环境下,如何保证不同节点的线程同步执行呢? 实际上,对于分布式场景,我们可以使用分布式锁,它是控制分布式系统之间 互斥访问共享资源 的一种方式。 比如说在一个分布式系统中,多台机器上部署了多个服务,当客户端一个用户发起一个数据插入请求时,如果没有分布式锁机制保证,那么那多台机器上的多个服务可能进行并发插入操作,导致数据重复插入,对于某些不允许有多余数据的业务来说,这就会造成问题。而分布式锁机制就是为了解决类似这类问题,保证多个服务之间互斥的访问共享资源,如果一个服务抢占了分布式锁,其他服务没获取到锁,就不进行后续操作。大致意思如下图所示(不一定准确): 分布式锁的特点 分布式锁一般有如下的特点: 互斥性: 同一时刻只能有一个线程持有锁 可重入性: 同一节点上的同一个线程如果获取了锁之后能够再次获取锁 锁超时:和J.U.C中的锁一样支持锁超时,防止死锁 高性能和高可用: 加锁和解锁需要高效,同时也需要保证高可用,防止分布式锁失效

Could not get a resource from the pool(SocketTimeoutException:)

孤街醉人 提交于 2020-07-18 08:44:13
问题 I'm running multiple worker threads(around 10) to access the data from the redis Q. For the i'm using infinte timeout for Jedis Client . Jedis jedis = pool.getResource(); jedis.getClient().setTimeoutInfinite(); Still i'm getting the error "Could not get a resource from the pool". The stacktrace is given below. redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) at Workers.Worker1.met1(Worker1.java

Could not get a resource from the pool(SocketTimeoutException:)

99封情书 提交于 2020-07-18 08:42:11
问题 I'm running multiple worker threads(around 10) to access the data from the redis Q. For the i'm using infinte timeout for Jedis Client . Jedis jedis = pool.getResource(); jedis.getClient().setTimeoutInfinite(); Still i'm getting the error "Could not get a resource from the pool". The stacktrace is given below. redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:22) at Workers.Worker1.met1(Worker1.java

Unable to get result from the Redis using Crud Repository in Spring Boot?

走远了吗. 提交于 2020-07-05 16:24:55
问题 I am developing Spring Boot + Redis example. I've taken a reference from link : https://www.baeldung.com/spring-data-redis-tutorial. In this example, I developed repository method Student findByNameAndGender(String name, Gender gender); and even the Student findByName(String name); , but I am not getting any result back on both cases.? Any quick help? Redis query - redis 127.0.0.1:6379> KEYS * 1) "Student" 2) "Student:bb4df14a-7f42-4fc3-b608-fc4b7d45109e" 3) "Student:69affaa4-e56c-49e3-9ef4

Unable to get result from the Redis using Crud Repository in Spring Boot?

风流意气都作罢 提交于 2020-07-05 16:23:08
问题 I am developing Spring Boot + Redis example. I've taken a reference from link : https://www.baeldung.com/spring-data-redis-tutorial. In this example, I developed repository method Student findByNameAndGender(String name, Gender gender); and even the Student findByName(String name); , but I am not getting any result back on both cases.? Any quick help? Redis query - redis 127.0.0.1:6379> KEYS * 1) "Student" 2) "Student:bb4df14a-7f42-4fc3-b608-fc4b7d45109e" 3) "Student:69affaa4-e56c-49e3-9ef4

Using Jedis pool in JAX-RS app running in Quarkus native results in ClassNotFoundException: org.apache.commons.pool2.impl.DefaultEvictionPolicy

落爺英雄遲暮 提交于 2020-06-28 03:54:28
问题 I'm trying to use JedisPool in application run in Quarkus native mode (works fine in JVM mode). I've already disabled JMX feature of the pool, which is not avaliable in native mode, like this: JedisPoolConfig jedisConfiguration = new JedisPoolConfig(); jedisConfiguration.setJmxEnabled(false); jedisPool = new JedisPool(jedisConfiguration, jedisURI); However I'm hitting following error: 2020-04-29 17:35:37,724 INFO [test.StockQuote] (main) java.lang.IllegalArgumentException: Unable to create

Dubbo(七):redis注册中心的应用

徘徊边缘 提交于 2020-05-09 16:22:33
  上篇我们讲了Dubbo中有一个非常本质和重要的功能,那就是服务的自动注册与发现,而这个功能是通过注册中心来实现的。上篇中使用zookeeper实现了注册中心的功能,同时了提了dubbo中有其他许多的注册中心的实现。   今天我们就来看看另一个注册中心的实现吧: redis 1. dubbo在 Redis 中的服务分布   dubbo在zk中的服务体现是一个个的文件路径形式,如 /dubbo/xxx.xx.XxxService/providers/xxx 。 而在redis中,则体现是一个个的缓存key-value。具体分布如下:     /dubbo/xxx.xx.XxxService/providers: 以hash类型存放所有提供者列表, 每个hash的字段为 url -> expireTime     /dubbo/xxx.xx.XxxService/consumers: 以hash类型存放所有消费者列表, 每个hash的字段为 url -> expireTime     /dubbo/xxx.xx.XxxService/configurators: 存放配置信息     /dubbo/xxx.xx.XxxService/routers: 存放路由配置信息   如上,同样,redis也是以service为粒度进行存储划分的。 2. Redis 组件的接入  

redis在java客户端的操作

元气小坏坏 提交于 2020-05-09 09:54:32
redis高性能,速度快,效率高的特点,用来做缓存服务器是很不错的选择。(和memcache相似) redis在客户端的操作步骤: 1.redis单机版操作 1.1通过Jedis对象操作 (1)将安装redis服务的服务器的ip地址和redis的端口号作为构造参数传递给Jedis,用来创建一个Jedis对象   Jedis jedis = new Jedis(ip,port); (2)通过第一步创建的jedis对象,操作redis的5大数据类型(hash类型,string类型,list类型,set类型,zset类型,有序)   jedis.set(string key,string value);   jedis.get(string key); (3)操作完成后关闭jedis连接   jedis.close(); 这种方法需要每次创建连接,关闭连接,比较浪费资源。因此使用下面的jedisPool连接池操作单机版redis 直接上代码:      // 创建jedis对象 Jedis jedis = new Jedis("ip", 6379 ); // 操作string数据类型 jedis.set("username", "helloworld" ); // 根据key取出对应的value值 String value = jedis.get("username" ); // 值输出

redis的jedis连接池:JedisPool

吃可爱长大的小学妹 提交于 2020-05-08 19:48:20
* 使用:    1. 创建JedisPool连接池对象    2. 调用方法 getResource()方法获取Jedis连接     //0.创建一个配置对象     JedisPoolConfig config = new JedisPoolConfig();     config.setMaxTotal(50);     config.setMaxIdle(10);     //1.创建Jedis连接池对象     JedisPool jedisPool = new JedisPool(config,"localhost",6379);     //2.获取连接     Jedis jedis = jedisPool.getResource();     //3. 使用     jedis.set("hehe","heihei");     //4. 关闭 归还到连接池中     jedis.close(); * 连接池工具类 /** JedisPool工具类 加载配置文件,配置连接池的参数 提供获取连接的方法 */ public class JedisPoolUtils { private static JedisPool jedisPool; static{ //读取配置文件 InputStream is = JedisPoolUtils.class