spring boot下JedisCluster方式连接Redis集群的配置
最近在使用springboot做项目,使用redis做缓存。在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器。但是项目代码挪到内网开发以后,内网redis服务器使用了集群的配置方式。所以原来的配置文件和帮助类 都完全不能使用了,所以最近对redis集群的配置进行了简单的研究。
1.首先是引入配置文件
gradle方式的配置文件
compile 'redis.clients:jedis:2.9.0'
2.application.yml的配置
spring:
application:
name: xxxx
session:
store-type: redis
redis:
password: xxxxx
clusterNodes: xxxxxxx
expireSeconds: 120
commandTimeout: 10000 #redis操作的超时时间
pool:
maxActive: 5000 #最大连接数
maxIdle: 30 #最大空闲连接数
minIdle: 5 #最小空闲连接数
maxWait: 3000 #获取连接最大等待时间 ms #default -1
3.新增类RedisProperties
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties { private int expireSeconds;
private String clusterNodes;
private String password;
private int commandTimeout;
}
4.JedisCluster方式连接集群的配置
@Configuration
public class JedisClusterConfig { @Autowired
private RedisProperties redisProperties; /**
* 注意:
* 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
* @return
*/
@Bean
public JedisCluster getJedisCluster() {
String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)
Set<HostAndPort> nodes = new HashSet<>(); for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
} return new JedisCluster(nodes,redisProperties.getCommandTimeout(),1000,1,redisProperties.getPassword() ,new GenericObjectPoolConfig());//需要密码连接的创建对象方式
} }
5.redis帮助
@Component
public class RedisUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); @Autowired
private JedisCluster jedisCluster; /**
* 设置缓存
* @param key 缓存key
* @param value 缓存value
*/
public void set(String key, String value) {
jedisCluster.set(key, value);
LOGGER.debug("RedisUtil:set cache key={},value={}", key, value);
} /**
* 设置缓存对象
* @param key 缓存key
* @param obj 缓存value
*/
public <T> void setObject(String key, T obj , int expireTime) {
jedisCluster.setex(key, expireTime, JSON.toJSONString(obj));
} /**
* 获取指定key的缓存
* @param key---JSON.parseObject(value, User.class);
*/
public String getObject(String key) {
return jedisCluster.get(key);
} /**
* 判断当前key值 是否存在
*
* @param key
*/
public boolean hasKey(String key) {
return jedisCluster.exists(key);
} /**
* 设置缓存,并且自己指定过期时间
* @param key
* @param value
* @param expireTime 过期时间
*/
public void setWithExpireTime( String key, String value, int expireTime) {
jedisCluster.setex(key, expireTime, value);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", key, value, expireTime);
} /**
* 获取指定key的缓存
* @param key
*/
public String get(String key) {
String value = jedisCluster.get(key);
LOGGER.debug("RedisUtil:get cache key={},value={}",key, value);
return value;
} /**
* 删除指定key的缓存
* @param key
*/
public void delete(String key) {
jedisCluster.del(key);
LOGGER.debug("RedisUtil:delete cache key={}", key);
} }
6.帮助类的使用方式
@Autowired
private RedisUtil redisUtil;
spring boot下JedisCluster方式连接Redis集群的配置的更多相关文章
- redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
- 通过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 但是使 ...
- 使用DBeaver Enterprise连接redis集群的一些操作记录
要点总结: 使用DBeaver Enterprise连接redis集群可以通过SQL语句查看key对应的value,但是没法查看key. 使用RedisDesktopManager连接redis集群可 ...
- lua连接redis集群
连接redis集群需要用到llua-resty-redis-cluster模块 github地址:https://github.com/cuiweixie/lua-resty-redis-cluste ...
- redis单点、redis主从、redis哨兵sentinel,redis集群cluster配置搭建与使用
目录 redis单点.redis主从.redis哨兵 sentinel,redis集群cluster配置搭建与使用 1 .redis 安装及配置 1.1 redis 单点 1.1.2 在命令窗口操作r ...
- Redis集群的配置
[转]Redis集群的配置 一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [red ...
- Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)
文/朱季谦 背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯 ...
- windows下eclipse远程连接hadoop集群开发mapreduce
转载请注明出处,谢谢 2017-10-22 17:14:09 之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...
- java 连接 redis集群时报错:Could not get a resource from the pool
由于弄这个的时候浪费了太多的时间,所以才记录下这个错,给大伙参考下 检查了一下,配置啥的都没问题的,但在redis集群机器上就可以,错误如下: Exception in thread "ma ...
随机推荐
- V-rep中的加速度计与陀螺仪
加速度计(Accelerometer) VREP的模型浏览器components→sensors中可以找到加速度计的模型,用于测量物体沿着世界坐标系三个坐标轴的加速度值. VREP中没有直接测量加速度 ...
- python模块之importlib(py3中功能有明显加强)
# -*- coding: utf-8 -*-#python 27#xiaodeng#python模块之importlib(py3中功能有明显加强)
- 一个成功的 Git 分支模型
在这篇文章中介绍的开发模型在大约一年前已经在我的私有项目和工作引入的,而且已经被证明是非常成功的.我想写一些关于这个模型的东西已经好一段时间了,但是一直苦于没有时间,不过现在可以了.我不想探讨任何项目 ...
- Docker命令分类及使用场景分布(脑图)
常见的Docker命令分类主要有 不同使用场景下的命令分布 有疑问可到官方文档查询: https://docs.docker.com/engine/reference/commandline/dock ...
- Dubbo在开发中的一些常用配置
介绍Dubbo在开发中的一些常用配置,文中内容主要参考dubbo文档配置和示例两节,详细可移步访问 传送站 1. 属性配置方法及加载顺序 属性常用配置方法主要有三种: 第一种是通过启动时在虚拟机参数 ...
- ios 中sqlite的用法
#import <sqlite3.h> @interface ViewController () { sqlite3 *_sqldb; } @end @implementation Vie ...
- Ubuntu 12.10安装QQ2012
[日期:2012-11-05] 在最新的Ubuntu 12.10下安装QQ2012,请根据自己的机器类型下载后按照下面的32位或64位安装说明安装. 下载网址:http://www.longene.o ...
- 使用T-SQL语句操作视图
转自:使用T-SQL语句操作视图 提示:只能查看,删除,创建视图,不能对数据进行增,删,改操作. use StuManageDB go --判断视图是否存在 if exists(Select * fr ...
- 设置树莓派SSH连接因超时闲置断开(转)
设置树莓派SSH连接因超时闲置断开 转自:http://shumeipai.nxez.com/2013/12/06/set-ssh-connection-is-disconnected-due-to- ...
- 【LeetCode】217. Contains Duplicate (2 solutions)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...