spring boot3-redis分库及配置
Java的Maven类型,理论不想多说,直接用
1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、博主比较喜欢用application.yml,习惯用application.properties也可以
#库号分组
spring:
data:
redis:
singleNode:
host: localhost
port: 6379
databaseConfigs:
db0:
index: 0
db1:
index: 1
3、 创建RedisDatabaseConfig类,用来做配置
package com.atguigu.boot3_06_reids.config;//package 自己文件的命名空间路劲,也就是主程序相对了路劲
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
public class RedisDatabaseConfig {
private SingleNodeConfig singleNode;
private Map<String, DatabaseConfig> databaseConfigs = new HashMap<>();
@Data
public static class SingleNodeConfig {
private String host;
private int port;
}
@Data
public static class DatabaseConfig {
private int index;
}
}
4、创建RedisTemplateConfig类,注入容器@bean
package com.atguigu.boot3_06_reids.Bean;//package 自己文件的命名空间路劲,也就是主程序相对了路劲
import com.atguigu.boot3_06_reids.config.RedisDatabaseConfig;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
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
public class RedisTemplateConfig {
@Autowired
private RedisDatabaseConfig redisDatabaseConfig;
@Bean(name = "redisTemplateDb0")
public RedisTemplate<String, Object> redisTemplateDb0() {
return createRedisTemplate(redisDatabaseConfig.getDatabaseConfigs().get("db0").getIndex());
}
@Bean(name = "redisTemplateDb1")
public RedisTemplate<String, Object> redisTemplateDb1() {
return createRedisTemplate(redisDatabaseConfig.getDatabaseConfigs().get("db1").getIndex());
}
private RedisTemplate<String, Object> createRedisTemplate(int databaseIndex) {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(
redisDatabaseConfig.getSingleNode().getHost(),
redisDatabaseConfig.getSingleNode().getPort()
);
config.setDatabase(databaseIndex);
LettuceConnectionFactory factory = new LettuceConnectionFactory(config);
factory.afterPropertiesSet();
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 设置 key 的序列化器
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
// 配置 ObjectMapper
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 使用替代方法设置类型验证器
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
.allowIfSubType(Object.class)
.build();
om.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL);
// 在构造函数中传入 ObjectMapper
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(om, Object.class);
template.setValueSerializer(jackson2JsonRedisSerializer);
// 设置 Hash 的 key 和 value 的序列化器
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
5、调用
@RestController
public class RedisController {
@Autowired
@Qualifier("redisTemplateDb0") //库号0
private RedisTemplate<String, Object> redisTemplateDb0;
@Autowired
@Qualifier("redisTemplateDb1") //库号1
private RedisTemplate<String, Object> redisTemplateDb1;
@GetMapping("/redis")
public void redis(){
String uuid = UUID.randomUUID().toString();//生成随机唯一id
redisTemplateDb0.opsForValue().set("redis", uuid);
System.out.println("当前:"+redisTemplateDb0.opsForValue().get("redis"));//读值打印
}
}