springboot整合jedisCluster
来源:互联网 发布:台湾留学生在大陆知乎 编辑:程序博客网 时间:2024/06/05 15:44
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 clusterspring.redis.cache.clusterNodes=192.168.xx.xx:6379,192.168.xx.:6380,192.168.xx.xx:6381spring.redis.cache.commandTimeout=5000
相当简单只需要几个redis的地址和端口的字符串就可以了。
redisProperties
在这里取springboot中的配置办法相当多,可以使用如下方法:
@Inject private Environment environment; String properties = environment.getproperties("xxx")
或者是在加上注解,@Value(“”)会在配置文件中取相关名字的配置。
但在本文中决定使用另外一种方法,定义一个类命名问RedisProperties,在里面定义的字段与配置文件中相对应,即可取到配置,如下:
@Component@ConfigurationProperties(prefix = "spring.redis.cache")@Datapublic class RedisProperties { private String clusterNodes; private Integer commandTimeout;}
如上,在使用时就能正常取到相关配置。
JedisClusterConfig
/** * Created by xiaxuan on 16/11/8. * 获取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)@WebAppConfigurationpublic 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"); }}
结果:
set和del都正常,整合成功。
2 0
- springboot整合jedisCluster
- springboot整合jedisCluster
- Redis JedisCluster Spring整合
- redis - JedisCluster Spring整合
- jedisCluster
- jedisCluster整合spring 的配置说明
- Springboot Redis分布式集群(4)- JedisCluster连接测试集群
- SpringBoot学习:SpringBoot整合mybatis
- SpringBoot整合Mybatis(分层整合)
- springboot整合freemarker
- SpringBoot和Mybatis整合
- springboot整合jsp
- springboot 整合mybatis
- springboot整合swagger
- springboot+mybatis+dubbo整合
- springboot mybaits mysql 整合
- springboot整合dubbox
- SpringBoot整合MyBatis
- 分离元器件搭建BUCK分析
- HDU 1050 Moving Tables
- NS3
- linux学习
- NS-3中计算packet延迟(每一个包的延迟)
- springboot整合jedisCluster
- rsyslog及logrotate小结
- 在MyEclipse上安装windowBuilder插件
- NS-3中的OnOffApplicaion作为协议仿真的CBR源
- ns3-3
- 正则表达式
- ns3-4
- hdu A+B
- BZOJ1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 DP