jedis

redis It seems like server has closed the connection

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i want to use redis sub/pub, but when i subscribe one channel, 2 minutes after,console output Exception: It seems like server has closed the connection. redis version:redis-3.0.3 jedis version:2.3.0 os:OS X Yosemite 10.10.5 Subscribe.class JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(10); jedisPoolConfig.setMaxWait(4000); jedisPoolConfig.setTestOnBorrow(true); JedisPool jedisPool = new JedisPool(jedisPoolConfig, "127.0.0.1", 6379); final Jedis jedis = jedisPool.getResource(); System.out.println(jedis

聊聊spring-data-redis的连接池的校验

我是研究僧i 提交于 2019-12-03 01:59:26
序 本文主要研究一下spring-data-redis的连接池的校验 lettuce LettucePoolingConnectionProvider spring-data-redis/2.0.10.RELEASE/spring-data-redis-2.0.10.RELEASE-sources.jar!/org/springframework/data/redis/connection/lettuce/LettucePoolingConnectionProvider.java class LettucePoolingConnectionProvider implements LettuceConnectionProvider, RedisClientProvider, DisposableBean { private static final Log log = LogFactory.getLog(LettucePoolingConnectionProvider.class); private final LettuceConnectionProvider connectionProvider; private final GenericObjectPoolConfig poolConfig; private final Map<StatefulConnection<?, ?>

Redis面试题及答案整理

家住魔仙堡 提交于 2019-12-03 01:38:33
Redis面试题及答案整理 1. Redis有哪些数据结构? 字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。 如果你是Redis中高级用户,还需要加上下面几种数据结构HyperLogLog、Geo、Pub/Sub。 2. 使用过Redis分布式锁么,它是什么回事? 先拿setnx来争抢锁,抢到之后,再用expire给锁加一个过期时间防止锁忘记了释放。 这时候对方会告诉你说你回答得不错,然后接着问如果在setnx之后执行expire之前进程意外crash或者要重启维护了,那会怎么样? 这时候你要给予惊讶的反馈:唉,是喔,这个锁就永远得不到释放了。紧接着你需要抓一抓自己得脑袋,故作思考片刻,好像接下来的结果是你主动思考出来的,然后回答:我记得set指令有非常复杂的参数,这个应该是可以同时把setnx和expire合成一条指令来用的!对方这时会显露笑容,心里开始默念:摁,这小子还不错。 jedis.set(String key, String value, String nx, String expx, int time),这个set()方法一共有五个形参: 第一个为key,我们使用key来当锁,因为key是唯一的。 第二个为value,我们传的是requestId,很多童鞋可能不明白,有key作为锁不就够了吗,为什么还要用到value

自定义对象存入Redis

匿名 (未验证) 提交于 2019-12-03 00:44:02
package com.qiyi.tvguo.cms.common; import com.alibaba.fastjson.JSON; import com.qiyi.tvguo.cms.common.utils.ObjectSerializeUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * redis client */ @Component @Slf4j public class RedisClient { private static JedisPool pool; @Value( "${redis.host}" ) private String

redis使用watch秒杀抢购思路

匿名 (未验证) 提交于 2019-12-03 00:44:02
1、使用watch,采用乐观锁 2、不使用悲观锁,因为等待时间非常长,响应慢 3、不使用队列,因为并发量会让队列内存瞬间升高 测试代码: import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import redis.clients.jedis.Jedis; /** * redis测试抢购 * * @author 10255_000 * */ public class RedisTest { public static void main(String[] args) { final String watchkeys = "watchkeys"; ExecutorService executor = Executors.newFixedThreadPool(20); final Jedis jedis = new Jedis("192.168.3.202", 6379); jedis.set(watchkeys, "0");// 重置watchkeys为0 jedis.del("setsucc", "setfail");// 清空抢成功的,与没有成功的 jedis.close(); for (int i = 0; i < 10000; i++) {//

SpringBoot+MyBatis+Redis实现SSO单点登录系统(二)

匿名 (未验证) 提交于 2019-12-03 00:44:02
配置文件配置数据库,redis等相关的信息。 # See http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html # Thymeleaf配置 spring .thymeleaf .cache =false spring .thymeleaf .mode =HTML spring .main .show -banner=false spring .thymeleaf .prefix =classpath:/templates spring .thymeleaf .suffix = .html logging .level .jdbc =OFF logging .level .jdbc .sqltiming =DEBUG logging .level .jdbc .resultsettable =DEBUG # 数据库配置 spring .datasource .driver -class-name= com .mysql .jdbc .Driver spring .datasource .url =jdbc:mysql:// 127.0 .0 .1 : 3306 /taotao?useSSL=false spring .datasource

一篇详解Redis -- 延时队列

匿名 (未验证) 提交于 2019-12-03 00:44:02
Redis的 list 数据结构常用来作为 异步消息队列 使用,使用 rpush/lpush 操作 入队 ,使用 lpop/rpop 来操作 出队 > rpush my-queue apple banana pear (integer) 3 > llen my-queue (integer) 3 > lpop my-queue "apple" > llen my-queue (integer) 2 > lpop my-queue "banana" > llen my-queue (integer) 1 > lpop my-queue "pear" > llen my-queue (integer) 0 > lpop my-queue (nil) 空队列 如果队列为空,客户端会陷入 pop的死循环 , 空轮询 不仅拉高了 客户端的CPU , Redis的QPS 也会被拉高 如果空轮询的客户端有几十个, Redis的慢查询 也会显著增加,可以尝试让客户端线程 sleep 1s 但睡眠会导致消息的 延迟增大 ,可以使用 blpop/brpop (blocking, 阻塞读 ) 阻塞读在队列没有数据时,会立即进入 休眠 状态,一旦有数据到来,会立即被 唤醒 , 消息延迟几乎为0 空闲连接 如果线程一直阻塞在那里,Redis的客户端连接就成了 闲置连接 闲置过久, 服务器 一般会 主动断开

Redis删除相同前缀的key

匿名 (未验证) 提交于 2019-12-03 00:44:02
如何优雅地删除Redis set集合中前缀相同的key? Redis中有删除单条数据的命令DEL,却没有批量删除特定前缀key的指令,但我们经常遇到需要根据前缀来删除的业务场景,那么究竟该怎么做呢?可能你一通搜索后会得到下边的答案: redis - cli -- raw keys "prefix-*" | xargs redis - cli del 直接在linux下通过redis的keys命令匹配到所有的key,然后调用系统命令xargs来删除,看似十全十美,实则风险巨大。这就是一颗随时爆炸的炸弹!我们都知道Redis是单线程服务模式,使用命令 keys * 查询key的时候会阻塞正常的业务请求,甚至造成redis宕机,所以肯定不行。 因此,我们在生产环境中应当避免使用上边的方法,那使用什么优雅的方法来解决呢?SCAN! SCAN介绍 Redis从2.8版本开始支持SCAN命令,它是一个基于游标的迭代器,每次被调用之后都会返回一个新的游标,用户在下次迭代时需要使用这个新游标作为SCAN命令的游标参数,以此来延续之前的迭代过程,直到服务器返回值为0的游标时,一次完整的遍历过程就结束了。 SCAN命令的基本语法如下: scan cursor [ MATCH pattern ] [ COUNT count ] MATCH : 匹配规则,例如遍历以ops-coffee

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

匿名 (未验证) 提交于 2019-12-03 00:44:02
解决办法:调整JedisPoolConfig中maxActive为适合自己系统的阀值。 <bean id="dataJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="300"/> <property name="maxIdle" value="100"/> <property name="maxWait" value="10000"/> <property name="testOnBorrow" value="true"/> </bean> 参考: https://www.iteye.com/topic/1122212 Jedis 对象用完以后,要释放掉,不让会一直占用,所以会出现无法获取新的资源。 由于防火墙原因无法连接到Redis IP地址或端口错误(端口未开放) 来源:博客园 作者: 21karat 链接:https://www.cnblogs.com/LJing21/p/11526952.html

redis-JedisPoolConfig配置

匿名 (未验证) 提交于 2019-12-03 00:44:02
edisPoolConfig config = new JedisPoolConfig(); //连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true config.setBlockWhenExhausted( true ); //设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数) config.setEvictionPolicyClassName( "org.apache.commons.pool2.impl.DefaultEvictionPolicy" ); //是否启用pool的jmx管理功能, 默认true config.setJmxEnabled( true ); //MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默 认为"pool", JMX不熟,具体不知道是干啥的...默认就好. config.setJmxNamePrefix( "pool" ); //是否启用后进先出, 默认true config.setLifo( true ); //最大空闲连接数, 默认8个 config.setMaxIdle( 8 );