redisson使用方式(springboot整合redisson)

项目结构:

 

 1.引入pom依赖:

<?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 http://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.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.springbootredisson</groupId>
  <artifactId>springboot-redissson</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springboot-redissson</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--springboot-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

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

    <!--整合redisson-->
    <dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson-spring-boot-starter</artifactId>
      <version>3.11.2</version>
      <exclusions>
        <exclusion>
          <groupId>org.redisson</groupId>
          <artifactId>redisson-spring-data</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <!--切面-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.4</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.2</version>
    </dependency>

    <!--lombok-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2.application配置

application.yml

server:
  port: 8012

spring:
  profiles:
    active: "local"
    #active: "dev"
    #active: "prod"
#    active: "test"

application-local.yml:

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 123
    timeout: 1000ms
    jedis:
      pool:
        max-idle: 8
        max-wait: -1ms
        min-idle: 0
        max-active: 8

3.启动类:Application

package com.springbootredisson;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableScheduling
@EnableAsync
@EnableAspectJAutoProxy(proxyTargetClass=true, exposeProxy=true)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        System.out.println("--启动成功--");
    }
}

4.测试类:ResissonTestController

package com.springbootredisson;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2022/11/30.
 */
@RestController
@RequestMapping("/redisson")
@Slf4j
public class ResissonTestController {
    @Autowired
    private RedissonClient redissonClient;
    @Value("${server.port}")
    private String port;

    @PostMapping("/test")
    public void addEquipmentAccount(){
        RLock rLock = redissonClient.getLock("test");
        String threadIdAndName = Thread.currentThread().getId() + "===" +Thread.currentThread().getName();
        log.info(threadIdAndName + "开始获取锁");
        // lock加锁代码写在try catch外面,防止加锁异常再走解锁引发解锁异常。
        rLock.lock();
        // 加锁成功后,每10秒续命一次,查看redisClient客户端可以看到key为test的过期时间TTL,每到剩余20秒的时候变成30秒。
        log.info(threadIdAndName + "加锁成功");
        System.out.println("端口号:" + port);
        try {
            if("8011".equals(port)){
                log.info(threadIdAndName + "睡眠15s");
                Thread.sleep(15000);
                // 模拟端口8011异常。
                int i = 1/0;
            }else{
                log.info(threadIdAndName + "睡眠30s");
                Thread.sleep(30000);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            /**
             * 1.程序异常,锁会一直续命,出现死锁。需要在finally中手动释放锁。
             * 2.宕机,模拟宕机停掉服务。锁会在默认30秒后过期自动释放。不会出现死锁。
             * 结论:异常需要在finally中手动释放锁。宕机不会引发死锁。
             */
            // 手动释放锁
            if(null != rLock && rLock.isLocked() && rLock.isHeldByCurrentThread()){
                rLock.unlock();
                log.info(threadIdAndName + "手动释放锁成功");
            }
        }
    }
}

怎么一直阻塞直到获取锁?

参考:https://blog.csdn.net/weixin_48921808/article/details/119918734

redisson是不是所有锁都会用看门狗机制?哪些加锁方法会用看门狗机制?

参考:https://blog.csdn.net/m0_45364328/article/details/125175796

 

posted @ 2022-11-30 15:12  super超人  阅读(1014)  评论(0编辑  收藏  举报