JedisCluster和springboot整合
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
相当简单只需要几个redis的地址和端口的字符串就可以了。
redisProperties
在这里取springboot中的配置办法相当多,可以使用如下方法:
@Inject
private Environment environment;
String properties = environment.getproperties("xxx")
或者是在加上注解,@Value(“”)会在配置文件中取相关名字的配置。
但在本文中决定使用另外一种方法,定义一个类命名问RedisProperties,在里面定义的字段与配置文件中相对应,即可取到配置,如下:
@Component
@ConfigurationProperties(prefix = "spring.redis.cache")
@Data
public class RedisProperties { private String clusterNodes;
private Integer commandTimeout;
}
如上,在使用时就能正常取到相关配置。
JedisClusterConfig
/**
* 获取JedisCluster的配置
*/
@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");
}
}
JedisCluster和springboot整合的更多相关文章
- springboot整合jedisCluster
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可以快速的启动服务或构建应 ...
随机推荐
- you_are_the_one(区间dp)
You Are the One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- ubuntu 安装 CUDA、 cuDNN 的tips
CUDA 查看驱动兼容性:https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html 查看GCC 与CUDA 驱动版本的兼容性 ...
- str 小列题
name = " aleX leNb "#2.有变量 完成如下操作: 移除 name 变量对应的值两边的空格,并输出处理结果 name=name.strip() print(nam ...
- yii报错yii\web\Request::cookieValidationKey must be configured with a secret key.
在config文件下main-local.php配置 'cookieValidationKey' => 'rabbit1234',
- matlab 中figure的图像 抗锯齿
linehandle = plot(xxxxxx); set( linehandle, 'linesmoothing', 'on' );
- [StructLayout(LayoutKind.Sequential) ] 是什么意思
首先介绍一下 结构体和类的区别 :类是按引用传递 结构体是按值传递进入正题:结构体是由若干成员组成的.布局有两种1.Sequential,顺序布局,比如struct S1{int a;int b;}那 ...
- php随机获取数组里面的值
srand() 函数播下随机数发生器种子,array_rand() 函数从数组中随机选出一个或多个元素,并返回.第二个参数用来确定要选出几个元素.如果选出的元素不止一个,则返回包含随机键名的数组,否则 ...
- zencart前台小语种后台英文 导入批量表 前后台不显示产品的问题
admin\includes\init_includes\init_languages.php 前台小语种后台英文导致批量表导入后,前后台不显示产品的问题将红色部分修改成前台语言对应的值,前台语言对应 ...
- CentOS7 配置阿里云yum源,非常之简单
1.进入yum的文件夹 命令:cd /etc/yum.repos.d/ 2.下载wget 命令:yum -y install wget 命令:yum install bash-completion ...