/*
* Redisson的配置类,提供RedissonClient实例
* */
@Configuration
public class RedissionConfiguration {
@Bean
public RedissonClient getRedissionClient(){
Config config=new Config();
//单例
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return Redisson.create(config);
}
}
@Repository
public class RedisServer {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Qualifier("getRedissionClient")//指定从本地自写的class中取得实例
@Autowired
private RedissonClient redissonClient;
public String redisLock(String goodID){
RLock rLock = redissonClient.getLock(goodID);
//加锁 5秒
rLock.lock(5, TimeUnit.SECONDS);
//业务代码 查询库存
int goodTotal = Integer.parseInt(redisTemplate.opsForValue().get(goodID));
if (goodTotal>0){
redisTemplate.opsForValue().set(goodID,String.valueOf(--goodTotal));
System.out.println("卖出一份,剩余库存:"+goodTotal);
}else{
System.out.println(" 售空 ");
}
//业务完成 释放锁
rLock.unlock();
return String.valueOf(goodTotal);
}
}
@RestController
public class UserController {
@Autowired
private RedisServer redisServer;
@RequestMapping("redisTest")
public String redisTest (){
String goodId = "goodShop";
for (int i=0;i<101;i++){
redisServer.redisLock(goodId);
}
return "执行完";
}
}
#redis配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
#0号数据库 0-15
spring.redis.database=1
以上全部代码,报错内容
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.redisson.client.RedisException: ERR Error running script (call to f_dad0f8d619e548e04f21fb48d50be5034df3adae): @user_script:1: WRONGTYPE Operation against a key holding the wrong kind of value . channel: [id: 0x05d91e30, L:/127.0.0.1:59137 - R:/127.0.0.1:6379] command: (EVAL), params: [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hincrby', KEYS[1], ARGV[2], 1); redis.call(..., 1, goodShop, 5000, df1f8625-9bfc-4583-8c6d-91f598fb34db:88]