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");
}
}
使用RedisTemplate
1、引入依赖:
<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的更多相关文章

  1. JedisCluster和springboot整合

    maven依赖 springboot整合jedisCluster相当简单,maven依赖如下: <dependency> <groupId>org.springframewor ...

  2. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  3. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  4. springboot整合mq接收消息队列

    继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...

  5. springboot整合mybaits注解开发

    springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...

  6. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  7. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  8. SpringBoot整合Kafka和Storm

    前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...

  9. SpringBoot整合SpringCloud搭建分布式应用

    什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...

随机推荐

  1. JS + flash 复制

    js代码ZeroClipboard组件制作复制剪切板复制粘贴文字内容,一键即可复制粘贴文字内容.兼容各大主流浏览器firefox,,Chrome,IE等. 演示代码 如下: <script ty ...

  2. SpringMVC实现多种数据类型绑定

    绑定基本数据类型 Java基本数据类型int的默认值是0,在使用int进行url传递参数时,参数key是必须写的,其值也只能是int类型的,否则将会报错. 比如方法: @RequestMapping( ...

  3. English trip -- Review Unit3 Family 家人

    Words daughter grandfather grandmother husband wife uncle aunt brother sister Who is ...? Loki's ... ...

  4. windows下线程间的通信方式

    1.事件: (在信息交换函数中将控件的值与控件id进行绑定,这样我们就可以更新或者获取控件的值) void CMy0722ThreadTalkingDlg::DoDataExchange(CDataE ...

  5. 怎么使用response.write来做一个javascript的alert弹出窗口

    Page.RegisterStartupScript("alert", "<script language=javascript>alert('添加成功'); ...

  6. 全源最短路径 - floyd算法 - O(N ^ 3)

    Floyd-Warshall算法的原理是动态规划. 设Di,j,k为从i到j的只以(1..k)集合中的节点为中间节点的最短路径的长度. 若最短路径经过点k,则Di,j,k = Di,k,k − 1 + ...

  7. 微信公众号平台开发(二)信息的分类.md

    在上一篇博客中,我们只是简单地与微信服务器建立了连接,接下来就是从微信服务器中接收信息了.在SecurityController中,我定义了两个方法(get和post).Get方法是我们用来与微信服务 ...

  8. Jquery中bind(), live(), on(), delegate()四种注册事件的优缺点,建议使用on()

    jquery中注册的事件,注册事件很容易理解偏差,叫法不一样.我第一反应就是如何添加事件,使用onclick之类的,暂时不讨论js注册事件的方法. 也看到园内前辈写过相关的帖子,但不是很详细,我找到了 ...

  9. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

  10. bzoj3105

    题解: 一道博弈论 题目要求取得最少,那么就是留下的最多 把石子从大到小排序 从打的开始刘 如果可以留,那么就留下了 如果留下了与前面留下来的异或后不为0,那么就可以留 代码: #include< ...