欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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")//指定從本地自寫的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("已售完,下次再來吧");
        //業(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ù)類型String常用操作命令

    Redis基本數(shù)據(jù)類型String常用操作命令

    這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型String常用操作命令,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Redis TTL命令實(shí)現(xiàn)數(shù)據(jù)生存時(shí)間

    Redis TTL命令實(shí)現(xiàn)數(shù)據(jù)生存時(shí)間

    生存時(shí)間可以通過Redis中的不同命令來設(shè)置、查看和管理,TTL命令是其中之一,本文主要介紹了Redis TTL命令實(shí)現(xiàn)數(shù)據(jù)生存時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • 深入理解Redis被覆寫后的失效時(shí)間

    深入理解Redis被覆寫后的失效時(shí)間

    Redis覆寫已存在的鍵會(huì)導(dǎo)致其舊的失效時(shí)間被新的鍵值對(duì)所取代,本文詳細(xì)解析了在鍵被覆寫時(shí),其失效時(shí)間的變化,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-09-09
  • 使用Ruby腳本部署Redis Cluster集群步驟講解

    使用Ruby腳本部署Redis Cluster集群步驟講解

    今天小編就為大家分享一篇關(guān)于使用Ruby腳本部署Redis Cluster集群步驟講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • K8s部署Redis主從集群教程

    K8s部署Redis主從集群教程

    本文介紹了在Kubernetes環(huán)境下搭建Redis集群的詳細(xì)步驟,包括環(huán)境準(zhǔn)備、安裝NFS、創(chuàng)建PV卷、搭建Redis集群、集群初始化、主從切換測(cè)試以及開放外網(wǎng)端口等內(nèi)容
    2025-01-01
  • Redis連接池配置及初始化實(shí)現(xiàn)

    Redis連接池配置及初始化實(shí)現(xiàn)

    這篇文章主要介紹了Redis連接池配置及初始化實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型哈希Hash常用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 利用Redis的有序集合實(shí)現(xiàn)排行榜功能實(shí)例代碼

    利用Redis的有序集合實(shí)現(xiàn)排行榜功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于如何利用Redis的有序集合實(shí)現(xiàn)排行榜功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 使用Grafana監(jiān)控Redis的操作方法

    使用Grafana監(jiān)控Redis的操作方法

    這篇文章主要介紹了使用Grafana監(jiān)控Redis,號(hào)稱下一代可視化監(jiān)控系統(tǒng),結(jié)合SpringBoot使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • Redis存儲(chǔ)斷點(diǎn)續(xù)傳文件狀態(tài)的最佳實(shí)踐

    Redis存儲(chǔ)斷點(diǎn)續(xù)傳文件狀態(tài)的最佳實(shí)踐

    在斷點(diǎn)續(xù)傳系統(tǒng)中,如何高效地存儲(chǔ)和更新文件上傳狀態(tài)是關(guān)鍵,得益于 Redis 高效的內(nèi)存操作和多種數(shù)據(jù)結(jié)構(gòu)的支持,它非常適合用于存儲(chǔ)上傳過程中的臨時(shí)狀態(tài)信息,下面,我們將探討如何利用 Redis 實(shí)現(xiàn)文件上傳狀態(tài)的存儲(chǔ),需要的朋友可以參考下
    2024-12-12

最新評(píng)論