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

SpringBoot整合分布式鎖redisson的示例代碼

 更新時(shí)間:2023年02月23日 09:39:33   作者:梁山教父  
這篇文章主要介紹了SpringBoot整合分布式鎖redisson,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1、導(dǎo)入maven坐標(biāo)

<!-- 用redisson作為所有分布式鎖,分布式對(duì)象等功能框架-->
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <version>3.12.5</version>
    </dependency>

2、redisson配置類(lèi)(如果redis沒(méi)有密碼就不需要private String password)

package com.xuechengpluscommon.config;
 
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @description Redisson 配置類(lèi)
 */
@Configuration
public class MyRedissonConfig {
 
    @Value(value = "${spring.redis.host}")
    private String host;
    @Value(value = "${spring.redis.port}")
    private int port;
    @Value(value = "${spring.redis.database}")
    private int database;
//    @Value(value = "${spring.redis.password}")
//    private String password;
 
    /**
     * 單Redis節(jié)點(diǎn)模式配置方法
     * 其他配置參數(shù),看:
     * <a >
     * 單Redis節(jié)點(diǎn)模式配置方法
     * </a>
     *
     * @return {@link RedissonClient}
     */
    @Bean(destroyMethod = "shutdown")
    RedissonClient redisson() {
        Config config = new Config();
        //Redis多節(jié)點(diǎn)
        // config.useClusterServers()
        //     .addNodeAddress("redis://127.0.0.1:6379", "redis://127.0.0.1:7001");
        //Redis單節(jié)點(diǎn)
        SingleServerConfig singleServerConfig = config.useSingleServer();
        //可以用"rediss://"來(lái)啟用SSL連接
        String address = "redis://" + host + ":" + port;
        singleServerConfig.setAddress(address);
        //設(shè)置 數(shù)據(jù)庫(kù)編號(hào)
        singleServerConfig.setDatabase(database);
//        singleServerConfig.setPassword(password);
        //連接池大小:默認(rèn)值:64
        // singleServerConfig.setConnectionPoolSize()
        return Redisson.create(config);
    }
 
}

yml中redis的配置

redis:
    database: 0
    host: ip地址
    port: 6379

3、創(chuàng)建redisson的bean

@Autowired
private RedissonClient redisson;

4、測(cè)試,入隊(duì)

@Test
void contextLoads1() {
    RQueue<String> queue = redisson.getQueue("RedissonQueue");
    queue.add("hello");
    System.out.println(queue);
}

redis數(shù)據(jù)

5、測(cè)試,出隊(duì)

@Test
void contextLoads2() {
    RQueue<String> queue = redisson.getQueue("RedissonQueue");
    String remove = queue.remove();
    System.out.println(remove);
}

出隊(duì)數(shù)據(jù):

6、分布式鎖

public R redisTest1() throws InterruptedException {
        //首先從redis查詢(xún)數(shù)據(jù)
        Orders redisOrder;
        redisOrder=(Orders) redisTemplate.opsForValue().get("redisTest");
        //如果存在redis則返回
        if(ObjectUtils.isNotEmpty(redisOrder)){
            return R.ok(redisOrder);
            //如果不存在則從數(shù)據(jù)庫(kù)查詢(xún)
        }else {
            //首先獲取分布式鎖
            RLock lock = redissonClient.getLock("redisTestDogWatch");
            //上鎖
            lock.lock();
 
            //獲取鎖之后進(jìn)行查詢(xún)
            try {
                redisOrder = (Orders) redisTemplate.opsForValue().get("redisTest");
                if (ObjectUtils.isNotEmpty(redisOrder)) {
                    return R.ok(redisOrder);
                }
                //數(shù)據(jù)庫(kù)查詢(xún)
                redisOrder= ordersService.getById(1577177773194113014L);
                redisTemplate.opsForValue().set("redisTest", redisOrder );
                System.out.println("[從數(shù)據(jù)庫(kù)中查詢(xún)]");
 
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //刪除鎖
                lock.unlock();
            }
            return R.ok(redisOrder);
        }
    }

在這里讓線(xiàn)程睡眠35秒

//上鎖
lock.lock();
Thread.sleep(35000);

此時(shí)三個(gè)服務(wù)器運(yùn)行

測(cè)試結(jié)果

可以首先看到已經(jīng)上鎖的redisTestDogWatch,每個(gè)鎖30秒,如果線(xiàn)程未完成則會(huì)自動(dòng)續(xù)30秒,如果線(xiàn)程完成到finally中的unlock就刪除鎖

目前可以看到只有一個(gè)服務(wù)器獲取到鎖

存儲(chǔ)redis的數(shù)據(jù)

此時(shí)進(jìn)行并發(fā)測(cè)試

依舊保持35秒的睡眠時(shí)間

此時(shí)可以看到未拿到鎖的線(xiàn)程報(bào)錯(cuò)

java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: d904f2b9-e75d-472d-a5a6-d0a8cc603a80 thread-id: 210

嘗試解鎖鎖定,未被當(dāng)前線(xiàn)程鎖定 按節(jié)點(diǎn) ID:C731FAC4-9BF7-4F4A-ACC1-A2CC9B78A02F 線(xiàn)程 ID:232

這是因?yàn)殒i住了,所以無(wú)法獲取鎖,然后看門(mén)狗會(huì)不斷刷新

到此這篇關(guān)于SpringBoot整合分布式鎖redisson的文章就介紹到這了,更多相關(guān)SpringBoot整合分布式鎖redisson內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 代理機(jī)制的實(shí)例詳解

    java 代理機(jī)制的實(shí)例詳解

    這篇文章主要介紹了java 代理機(jī)制的實(shí)例詳解的相關(guān)資料,這里說(shuō)明下如何實(shí)現(xiàn)代理機(jī)制,幫助大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • java jdbc連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作

    java jdbc連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作

    這篇文章主要為大家詳細(xì)介紹了java jdbc連接mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Java中Runnable和Thread的區(qū)別分析

    Java中Runnable和Thread的區(qū)別分析

    在java中可有兩種方式實(shí)現(xiàn)多線(xiàn)程,一種是繼承Thread類(lèi),一種是實(shí)現(xiàn)Runnable接口,下面就拉分別介紹一下這兩種方法的優(yōu)缺點(diǎn)
    2013-03-03
  • springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法

    springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法

    這篇文章主要介紹了springboot 項(xiàng)目容器啟動(dòng)后如何自動(dòng)執(zhí)行指定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring框架接入單機(jī)Redis兩種實(shí)現(xiàn)方式解析

    Spring框架接入單機(jī)Redis兩種實(shí)現(xiàn)方式解析

    這篇文章主要介紹了Spring框架接入單機(jī)Redis兩種實(shí)現(xiàn)方式解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之開(kāi)發(fā)聊天室功能

    Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之開(kāi)發(fā)聊天室功能

    這篇文章主要為大家詳細(xì)介紹了如何利用Netty實(shí)現(xiàn)聊天室功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Netty網(wǎng)絡(luò)編程有一定幫助,需要的可以參考一下
    2022-10-10
  • SpringBoot整合mybatis的方法詳解

    SpringBoot整合mybatis的方法詳解

    這篇文章主要為大家詳細(xì)介紹了SpringBoot整合mybatis的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • 解決Spring Boot項(xiàng)目端口8080被占用的問(wèn)題

    解決Spring Boot項(xiàng)目端口8080被占用的問(wèn)題

    這篇文章主要介紹了解決Spring Boot項(xiàng)目端口8080被占用的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Spring高級(jí)之注解@PropertySource的原理

    Spring高級(jí)之注解@PropertySource的原理

    這篇文章主要介紹了Spring高級(jí)之注解@PropertySource的原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 關(guān)于String轉(zhuǎn)Json的幾種方式

    關(guān)于String轉(zhuǎn)Json的幾種方式

    這篇文章主要介紹了關(guān)于String轉(zhuǎn)Json的幾種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評(píng)論