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");
}
}

最新文章

  1. BZOJ 3676: [Apio2014]回文串
  2. Mac OSX中的@executable_path, @load_path和@rpath的理解
  3. 前端Javascript书籍分享
  4. 学android:直接用jdk来helloworld
  5. c#新语法学习笔记
  6. nginx.conf文件说明
  7. 05_最长公共子序列问题(LCS)
  8. dedecms手机站要同步pc站的图片
  9. PHP包含文件函数include、include_once、require、require_once区别总结
  10. JQuery multiselect的相关使用
  11. mac升级yosemite后安装gd的freetype扩展
  12. POJ 3253 Fence Repair (贪心)
  13. Zookeeper基本知识
  14. windows 编程 —— 消息与参数(滚动条、键盘、鼠标)
  15. Android Studio 2.1.x 关联SDK API Source
  16. 使用VNC+SSH建立安全的远程桌面访问WINDOWS服务器
  17. Raft论文学习笔记
  18. hi3531的hifb显示1080p60Hz
  19. 数据库学习番外篇 神奇的Redis
  20. gitbook 入门教程之使用 gitbook-cli 开发电子书

热门文章

  1. CentOS7之ssh-Xshell密钥认证登陆
  2. java使用face++简单实现人脸识别注册登录
  3. 【6.12校内test】T3 城市交通费
  4. 洛谷 P5043 树的同构 题解
  5. PythonDay15
  6. Jade学习(四)之结合node如何编译执行
  7. 在CentOS7上无人值守安装Zabbix4.2
  8. Source Insight 中调用Notepad++
  9. centos在无外网情况下,进行yum挂载
  10. Spring Framework Part3 IoC and Dynamic Proxy