1. 各种配置文档
https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#24-%E9%9B%86%E7%BE%A4%E6%A8%A1%E5%BC%8F
2.代码开始
-
导包
redisson-spring-boot-starter 包包含了 redis 依赖和 redisson 依赖
<!--redisson-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.10.6</version>
</dependency>
- 配置文件application.yml
server:
port: 8088
spring:
redis:
host: 172.16.223.136
password: 123456
- redis配置类
package com.redisson.cluster.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
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.RedisSerializationContext;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
/**
* @description:
* @author: Liangyb (yunbo.liang@ucarinc.com)
* @create: 2020/04/19 17:25
*/
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private String database;
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
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);
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
//如果链接redis失败增加redission链接redis的配置
@Bean
RedissonClient redissonClient() throws IOException {
Config config = new Config();
config.useSingleServer().setAddress("redis://"+host+":"+port).setPassword(password).setDatabase(StringUtils.isBlank(database) 1 : Integer.valueOf(database));
return Redisson.create(config);
}
}
- 测试类controller
package com.redisson.cluster.controller;
import com.redisson.cluster.util.RedisUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
/**
* @description: 测试类
* @author: Liangyb (yunbo.liang@ucarinc.com)
* @create: 2020/09/10 17:47
*/
@RestController
@RequestMapping("")
public class RedisLockController {
//商品id分布式锁key
private static String lockKey = "testLockKey";
//商品redis库存key
private static String product_count_key = "product-count";
@Autowired
private RedisUtils redisUtils;
@Autowired
private RedissonClient redisson;
/**
* 初始化redis库
* @return
*/
@GetMapping("/init")
public void init(){
redisUtils.set(product_count_key,100);
}
/**
* 不加锁
* @return
*/
@GetMapping("/test")
public void lock(){
// 业务代码
Integer productCount = Integer.parseInt(redisUtils.get(product_count_key).toString());
if (productCount <= 0) {
System.out.println("库存不足");
}
productCount = productCount - 1;
System.out.println("成功购买,库存:" + productCount);
redisUtils.set(product_count_key, String.valueOf(productCount));
}
/**
* 加锁测试
* @return
*/
@GetMapping("test3")
public String test3(){
// 获取锁
RLock redissonLock = redisson.getLock(lockKey);
redissonLock.lock(30, TimeUnit.SECONDS);
try {
// 业务代码
Integer productCount = Integer.parseInt(redisUtils.get(product_count_key).toString());
if (productCount <= 0) {
System.out.println("库存不足");
return "fail";
}
productCount = productCount - 1;
System.out.println("成功购买,库存:" + productCount);
redisUtils.set(product_count_key, String.valueOf(productCount));
} finally {
// 释放锁
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
// redissonLock.unlock();
}
return "success";
}
}
- jmeter测试结果
浏览器访问初始化redis库存:
http://localhost:8088/init
jmeter访问测试加锁和不加锁:
http://localhost:8088/test3
http://localhost:8088/test
加锁结果:
99
.
.
.
成功购买,库存:19
成功购买,库存:18
成功购买,库存:17
成功购买,库存:16
成功购买,库存:15
成功购买,库存:14
成功购买,库存:13
成功购买,库存:12
成功购买,库存:11
成功购买,库存:10
成功购买,库存:9
成功购买,库存:8
成功购买,库存:7
成功购买,库存:6
成功购买,库存:5
成功购买,库存:4
成功购买,库存:3
成功购买,库存:2
成功购买,库存:1
成功购买,库存:0
不加锁结果:
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
成功购买,库存:99
.
.
.