环境:SpringBoot2.x
maven增加配置
<!-- redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot2.0的redis整合包多出lettuce连接池,需要增加commons-pool2包
1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce
spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.6.5</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.25.Final</version>
</dependency>
增加yml文件配置
redis:
database: 0
host: 127.0.0.1
port: 6379
password: '123456'
jedis:
pool:
#最大连接数据库连接数,设 0 为没有限制
max-active: 8
#最大等待连接中的数量,设 0 为没有限制
max-idle: 8
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
max-wait: -1ms
#最小等待连接中的数量,设 0 为没有限制
min-idle: 0
增加RedissonConfig
import org.redisson.Redisson;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
//添加redisson的bean
@Bean
public Redisson redisson() {
Config config = new Config();
//此示例是单机的,可以是主从、sentinel、集群等模式
SingleServerConfig singleServerConfig = config.useSingleServer()
.setAddress("redis://" + host + ":" + port);
singleServerConfig.setPassword(password);//设置密码
return (Redisson) Redisson.create(config);
}
}
模拟测试控制器
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("")
public class RedisLockController {
private static String product1Count = "product1Count";//商品1的数量key
private static String lockKey = "testLockKey";//分布式锁的key
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private Redisson redisson;
/**
* 初始化设置商品数量
*
* @return
*/
@RequestMapping("/setProductCount")
public String setValue() {
redisTemplate.opsForValue().set(product1Count, "100");
return "success";
}
/**
* 模拟秒杀抢购,并发多个请求过来,查看是否出现超卖
*
* @return
*/
@RequestMapping("/spike")
public String spike() {
String flag = "success";
RLock lock = redisson.getLock(lockKey);
try {
//lock.lockAsync(5 , TimeUnit.SECONDS);
//lock.lock(5, TimeUnit.SECONDS); //设置60秒自动释放锁 (默认是30秒自动过期)
Future<Boolean> res = lock.tryLockAsync(100, 5, TimeUnit.SECONDS);
boolean result = res.get();
System.out.println("result:" + result);
if (result) {
int stock = Integer.parseInt(redisTemplate.opsForValue().get(product1Count).toString());
if (stock > 0) {
redisTemplate.opsForValue().set(product1Count, (stock - 1) + "");
} else {
flag = "fail";
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock(); //释放锁
}
return flag;
}
}
模拟秒杀抢购场景,初始化100库存,用jmeter软件工具测试,设置2秒内启动 300线程,循环请求2次,总计600请求,最后查看库存是否为负数,证明分布式锁是否锁住了库存
来源:oschina
链接:https://my.oschina.net/u/4309098/blog/3414314