微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用redis实现分布式锁

一、使用StringRedistemplate实现分布式锁

package com.example.baidu.redis;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Redistemplate;
import org.springframework.data.redis.core.StringRedistemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {

    @Autowired
    private Redistemplate<String, String> redistemplate;

    @Autowired
    private StringRedistemplate stringRedistemplate;

    @GetMapping("/redis")
    public void sendMessage(@RequestParam String message) {
        String clientId = UUID.randomUUID().toString();
        Boolean result = stringRedistemplate.opsForValue().setIfAbsent(message, clientId, 10, TimeUnit.SECONDS);
        if (!result) {
            System.out.println("获取锁失败!");
        } else {
            try {
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                ;
                e.printstacktrace();
            } finally {
                if (clientId.equals(stringRedistemplate.opsForValue().get(message))) {
                    stringRedistemplate.delete(message);
                }
            }
        }


    }

}

 

二、使用Redisson实现分布式锁

package com.example.baidu.redis;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;


@Controller
public class RedissonController {


    @GetMapping("/redisson")
    @ResponseBody
    public int redisson() {
        testThreadLock();
        return 0;
    }


    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private Redisson redisson;


    public void lock(String key, String num) {

        //获取锁
        RLock lock = redisson.getLock(key);
        boolean locked = false;
        try {
            //设置锁
            locked = lock.tryLock(10, TimeUnit.SECONDS);
            //locked = lock.lock();
            if (locked) {
                //开始写业务
                System.out.println(num + "锁住了。。。");
                System.out.println(num + "模拟业务耗时开始。。");
                Thread.sleep(10);
                System.out.println(num + "模拟业务耗时结束。。。");
            } else {
                System.out.println(num + "没锁住。。。");
            }
        } catch (Exception e) {
            e.printstacktrace();
        } finally {
            if (locked) {
                System.out.println(num + "释放锁");
                System.out.println();
                lock.unlock();
            }
        }
    }


    public void testThreadLock() {
        String key = "threadLock";
        for (int i = 1; i < 100; i++) {
            new Thread() {
                @Override
                public void run() {
                    lock("test", this.getName());
                }
            }.start();
        }
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐