springboot整合jedisCluster
来源:互联网 发布:淘宝缴纳保证金 编辑:程序博客网 时间:2024/06/05 14:29
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
定义一个类命名问RedisProperties,在里面定义的字段与配置文件中相对应,即可取到配置,如下:
@Component@ConfigurationProperties(prefix = "spring.redis.cache")@Datapublic 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)@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"); }}
使用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)); }
阅读全文
0 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
- 原来java中也有类似goto语句的标签啊--java label标签(一)
- Hibernate核心类用法-使用Configuration装载配置
- 在一个app中启动另外一个app的service
- python建立二叉树并且可视化
- DecorView与window的创建
- springboot整合jedisCluster
- vue 实战 之 饿了吗 页头模板
- 设计模式之抽象工厂
- 通过PL/SQL创建序列和触发器实现表自增字段
- JavaScript面向对象编程
- 输入一个链表,反转链表后,输出链表的所有元素。
- list集合为空或为null的区别
- 无apk加壳无防护app是有多危险,看了几维安全专业检测你就知道了
- java-并发集合-并发队列 ConcurrentLinkedQueue 演示