Redis分布式锁、内存淘汰策略

Redis分布式锁、内存淘汰策略

1.Redis分布式锁

常见的面试题:

  • Redis除了拿来做缓存,你还见过基于Redis的什么用法?
  • Redis做分布式锁的时候有需要注意的问题?
  • 如果是Redis是单点部署的,会带来什么问题?那你准备怎么解决单点问题呢?
  • 集群模式下,比如主从模式,有没有什么问题呢?
  • 那你简单的介绍一下Redlock吧?你简历上写redisson,你谈谈。
  • Redis分布式锁如何续期?看门狗知道吗?

boot整合redis搭建超卖程序

使用场景:多个服务间 + 保证同一时刻内 + 同一用户只能有一个请求(防止关键业务出现数据冲突和并发错误)

建两个Module:boot_redis01,boot_redis02

POM

<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.0modelVersion>

	<parent>
	    <groupId>org.springframework.bootgroupId>
	    <artifactId>spring-boot-starter-parentartifactId>
	    <version>2.3.3.RELEASEversion>
	    <relativePath/> 
	parent>

	<groupId>com.lungroupId>
	<artifactId>boot_redis01artifactId> 
	<version>1.0.0-SNAPSHOTversion>
	<packaging>jarpackaging>

	<name>boot_redis01name> 
	<url>http://maven.apache.orgurl>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
	properties>

	<dependencies>

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-actuatorartifactId>
		dependency>
		
		

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-data-redisartifactId>
		dependency>
		
		<dependency>
			<groupId>org.apache.commonsgroupId>
			<artifactId>commons-pool2artifactId>
		dependency>
		
		
		<dependency>
			<groupId>redis.clientsgroupId>
			<artifactId>jedisartifactId>
			<version>3.1.0version>
		dependency>

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-aopartifactId>
		dependency>
		
		
		<dependency>
			<groupId>org.redissongroupId>
			<artifactId>redissonartifactId>
			<version>3.13.4version>
		dependency>

		
		<dependency>
		    <groupId>org.springframework.bootgroupId>
		    <artifactId>spring-boot-devtoolsartifactId>
		    <scope>runtimescope>
		    <optional>trueoptional>
		dependency>
		
		<dependency>
		    <groupId>org.projectlombokgroupId>
		    <artifactId>lombokartifactId>
		    <optional>trueoptional>
		dependency>
		
		<dependency>
		    <groupId>org.springframework.bootgroupId>
		    <artifactId>spring-boot-starter-testartifactId><scope>testscope>
		    <exclusions>
		        <exclusion>
		            <groupId>org.junit.vintagegroupId>
		            <artifactId>junit-vintage-engineartifactId>
		        exclusion>
		    exclusions>
		dependency>

	dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>
server.port=1111
#2222

#=========================redis相关配香========================
#Redis数据库索引(默认方0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=192.168.111.147
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)默认8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)默认-1
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接默认8
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接默犬认0
spring.redis.lettuce.pool.min-idle=0

主启动类

@SpringBootApplication
public class BootRedis01Application{
   
    
    public static void main(String[] args){
   
        SpringApplication.run(BootRedis01Application.class, args);
    }
}

配置类

@Configuration
public class RedisConfig {
   

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory

你可能感兴趣的:(JAVA,redis,分布式,java)