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.4 安装rabbitmq-3.7.x
1.使用阿里源以加速:配置方式 2.安装最新的erlang(2018年5月16日 安装的是 Erlang20.3.x) wget https://packages.erlang-solutions.c ...
- RHEL7 Apache 服务测试
把防火墙和selinux关闭,这样试验过程中就不用配置相关策略了. 实验一.安装apache,并提供服务 在RHEL1上 #yum install -y httpd #echo basictest & ...
- 解决inline-block上下不对齐
一开始都是行内元素,但是左边按钮和右边标题就是对不齐,于是,设置左边按钮float:left就可以了. <header class="right-header"> &l ...
- 【centos6】给php命令设置全局变量三种方式
方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/my ...
- Python面向对象几个知识点
一.判断对象是否属于类.判断类是否派生自某个类 class Zero: pass class One(Zero): pass class Two(One, Zero): pass # 判断对象是否完全 ...
- 如何从windows中拷贝文件到linux (ubuntu)??
安装ssh,然后用客户端似的界面远程手动拖动即可.
- 浅谈 iOS 与 H5 的交互- JavaScriptCore 框架
前言 小的作为一个iOS程序猿,可能研究JavaScript以及H5相关的知识并不是为了真正的要去转行做这一方面,其实更多的为了要研究OC中的JavaScriptCore框架,JavaScriptCo ...
- Intel Edison学习笔记(一)—— 刷系统
一.下载安装包 1.固件安装包:官网下载地址:http://downloadmirror.intel.com/ ... image-ww25.5-15.zip2 2.烧录工具下载地址:http://d ...
- jQuery插件EasyDrag轻松实现JS拖动的效果
jquery.easydrag下载地址:http://ishare.iask.sina.com.cn/f/34289681.html 实现效果图:分布实现一.页面Html标签元素定义 <!doc ...
- 【Linux】统计命令wc
如果我想要知道 /etc/man.config 这个文件里面有多少字?多少行?多少字符的话, 可以怎么做呢?其实可以利用 wc 这个命令来达成喔!他可以帮我们计算输出的信息的整体数据! [root@w ...