springboot整合jedisCluster
maven依赖
springboot整合jedisCluster相当简单,maven依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
加了这一个依赖之后就不要再加上jedis的这一个依赖了:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
加这个可能在本身测试的时候,可能会导致jedisCluster对象正常,但是在测试的时候会发现set数据的时候会出现问题,我把jedis的依赖去掉之后,这个问题解决,因此不要加上jedis的这一个依赖,spring-boot-starter-redis这一个引入相关jedis需要的包。
application.properties配置
这里的配置相当简单,只需要天上redis的相关地址就行了,如下:
#redis cluster
spring.redis.cache.clusterNodes=192.168.xx.xx:6379,192.168.xx.:6380,192.168.xx.xx:6381
spring.redis.cache.commandTimeout=5000
定义一个类命名问RedisProperties,在里面定义的字段与配置文件中相对应,即可取到配置,如下:
@Component
@ConfigurationProperties(prefix = "spring.redis.cache")
@Data
public class RedisProperties { private String clusterNodes;
private Integer commandTimeout;
}
JedisClusterConfig
@Configuration
@ConditionalOnClass({JedisCluster.class})
@EnableConfigurationProperties(RedisProperties.class)
public class JedisClusterConfig { @Inject
private RedisProperties redisProperties; @Bean
@Singleton
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());
}
}
配置就完成,现在进行测试一次。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootWebApplication.class)
@WebAppConfiguration
public class TestJedisCluster { @Inject
private JedisCluster jedisCluster; @Test
public void testJedis() {
jedisCluster.set("test_jedis_cluster", "38967");
Assert.assertEquals("38967", jedisCluster.get("test_jedis_cluster"));
jedisCluster.del("test_jedis_cluster");
}
}
使用RedisTemplate1、引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置文件application.yml在添加配置(假设有6个nodes):
spring:
redis:
cluster:
nodes:
- 192.168.0.17:6390
- 192.168.0.17:6391
- 192.168.0.17:6392
- 192.168.0.9:6390
- 192.168.0.9:6391
- 192.168.0.9:6392
代码测试:
@Autowired
RedisTemplate<String, String> redisTemplate; @Test
public void redisTest() {
String key = "redisTestKey";
String value = "I am test value"; ValueOperations<String, String> opsForValue = redisTemplate.opsForValue(); //数据插入测试:
opsForValue.set(key, value);
String valueFromRedis = opsForValue.get(key);
logger.info("redis value after set: {}", valueFromRedis);
assertThat(valueFromRedis, is(value)); //数据删除测试:
redisTemplate.delete(key);
valueFromRedis = opsForValue.get(key);
logger.info("redis value after delete: {}", valueFromRedis);
assertThat(valueFromRedis, equalTo(null));
}
springboot整合jedisCluster的更多相关文章
- JedisCluster和springboot整合
maven依赖 springboot整合jedisCluster相当简单,maven依赖如下: <dependency> <groupId>org.springframewor ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
随机推荐
- HDU 6098 Inversion
Inversion 思路:从大到小排序后,每次找到第一个下标不整出i的输出. 代码: #include<bits/stdc++.h> using namespace std; #defin ...
- apache安装方式
1.首先需要下载apachi,apache_2.2.22.msi 2.双击安装包进行安装,安装过程中可能出现一些选择性问题,将重点部分截图如下: 说明:这三个内容就按如上输入即可. 说明:选择自定义安 ...
- eclipse 里,打开的文件的各个标签,标题乱码。
Eclipse Mars 版本有这个问题. 提交到社区了,也还是没有人回答.Eclipse看来是没落了. https://www.eclipse.org/forums/index.php/t/1072 ...
- 雷林鹏分享:Ruby 环境变量
Ruby 环境变量 Ruby 解释器使用下列环境变量来控制它的行为.ENV 对象包含了所有当前设置的环境变量列表. 变量描述 DLN_LIBRARY_PATH动态加载模块搜索的路径. HOME当没有参 ...
- LeetCode--226--翻转二叉树
问题描述: 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howel ...
- spring--mvc用戶注册用户名验重
spring--mvc用戶注册用户名验重 注册是验证用户名是否重复.post方法,当表单的用户名文本框失去焦点时,由ajax方法指定,进行@RequestMapping指定的url提交时调用的方法. ...
- javassist和jdk动态代理
先来一个InvocationHandler示例,InvocationHandler类的作用是:对原始对象的方法做一个拦截. package com.zhang; import java.lang.re ...
- java反编译工具eclipse插件jad的使用
https://www.cnblogs.com/zhikou/p/8098137.html 这边文章主要介绍如何使用Eclipse的插件jad进行反编译,在查看一些jar包里面的class文件时,就可 ...
- kill word fore out
1● fore f ɔ: 预先,前面
- 快速切题 hdu2416 Treasure of the Chimp Island 搜索 解题报告
Treasure of the Chimp Island Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...