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 ...
随机推荐
- CentOS7 使用ntp设置系统时间,开机自动设置时间,
首先如果没有安装ntp自己装一下: yum install -y ntp 然后,如果开了防火墙,记得打开自己的123端口,该端口是ntp用来同步时间的 firewall-cmd --zone=publ ...
- foxmail怎么设置个性签名
foxmail是我们日常使用的邮件客户端之一,相信很多的朋友都在使用.那么,我们在发邮件的时候,很多朋友都会使用一个个性签名,那么对于foxmail的邮件签名怎么来设置呢?今天笔者就来给大家分享一下, ...
- 转:Ogre源码剖析 - 场景管理之Octree
由于本人的引擎ProjectGaia服务于08年创新杯的游戏项目 – 3D太空游戏,所以理所应当加入Octree(八叉树 – 已经周宁学长发帖介绍过)场景管理器.参考了无数Octree的代码,发现还是 ...
- Arduino和C51开发DHT11温湿度传感器
技术:51单片机.Arduino.DHT11.温湿度传感器.传感器 概述 学习单片机离不开对传感器的学习,本文主要介绍DHT11温湿度传感器的使用方法,同学也可以了解更多的传感器,这对你以后开发产 ...
- update pm storage
BEGIN #Routine body goes here... INSERT INTO EMS_PM_STORAGE ( AMOID, GP_BEGIN_TIME, EMS_RECORD_TIME, ...
- 在子线程中发送短信,静态注册SentMsgReceiver。
1. 应该在子线程中执行发送短信的操作. 如果没有在子线程中发送短信会出现错误:点击发送短信之后,立即跳转到其他界面,那么这次发送短信可能就会失败! 请注意往子线程方法中传入外部的实参必须由final ...
- Swift3 隐藏状态栏,修改状态栏颜色
之前做法: override func viewWillAppear(_ animated: Bool) { UIApplication.shared.isStatusBarHidden = true ...
- 【转载】TCP数据包结构
最近在研究TCP协议,找了点资料,感觉很经典,所以转载过来. 如果本文中图片不能观看,请链接原始地址:http://xinxiangsui2018.blog.163.com/blog/static/1 ...
- oracle收购Mysql后,Mysql的安装配置方法
自从Oracle收购MySQL后,略微发生了一些小小的变化,原来mysql安装完成后默认是没有密码的,但是新版的mysql安装完成后oracle提供了一个free password放着/root/.m ...
- mac 终端添加颜色
1.打开终端,然后找到终端偏好设置,选择自己喜欢的颜色 2.然后切换到当前用户的家目录: cd ~ 3.打开文件,开始编辑".bash_profile", 添加下面两句 expor ...