項目結構:
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