redisredis Redisson 基本用法
Posted 九师兄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redisredis Redisson 基本用法相关的知识,希望对你有一定的参考价值。
1.概述
转载:https://www.cnblogs.com/cjsblog/p/11273205.html 建议看原文
1. Redisson
Redisson是Redis官方推荐的Java版的Redis客户端。它提供的功能非常多,也非常强大,此处我们只用它的分布式锁功能。
https://github.com/redisson/redisson
1.1. 基本用法
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.11.1</version>
</dependency>
1.2. Distributed locks and synchronizers
RedissonClient中提供了好多种锁,还有其它很多实用的方法
默认,非公平锁
最简洁的一种方法
指定超时时间
异步
1.2.2 Fair Lock
1.2.3 MultiLock
1.2.4 RedLock
1.3. 示例
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.1.6.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.cjs.example</groupId>
12 <artifactId>cjs-redisson-example</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>cjs-redisson-example</name>
15
16 <properties>
17 <java.version>1.8</java.version>
18 </properties>
19
20 <dependencies>
21 <dependency>
22 <groupId>org.springframework.boot</groupId>
23 <artifactId>spring-boot-starter-data-jpa</artifactId>
24 </dependency>
25 <dependency>
26 <groupId>org.springframework.boot</groupId>
27 <artifactId>spring-boot-starter-data-redis</artifactId>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-web</artifactId>
32 </dependency>
33
34 <!-- https://github.com/redisson/redisson#quick-start -->
35 <dependency>
36 <groupId>org.redisson</groupId>
37 <artifactId>redisson</artifactId>
38 <version>3.11.1</version>
39 </dependency>
40
41
42 <dependency>
43 <groupId>org.apache.commons</groupId>
44 <artifactId>commons-lang3</artifactId>
45 <version>3.9</version>
46 </dependency>
47 <dependency>
48 <groupId>com.alibaba</groupId>
49 <artifactId>fastjson</artifactId>
50 <version>1.2.58</version>
51 </dependency>
52 <dependency>
53 <groupId>org.apache.commons</groupId>
54 <artifactId>commons-pool2</artifactId>
55 <version>2.6.2</version>
56 </dependency>
57
58 <dependency>
59 <groupId>mysql</groupId>
60 <artifactId>mysql-connector-java</artifactId>
61 <scope>runtime</scope>
62 </dependency>
63 <dependency>
64 <groupId>org.projectlombok</groupId>
65 <artifactId>lombok</artifactId>
66 <optional>true</optional>
67 </dependency>
68 </dependencies>
69
70 <build>
71 <plugins>
72 <plugin>
73 <groupId>org.springframework.boot</groupId>
74 <artifactId>spring-boot-maven-plugin</artifactId>
75 </plugin>
76 </plugins>
77 </build>
78
79 </project>
application.yml
1 server:
2 port: 8080
3 spring:
4 application:
5 name: cjs-redisson-example
6 redis:
7 cluster:
8 nodes: 10.0.29.30:6379, 10.0.29.95:6379, 10.0.29.205:6379
9 lettuce:
10 pool:
11 min-idle: 0
12 max-idle: 8
13 max-active: 20
14 datasource:
15 url: jdbc:mysql://127.0.0.1:3306/test
16 username: root
17 password: 123456
18 driver-class-name: com.mysql.cj.jdbc.Driver
19 type: com.zaxxer.hikari.HikariDataSource
RedissonConfig.java
1 package com.cjs.example.lock.config;
2
3 import org.redisson.Redisson;
4 import org.redisson.api.RedissonClient;
5 import org.redisson.config.Config;
6 import org.springframework.context.annotation.Bean;
7 import org.springframework.context.annotation.Configuration;
8
9 /**
10 * @author ChengJianSheng
11 * @date 2019-07-26
12 */
13 @Configuration
14 public class RedissonConfig {
15
16 @Bean
17 public RedissonClient redissonClient() {
18 Config config = new Config();
19 config.useClusterServers()
20 .setScanInterval(2000)
21 .addNodeAddress("redis://10.0.29.30:6379", "redis://10.0.29.95:6379")
22 .addNodeAddress("redis://10.0.29.205:6379");
23
24 RedissonClient redisson = Redisson.create(config);
25
26 return redisson;
27 }
28
29 }
CourseServiceImpl.java
1 package com.cjs.example.lock.service.impl;
2
3 import com.alibaba.fastjson.JSON;
4 import com.cjs.example.lock.constant.RedisKeyPrefixConstant;
5 import com.cjs.example.lock.model.CourseModel;
6 import com.cjs.example.lock.model.CourseRecordModel;
7 import com.cjs.example.lock.repository.CourseRecordRepository;
8 import com.cjs.example.lock.repository.CourseRepository;
9 import com.cjs.example.lock.service.CourseService;
10 import lombok.extern.slf4j.Slf4j;
11 import org.apache.commons.lang3.StringUtils;
12 import org.redisson.api.RLock;
13 import org.redisson.api.RedissonClient;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.data.redis.core.HashOperations;
16 import org.springframework.data.redis.core.StringRedisTemplate;
17 import org.springframework.stereotype.Service;
18
19 import java.util.concurrent.TimeUnit;
20
21 /**
22 * @author ChengJianSheng
23 * @date 2019-07-26
24 */
25 @Slf4j
26 @Service
27 public class CourseServiceImpl implements CourseService {
28
29 @Autowired
30 private CourseRepository courseRepository;
31 @Autowired
32 private CourseRecordRepository courseRecordRepository;
33 @Autowired
34 private StringRedisTemplate stringRedisTemplate;
35 @Autowired
36 private RedissonClient redissonClient;
37
38 @Override
39 public CourseModel getById(Integer courseId) {
40
41 CourseModel courseModel = null;
42
43 HashOperations<String, String, String> hashOperations = stringRedisTemplate.opsForHash();
44
45 String value = hashOperations.get(RedisKeyPrefixConstant.COURSE, String.valueOf(courseId));
46
47 if (StringUtils.isBlank(value)) {
48 String lockKey = RedisKeyPrefixConstant.LOCK_COURSE + courseId;
49 RLock lock = redissonClient.getLock(lockKey);
50 try {
51 boolean res = lock.tryLock(10, TimeUnit.SECONDS);
52 if (res) {
53 value = hashOperations.get(RedisKeyPrefixConstant.COURSE, String.valueOf(courseId));
54 if (StringUtils.isBlank(value)) {
55 log.info("从数据库中读取");
56 courseModel = courseRepository.findById(courseId).orElse(null);
57 hashOperations.put(RedisKeyPrefixConstant.COURSE, String.valueOf(courseId), JSON.toJSONString(courseModel));
58 }
59 }
60 } catch (InterruptedException e) {
61 e.printStackTrace();
62 } finally {
63 lock.unlock();
64 }
65 } else {
66 log.info("从缓存中读取");
67 courseModel = JSON.parseObject(value, CourseModel.class);
68 }
69
70 return courseModel;
71 }
72
73 @Override
74 public void upload(Integer userId, Integer courseId, Integer studyProcess) {
75
76 HashOperations<String, String, String> hashOperations = stringRedisTemplate.opsForHash();
77
78 String cacheKey = RedisKeyPrefixConstant.COURSE_PROGRESS + ":" + userId;
79 String cacheValue = hashOperations.get(cacheKey, String.valueOf(courseId));
80 if (StringUtils.isNotBlank(cacheValue) && studyProcess <= Integer.valueOf(cacheValue)) {
81 return;
82 }
83
84 String lockKey = "upload:" + userId + ":" + courseId;
85
86 RLock lock = redissonClient.getLock(lockKey);
87
88 try {
89 lock.lock(10, TimeUnit.SECONDS);
90
91 cacheValue = hashOperations.get(cacheKey, String.valueOf(courseId));
92 if (StringUtils.isBlank(cacheValue) || studyProcess > Integer.valueOf(cacheValue)) {
93 CourseRecordModel model = new CourseRecordModel();
94 model.setUserId(userId);
95 model.setCourseId(courseId);
96 model.setStudyProcess(studyProcess);
97 courseRecordRepository.save(model);
98 hashOperations.put(cacheKey, String.valueOf(courseId), String.valueOf(studyProcess));
99 }
100
101 } catch (Exception ex) {
102 log.error("获取所超时!", ex);
103 } finally {
104 lock.unlock();
105 }
106
107 }
108 }
StockServiceImpl.java
1 package com.cjs.example.lock.service.impl;
2
3 import com.cjs.example.lock.constant.RedisKeyPrefixConstant;
4 import com.cjs.example.以上是关于redisredis Redisson 基本用法的主要内容,如果未能解决你的问题,请参考以下文章