Git Product home page Git Product logo

redis-lock's Introduction

redis 分布式锁的实现

最近面试过程中,被问到了如何使用redis实现分布式锁,开始是在百度搜索了一些博客,了解到一个setNx(key, value)的方法, 然后结合get(key)、getSet(key, value)方法就可以实现分布式锁。但是后来再次被问到具体如何实现时,才发现可能还是有点问题的, 所以还是实践一下比较好,框架就用springboot,比较方便

搭建redis环境

  1. 可以直接下载windows版本解压、运行redis-server.exe即可,参考下载地址https://github.com/microsoftarchive/redis/releases/tag/win-3.2.100
  2. 去redis官网(https://redis.io/)下载,参照示例解压、编译即可

本次使用方式1,直接在Windows平台上搭建redis环境

搭建项目框架,引入redis的依赖,配置redis

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

# redis基础连接配置
spring.redis.host=localhost
spring.redis.port=6379

测试redis连接是否正常:在springboot测试类中添加以下代码,简单测试

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    public void contextLoads() {
    }

    @Test
    public void testRedis(){
        // 写入一个值
        stringRedisTemplate.opsForValue().set("test", "test data");
        // 测试获取值
        String test = stringRedisTemplate.opsForValue().get("test");
        System.out.println(test);
    }

分布式锁的实现

redis-lock's People

Contributors

hl9088 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.