击穿
Key的过期造成并发访问数据库,前置条件肯定发生了高并发
怎么解决?
如果key过期了,那么第一次访问范围null,这种情况第二次用setnx设置key,如果没有key才会设置成功,也就相当于一把锁,只有一个人能够拿到这把锁。拿到锁的人,才能够访问数据库,然后更新缓存;拿不到锁的人,随机sleep一个小的时间窗,然后再重新get key。这时候,缓存已经被更新了,直接从缓存取到数据返回,就没有击穿的情况了。
问题:
1、如果拿到锁的人挂了怎么办?
答:可以设置锁的过期时间
2、拿到锁的人没挂,但是锁超时了,怎么办?
答:可以用多线程,一个线程处理访问数据库,更新缓存等主流程;另一个线程监控主线程,如果没有执行完就更新锁超时时间
穿透
从业务接收查询的是你系统根本不存在的数据
雪崩
大量的key同时失效,间接造成大量的访问到达DB
分布式锁
1、setnx
2、过期时间
3、多线程(守护线程)延长过期时间
redisson
zookeeper 做分布式锁!
redis做分布式锁快,zookeeper做分布式锁,准确性更好,更容易。
api
https://redis.io/clients#java
-
jedis
https://github.com/redis/jedis
jedis是线程不安全的
-
lettuce
https://github.com/lettuce-io/lettuce-core
支持同步、异步 -
Spring Data Redis
https://spring.io/projects/spring-data-redis
-
spring Boot
https://spring.io/projects/spring-boot#learn
https://docs.spring.io/spring-boot/docs/2.1.8.RELEASE/reference/html/boot-features-nosql.html#boot-features-connecting-to-redis
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
<groupId>org.garen.study</groupId>
<artifactId>study-02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- other dependency elements omitted -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.2</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
RedisConfiguration.java
package org.garen.study.study02;
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.jedis.JedisConnectionFactory;
@Configuration
class RedisConfiguration {
@Bean
public JedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("192.168.174.61", 6379);
return new JedisConnectionFactory(config);
}
}
TestRedis.java
package org.garen.study.study02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Component
public class TestRedis {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
public void testRedis() {
redisTemplate.opsForValue().set("hello", "Hello China!");
System.out.println(redisTemplate.opsForValue().get("hello"));
stringRedisTemplate.opsForValue().set("hello2", "Hello GuangDong!");
System.out.println(stringRedisTemplate.opsForValue().get("hello2"));
}
}
Study02Application.java
package org.garen.study.study02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Study02Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Study02Application.class, args);
TestRedis redis = ctx.getBean(TestRedis.class);
redis.testRedis();
}
}
上一篇《10 Redis的集群:主从复制、CAP、PAXOS、cluster分片集群02》
下一篇 《11 Zookeeper介绍、安装、shell cli 使用,基本概念验证》