首先,我们要搞明白的是,Redis为什么会出现,数据库底层只用Mysql,SqlServer等关系型数据库不好吗,何必多此一举。
在这个软件行业发展的初期的确是这样,试想一下,比如说21世纪之前的中国,有许多人温饱都有问题,哪里来的经济能力去支持为了方便而诞生出的软件行业呢。而在进入21世纪以后,人们的消费能力大幅度的提升了,当然也就能够支撑软件的发展了。
说这些有什么用呢,我希望所有学开发的人都有一颗关注生活的心,软件是什么,在我看来,软件也是具有灵魂的,它产生的目的就是和人类交互,扯远了,回来,正因为使用软件的人多了,试想,创建了一个购物网站,假设他存储数据只用MySQL这种关系型数据库实现,所有点击商品信息的人,都要通过MySQL来查询数据,五万,十万,通过索引查询还可以,但是当数据百万千万呢,就比如说双十一试想多么庞大的数据量,MySQL的性能很显然是不能支持的。我们知道数据从内存中查询是非常快的,Redis正式基于这种原因,以及内存查询快,被构建出来,是遵循BSD协议的高速缓冲数据库。
Redis的性能十分优异,一秒写入数据可达到8万次左右,读取数据更是可达到11万次左右,这样的性能支持他做各种高并发的软件产品。Redis有五种基本数据类型,string,list,set,zset,hash,还有三种图等数据类型常用的是string,存储数据采用K,V的形式。
初学Redis时,多练一些get,set存取数据,怎么开启事务(Redis是支持事务),加乐观锁,提交事务等操作。事实上,与Springboot等整合时,你会发现其方法名几乎一样.
Redis是将数据存储在内存中,那么你可能会想到是否每次重启Redis,其数据都会丢失,这就涉及到了Redis的持久化,他的持久化有两种方式RDB和AOF(快照和日志),一种是记录数据,一种是拷贝命令。在实际开发中一般是两种结合使用。
好了,说了怎么多,讲一下,Springboot如何整合Redis
1.相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.yaml配置
spring:
redis:
# 地址
host: 192.168.93.129
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password:
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
3.Redis简单使用(String类型的存取)
package com.lrl.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
private void test(){
redisTemplate.opsForValue().set("aaa","123456");
System.out.println(redisTemplate.opsForValue().get("aaa"));
}
}
4.自定义RedisTemplate
package com.lrl.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@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;
}
}
在这个大数据的时代,信息量庞大到难以想象,Redis支持高并发,适用于对于做一些秒杀类的软件产品。
在实际的开发中,显然只使用一个Redis接收显然不合适,这就涉及到了集群的知识,集群存在的目的就是为了大数据的处理,集群的搭建遵循读写分离,我们都知道数据库做的大量是读的操作,写的操作只占到了10%左右,所以可以在搭建集群的时候可以采用主从复制(主机只读,从机只写),另外可以采用哨兵模式,当主机宕机时,从机可换为主机。
对了,我比较喜欢一个B站博主——狂神,他的Redis讲的不错,大家可以参考
Redis就说到这里了,这里只是一个大体的框架,如果你们喜欢,可以三联支持一下,你们的支持就是我创作的动力。如果有问题可以私信我。