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

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

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

Redisson實(shí)現(xiàn)集群環(huán)境下的分布式鎖十分簡(jiǎ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://”前綴,否則將會(huì)報(bào)錯(cuò)。
        //此例集群為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個(gè)線程取得鎖,其余線程將自旋等待
* */

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


測(cè)試Controller

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

}

并發(fā)測(cè)試

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

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

控制臺(tái)結(jié)果輸出

redis緩存

總結(jié)

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

相關(guān)文章

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

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

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

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

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

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

    這篇文章主要介紹了Redis5之后版本的高可用集群搭建的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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ù)庫(kù)時(shí),我們通常會(huì)使用 RedisTemplate 來(lái)簡(jiǎn)化與 Redis 進(jìn)行交互的操作,而其中一個(gè)重要的配置項(xiàng)就是序列化設(shè)置,它決定了數(shù)據(jù)在存儲(chǔ)到 Redis 中時(shí)的格式,本文將介紹如何進(jìn)行 RedisTemplate 的序列化設(shè)置,以及一些常見(jiàn)的序列化方案
    2024-11-11
  • redis以目錄形式存儲(chǔ)和讀取數(shù)據(jù)實(shí)現(xiàn)方式

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

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

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

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

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

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

    Redis解決key沖突的問(wèn)題解決

    本文主要介紹了Redis解決key沖突的問(wèn)題解決,通過(guò)嚴(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)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06

最新評(píng)論

望谟县| 始兴县| 沿河| 敖汉旗| 都江堰市| 安陆市| 丹凤县| 龙口市| 陇川县| 湟中县| 额尔古纳市| 开原市| 永仁县| 佛坪县| 德钦县| 赤壁市| 新郑市| 哈尔滨市| 冕宁县| 大足县| 全南县| 将乐县| 砀山县| 遵化市| 钦州市| 望江县| 霍城县| 双桥区| 东台市| 天祝| 衡水市| 仁布县| 湖北省| 桃园县| 南京市| 水富县| 莱阳市| 开平市| 维西| 宁都县| 乌兰察布市|