springboot整合redis
1.springboot测试redis
springboot为操作redis准备了两个工具类StringRedisTemplate和RedisTemplate。StringRedisTemplate是RedisTemplate的子类。StringRedisTemplate它的泛型key和value都是String类型。 RedisTemplate它的key value的泛型是Object。
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置
spring.redis.host=192.168.31.24 虚拟机IP
spring.redis.port=6379 虚拟机端口
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-wait=20000
3.测试
package com.zb.springbootredis01;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class SpringbootRedis01ApplicationTests {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
//操作key
Set<String> keys = redisTemplate.keys("*");
System.out.println(keys);
redisTemplate.delete("k8");//删除
System.out.println(redisTemplate.getExpire("k3"));
redisTemplate.expire("k3", 60, TimeUnit.SECONDS);//设置过期时间
System.out.println(redisTemplate.getExpire("k3"));
}
@Test
public void testString(){
ValueOperations<String, String> forValue = redisTemplate.opsForValue();//操作字符串类型
forValue.set("k1","v1");
System.out.println(forValue.get("k1"));
Map<String,String> map=new HashMap<>();
map.put("k9","v9");
map.put("k10","v10");
forValue.multiSet(map);
List<String> keys=new ArrayList<>();
keys.add("k1");
keys.add("k2");
keys.add("k4");
List<String> strings = forValue.multiGet(keys);
System.out.println(strings);
System.out.println(forValue.setIfAbsent("k2", "v2"));
}
}
2.springboot连接redis
使用redisTemplate该类可以存放任意类型的数据,但是该类型的数据必须实现序列,获取redis中对应的数据时,会进行反序列化。 如果使用RedisTemplate建议大家指定key,value,以及hashkey的序列化方式。
package com.zb.config;
/**
* @Author 周波
* @Date 2021/4/15 16:16
* @Version 1.0
*/
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.time.Duration;
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
//比如验证码
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(600)) //缓存过期10分钟 ---- 业务需求。
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//设置key的序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //设置value的序列化
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
3.springboot连接redis集群
在springboot配置文件中配置
1.连接哨兵
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.31.24:26379 //虚拟机的IP
2.去中心化集群
spring.redis.cluster.nodes=192.168.31.24:8001,192.168.31.24:8002,192.168.31.24192.168.31.24:8003,192.168.31.24:8004,192.168.31.24:8009,192.168.31.24:8006