zoukankan      html  css  js  c++  java
  • redisson 分布式加锁

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    @RequestMapping("/m1")
    public int insertUserT3() {

    String lockKey="lockKey";
    boolean ok= stringRedisTemplate.opsForValue().setIfAbsent("lockKey","m1");
    try
    {
    if (!ok)
    {
    return -1;
    }
    num=num+1;
    userServiceTest02.insertUser("m1",num );
    }finally {
    stringRedisTemplate.delete(lockKey);
    }

    return 1;
    }

     

    redisson 解决方案

    Pom 的解决文件
    <dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.11.1</version>
    </dependency>

    @Configuration
    public class RedisConfig {

    @Bean("redission")
    public Redisson redissonClient() {
    Config config = new Config();
    // 单机模式
    config.useSingleServer()
    .setAddress("redis://127.0.0.1:6379").setDatabase(0);
    return(Redisson)Redisson.create(config);
    }
    }

    @Autowired
    Redisson redisson;
    @RequestMapping("/mm")
    public int insertUserT2() {


    String strKey="stockKey";
    RLock Redissionlock= redisson.getLock(strKey);
    try
    {
    Redissionlock.lock();
    num=num+1;
    userServiceTest02.insertUser("zhangsan",num );

    }
    finally {

    Redissionlock.unlock();
    }

    return 1;
    }

    ------------------------------------------------------------------------------------------------------------------------------------------------

    检查数据是否有重复

    DELETE FROM users;
    SELECT * FROM users WHERE age=2;

    SELECT * FROM users GROUP BY age HAVING COUNT(age)>1;

  • 相关阅读:
    xp下双开3389源码
    批处理加密解密原理
    Durango框架开源
    细说PHP5.3.4变量的引用赋值
    svnversion
    sqlite 的使用
    message日志_默认是一周一个日志,保存4周
    Smartmontool 使用
    NETRA之数据库处理
    mysql 日志转
  • 原文地址:https://www.cnblogs.com/bruce1992/p/13779732.html
Copyright © 2011-2022 走看看