勇敢的打工人 2021-03-17 09:39 采纳率: 42.9%
浏览 298

redisson实现分布式锁报错

/*
 * 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]
  • 写回答

2条回答 默认 最新

  • 关注

    看报错日志,像是操作的数据类型不对导致的

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗