spring-boot配置redis cluster

只需简单两步,便可以配置好redis cluster连接,然后方便地使用RedisTemplate来存取数据:

1、引入依赖:

		
			org.springframework.boot
			spring-boot-starter-data-redis
		

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

3、测试:

	@Autowired
	RedisTemplate redisTemplate;
	
	@Test
	public void redisTest() {
		String key = "redisTestKey";
		String value = "I am test value";
		
		ValueOperations 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));
	}
使用RedisTemplate,可以方便的存取redis中的内容。


你可能感兴趣的:(缓存,项目配置,Spring,Boot)