最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis集群利用Redisson實(shí)現(xiàn)分布式鎖方式

 更新時間:2024年05月15日 16:46:51   作者:-luking-  
這篇文章主要介紹了Redis集群利用Redisson實(shí)現(xiàn)分布式鎖方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Redisson實(shí)現(xiàn)集群環(huán)境下的分布式鎖十分簡單:

引入依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lk</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        //Redis起步依賴
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
        //Redisson起步依賴
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.11.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.48</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Redisson配置類

package com.lk.demo.redissionconfig;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
* Redisson的配置類,提供RedissonClient實(shí)例
* */
@Configuration
public class RedissionConfiguration {

    @Bean
    public RedissonClient getRedissionClient(){
        Config config=new Config();
        //集群模式,集群節(jié)點(diǎn)的地址須使用“redis://”前綴,否則將會報錯。
        //此例集群為3節(jié)點(diǎn),各節(jié)點(diǎn)1主1從
        config.useClusterServers().addNodeAddress("redis://192.168.37.134:7001","redis://192.168.37.134:7002",
                "redis://192.168.37.134:7003","redis://192.168.37.134:7004","redis://192.168.37.134:7005","redis://192.168.37.134:7006");
        return Redisson.create(config);
    }
}

分布式鎖

package com.lk.demo.redissionconfig;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;


import java.util.concurrent.TimeUnit;

/*
* 利用RedissonClient部署及解除分布式鎖
* 直接調(diào)用lock、unlock方法,此方法只允許1個線程取得鎖,其余線程將自旋等待
* */

@Repository
public class HandlerLock{
    @Autowired
    private RedisTemplate<String,String> rt;
    //@Qualifier("redisson") //在org.redisson.spring.starter包中,可以從配置文件讀取redis配置,并返回redissonclient對象
    @Qualifier("getRedissionClient")//指定從本地自寫的class中取得實(shí)例
    @Autowired
    private RedissonClient redissonClient;
    //實(shí)驗(yàn)方法,測試分布式鎖
    public void doLock(String lockname){
        //從RedissonClient取得Rlock實(shí)例
        RLock rlock=redissonClient.getLock(lockname);
        //嘗試取鎖,有效期為3s,到期后自動釋放。如果取得鎖繼續(xù)執(zhí)行。取鎖失敗則自旋。
        //亦可使用rlock.tryLock()方法,此方法也為嘗試取鎖,并返回boolean結(jié)果
        rlock.lock(3,TimeUnit.SECONDS);
        //以下為測試業(yè)務(wù)代碼,
        System.out.println(Thread.currentThread().getName()+"取得鎖");
        int store=Integer.valueOf(rt.opsForValue().get("store"));
        if(store>0)
        {
            System.out.printf("售出1,當(dāng)前庫存為%d\n",--store);
            rt.opsForValue().set("store",String.valueOf(store));
        }else
            System.out.println("已售完,下次再來吧");
        //業(yè)務(wù)完成,釋放鎖
        rlock.unlock();
        System.out.println("解鎖");
    }
}


測試Controller

@Controller
public class Controllerdemo {
    @Autowired
    private HandlerLock handlerLock;
    @RequestMapping("/hello")
    @ResponseBody
    public void demo(){
        handlerLock.doLock("luking");
    }

}

并發(fā)測試

啟動SpringBoot,使用Jmeter進(jìn)行并發(fā)壓力測試,Redis中store字段現(xiàn)有值100。

500線程并發(fā),循環(huán)2次

控制臺結(jié)果輸出

redis緩存

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis特殊類型數(shù)據(jù)結(jié)構(gòu)Bitmap、HyperLogLog、GEO的使用及場景分析

    Redis特殊類型數(shù)據(jù)結(jié)構(gòu)Bitmap、HyperLogLog、GEO的使用及場景分析

    文章介紹了Redis的三種特殊數(shù)據(jù)類型:Bitmap、HyperLogLog和GEO,分別用于不同的場景,Bitmap適合存儲大量二進(jìn)制數(shù)據(jù),HyperLogLog在大數(shù)據(jù)場景下能高效統(tǒng)計(jì)基數(shù),而GEO則用于地理空間信息的管理和查詢,本文介紹的非常詳細(xì),感興趣的朋友跟隨小編一起通過本文學(xué)習(xí)吧
    2025-12-12
  • Redis的數(shù)據(jù)過期策略和數(shù)據(jù)淘汰策略

    Redis的數(shù)據(jù)過期策略和數(shù)據(jù)淘汰策略

    本文主要介紹了Redis的數(shù)據(jù)過期策略和數(shù)據(jù)淘汰策略,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Redis5之后版本的高可用集群搭建的實(shí)現(xiàn)

    Redis5之后版本的高可用集群搭建的實(shí)現(xiàn)

    這篇文章主要介紹了Redis5之后版本的高可用集群搭建的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Redis Cluster集群收縮主從節(jié)點(diǎn)詳細(xì)教程

    Redis Cluster集群收縮主從節(jié)點(diǎn)詳細(xì)教程

    集群收縮的源端就是要下線的主節(jié)點(diǎn),目標(biāo)端就是在線的主節(jié)點(diǎn),這篇文章主要介紹了Redis Cluster集群收縮主從節(jié)點(diǎn)詳細(xì)教程,需要的朋友可以參考下
    2021-11-11
  • RedisTemplate序列化設(shè)置的流程和具體步驟

    RedisTemplate序列化設(shè)置的流程和具體步驟

    在使用 Redis 作為緩存數(shù)據(jù)庫時,我們通常會使用 RedisTemplate 來簡化與 Redis 進(jìn)行交互的操作,而其中一個重要的配置項(xiàng)就是序列化設(shè)置,它決定了數(shù)據(jù)在存儲到 Redis 中時的格式,本文將介紹如何進(jìn)行 RedisTemplate 的序列化設(shè)置,以及一些常見的序列化方案
    2024-11-11
  • redis以目錄形式存儲和讀取數(shù)據(jù)實(shí)現(xiàn)方式

    redis以目錄形式存儲和讀取數(shù)據(jù)實(shí)現(xiàn)方式

    這段文章主要討論了在項(xiàng)目中使用Redis進(jìn)行數(shù)據(jù)寫入與讀取的方法,并強(qiáng)調(diào)了合理設(shè)置key的重要性,建議使用特定目錄形式,如“company:companyTree-all”來組織數(shù)據(jù)結(jié)構(gòu),以提高數(shù)據(jù)管理效率
    2026-05-05
  • Redis高級數(shù)據(jù)類型Hyperloglog、Bitmap的使用

    Redis高級數(shù)據(jù)類型Hyperloglog、Bitmap的使用

    很多小伙伴在面試中都會被問道 Redis的常用數(shù)據(jù)結(jié)構(gòu)有哪些?可能很大一部分回答都是 string、hash、list、set、zset,但其實(shí)還有Hyperloglog和Bitmap,本文就來介紹一下
    2021-05-05
  • 一篇文章帶你弄清楚Redis的精髓

    一篇文章帶你弄清楚Redis的精髓

    Redis是一個開源的、支持網(wǎng)絡(luò)、基于內(nèi)存的鍵值對存儲系統(tǒng),它可以用作數(shù)據(jù)庫、緩存和消息中間件。它支持多種數(shù)據(jù)類型,包括字符串、散列、列表、集合、位圖等,擁有極快的讀寫速度,并且支持豐富的特性,如事務(wù)、持久化、復(fù)制、腳本、發(fā)布/訂閱等。
    2023-02-02
  • Redis解決key沖突的問題解決

    Redis解決key沖突的問題解決

    本文主要介紹了Redis解決key沖突的問題解決,通過嚴(yán)格的key命名規(guī)范、RedisDB隔離、分布式并發(fā)控制和命名空間等手段,可以有效預(yù)防key沖突,感興趣的可以了解一下
    2025-11-11
  • Rocky9部署redis的實(shí)現(xiàn)示例

    Rocky9部署redis的實(shí)現(xiàn)示例

    本文主要介紹了Rocky9部署redis的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06

最新評論

沙坪坝区| 昌图县| 池州市| 通辽市| 尤溪县| 长汀县| 沭阳县| 望江县| 山东省| 北京市| 紫阳县| 上犹县| 微山县| 台东县| 泸定县| 安顺市| 东莞市| 长丰县| 台湾省| 神木县| 定西市| 霍州市| 故城县| 噶尔县| 长岛县| 平阴县| 台前县| 平遥县| 攀枝花市| 红安县| 吉木乃县| 瓮安县| 乌兰浩特市| 宝坻区| 巴里| 台南县| 岫岩| 沽源县| 邮箱| 琼结县| 探索|