1. 各種配置文檔
2.代碼開(kāi)始
-
導(dǎo)包
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);
}
}
- 測(cè)試類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: 測(cè)試類
* @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庫(kù)存key
private static String product_count_key = "product-count";
@Autowired
private RedisUtils redisUtils;
@Autowired
private RedissonClient redisson;
/**
* 初始化redis庫(kù)
* @return
*/
@GetMapping("/init")
public void init(){
redisUtils.set(product_count_key,100);
}
/**
* 不加鎖
* @return
*/
@GetMapping("/test")
public void lock(){
// 業(yè)務(wù)代碼
Integer productCount = Integer.parseInt(redisUtils.get(product_count_key).toString());
if (productCount <= 0) {
System.out.println("庫(kù)存不足");
}
productCount = productCount - 1;
System.out.println("成功購(gòu)買(mǎi),庫(kù)存:" + productCount);
redisUtils.set(product_count_key, String.valueOf(productCount));
}
/**
* 加鎖測(cè)試
* @return
*/
@GetMapping("test3")
public String test3(){
// 獲取鎖
RLock redissonLock = redisson.getLock(lockKey);
redissonLock.lock(30, TimeUnit.SECONDS);
try {
// 業(yè)務(wù)代碼
Integer productCount = Integer.parseInt(redisUtils.get(product_count_key).toString());
if (productCount <= 0) {
System.out.println("庫(kù)存不足");
return "fail";
}
productCount = productCount - 1;
System.out.println("成功購(gòu)買(mǎi),庫(kù)存:" + productCount);
redisUtils.set(product_count_key, String.valueOf(productCount));
} finally {
// 釋放鎖
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
// redissonLock.unlock();
}
return "success";
}
}
- jmeter測(cè)試結(jié)果
瀏覽器訪問(wèn)初始化redis庫(kù)存:
http://localhost:8088/init
jmeter訪問(wèn)測(cè)試加鎖和不加鎖:
http://localhost:8088/test3
http://localhost:8088/test
加鎖結(jié)果:
99
.
.
.
成功購(gòu)買(mǎi),庫(kù)存:19
成功購(gòu)買(mǎi),庫(kù)存:18
成功購(gòu)買(mǎi),庫(kù)存:17
成功購(gòu)買(mǎi),庫(kù)存:16
成功購(gòu)買(mǎi),庫(kù)存:15
成功購(gòu)買(mǎi),庫(kù)存:14
成功購(gòu)買(mǎi),庫(kù)存:13
成功購(gòu)買(mǎi),庫(kù)存:12
成功購(gòu)買(mǎi),庫(kù)存:11
成功購(gòu)買(mǎi),庫(kù)存:10
成功購(gòu)買(mǎi),庫(kù)存:9
成功購(gòu)買(mǎi),庫(kù)存:8
成功購(gòu)買(mǎi),庫(kù)存:7
成功購(gòu)買(mǎi),庫(kù)存:6
成功購(gòu)買(mǎi),庫(kù)存:5
成功購(gòu)買(mǎi),庫(kù)存:4
成功購(gòu)買(mǎi),庫(kù)存:3
成功購(gòu)買(mǎi),庫(kù)存:2
成功購(gòu)買(mǎi),庫(kù)存:1
成功購(gòu)買(mǎi),庫(kù)存:0
不加鎖結(jié)果:
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
成功購(gòu)買(mǎi),庫(kù)存:99
.
.
.