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可以快速的启动服务或构建应 ...
随机推荐
- Unity2017烘焙参数设置
- 动态规划3--Help Jimmy
动态规划3--Help Jimmy 一.心得 二.题目 三.分析 Jimmy跳到一块板上后,可以有两种选择,向左走,或向右走.走到左端和走到右端所需的时间,是很容易算的.如果我们能知道,以左端为起点到 ...
- Interactive Reporting , SQL*Net not loaded successfully 问题的解决。
.bashrc 什么的,早就把 TNS_ADMIN , 和 LD_LIBRARY_PATH 加进去了,可就是不好使. 终极方法还是在 workspace 的 R&A -> Service ...
- SpringMVC是如何逐步简化Servlet的编程的
转自:https://www.cnblogs.com/winterfells/p/8476759.html Servlet和JSP是开发java Web应用程序的两种基本技术,Spring MVC是S ...
- Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)
Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1. 内联样式 ...
- LeetCode--118--杨辉三件I
问题描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2 ...
- LeetCode--104--二叉树的最大深度
问题描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null, ...
- 数据库SQL优化(百万级数据库优化方案)
1. 对查询进行优化 要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2. where 子句 要尽量避免在 where 子句中对字段进行 null 值判断.( ...
- OC Foundation框架—集合
Foundation框架—集合 一.NSArray和NSMutableArray (一)NSArray不可变数组 (1)NSArray的基本介绍 NSArray是OC中使用的数组,是面向对象的,以面向 ...
- linux和window是文件挂载
1. 首先在windows下面创建share文件夹并设置共享(右键->属性->共享)2. 确认ubuntu安装了mount.cifs,apt-get install mount.cifs ...