抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:无。

环境

Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
Redis 7.4.8
Redisson 3.18.0

1 入门案例

1.1 创建项目

目录结构:

code
1
2
3
4
5
6
7
8
9
10
11
demo-redisson/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── application.yml
│ └── test/
└── pom.xml

1.2 管理依赖

创建pom.xml文件:

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.example</groupId>
<artifactId>demo-redisson</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.18.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

1.3 配置文件

创建application.yml文件:

application.yml
1
2
3
4
5
spring:
redis:
host: localhost
port: 6379
timeout: 2000ms

1.4 运行代码

在启动类中实现CommandLineRunner接口,在应用启动后测试:

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private RedissonClient redissonClient;

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... args) {
// 获取操作对象
RBucket<String> bucket = redissonClient.getBucket("user:name");

// 设置值
bucket.set("张三");

// 获取值
String name = bucket.get();
System.out.println("用户名:" + name);

// 删除值
bucket.delete();
}
}

2 核心组件

核心组件:

  • RedissonClient:Redisson的客户端入口,所有操作通过它发起。
  • RObject:所有分布式数据结构的基类,提供通用的操作方法。
  • RExpirable:支持设置过期时间的数据结构接口。
  • RBatch:批量操作接口,支持在一次网络请求中执行多个命令。
  • RTransaction:事务操作接口,支持Redis事务。
  • Codec:序列化编解码器,控制数据的序列化方式。

评论