Spring boot 整合redis集群
一、環(huán)境搭建
二、創(chuàng)建Spring boot項目
1.創(chuàng)建boot項目
2.創(chuàng)建配置文件
spring:
redis:
database: 0 # Redis數(shù)據(jù)庫索引(默認為0)
timeout: 100000 # 連接超時時間(毫秒)
# jedis:
# pool:
# max-active: 8 # 連接池最大連接數(shù)(使用負值表示沒有限制)
# max-idle: 8 # 連接池中的最大空閑連接
# max-wait: -1 # 連接池最大阻塞等待時間(使用負值表示沒有限制)
# min-idle: 0 # 連接池中的最小空閑連接
cluster:
nodes:
- 10.1.194.193:6379
- 10.1.194.193:6380
- 10.1.194.193:6381
- 10.1.194.193:6382
- 10.1.194.193:6383
- 10.1.194.193:6384
connectionTimeout: 6000
soTimeout: 6000
maxAttempts: 5
# password:
3.初始化RedisTemplate
package com.lagou.demo.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){
RedisTemplate<String,Object> template=new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//value采用jackson序列化方式
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的key采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//hash的value采用String的序列化方式
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
4.編寫測試代碼
我這邊使用了2種方式調(diào)用
package com.lagou.demo;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
@SpringBootTest
@RunWith(SpringRunner.class)
class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void get() {
redisTemplate.opsForValue().set("sex","nan");
System.out.println(redisTemplate.opsForValue().get("name"));
System.out.println("------------------------------------------------------");
JedisCluster jcd = null;
try {
JedisPoolConfig config = new JedisPoolConfig();
Set<HostAndPort> jedisClusterNode = new HashSet<>();
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6379));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6380));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6381));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6382));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6383));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6384));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6385));
jedisClusterNode.add(new HostAndPort("10.1.194.193", 6386));
jcd = new JedisCluster(jedisClusterNode, config);
//jcd.set("name", "zhang3");
String value = jcd.get("sex");
System.out.println("get val from redis cluster: " + value);
} finally {
if (jcd != null){
jcd.close();
}
}
}
}