「这是我参与11月更文挑战的第9天,活动详情查看:2021最后一次更文挑战」
Springboot整合
Springboot操作数据:spring-data jpa jdbc
SpringData也是和SpringBoot齐名的
说明:在springboot2.x之后,原来使用的jedis被替换为了lettuce
jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全,使用jedis poll连接池 更像BIO模式
lettuce:采用netty,实例可以再多个线程中进行共享,不存在线程不安全的情况!可以减少线程数据了,更像NIO模式
Springboot所有的配置类,都有一个自动配置类
RedisAutoConfiguration
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
RedisConfig
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
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);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
序列化
public class Student implements Serializable {}
用jdk序列化会变成前面有转义字符的,默认使用的是jdk序列化
一般不使用原生的api操作
RedisUtils
Redis.conf详解
启动的时候,通过配置文件启动
快照
持久化,在规定时间内,执行了多少次操作,则会持久化到文件.rdb .aof
redis是内存数据库,如果没有持久化,那么数据断电即失
protected-mode no #保护模式
port 6379
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
#日志级别
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
logfile "" #日志文件名
databases 16 #一共有16个数据库
SNAPSHOTTING(快照)
#如果900s内,如果至少一个key进行了修改,我们进行持久化操作
save 900 1
save 300 10
save 60 10000
#
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
SECURITY(安全)
requirepass foobared #redis设置密码
CLIENTS(客户端)
maxclients 10000
APPEND ONLY MODE(append模式)
appendonly no
# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"
# appendfsync always
appendfsync everysec
# appendfsync no
stop-writes-on-bg
rdbcompresssion yes #是否压缩rdb文件,需要消耗一些cpu资源
rdbchecksum yes #保存rdb文件的时候是否进行校验
dir ./ #rdb文件保存目录
REp #主从复制
config set 密码
auth 123456 使用密码进行登录
限制CLIENT
maxclients 10000 #设置连接最大客户端
maxmemory-polity noeviction#内存达到满的处理策略 #移除一些key
APPEND ONLY 模式 aof配置
appendonly no #默认不开启aof模式的,默认是使用rdb方式持久化的,在大部分情况下rdb是够用的
appendfilename "appendonly.aof" #持久化文件的名字
appendfsync everysec #每秒执行一次sync,可能会丢失1s的数据