SpringBoot实现自定义Redis的连接

kyang MVP++

在这里插入图片描述

1.docker安装Redis

1
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf

2.maven 的pom文件导包

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

3.自定义属性

application.yml文件中加入你自己想要的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
redis:
config:
host: 192.168.200.142 #填你redis安装的电脑的ip
port: 6379
password: 你的密码
pool-size: 10
min-idle-size: 5
idle-timeout: 30000
connect-timeout: 5000
retry-attempts: 3
retry-interval: 1000
ping-interval: 60000
keep-alive: true

4.读取配置

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
@Data
@ConfigurationProperties(prefix = "redis.config", ignoreInvalidFields = true)
public class RedisClientConfigProperties {

private String host;

private int port;

private String password;

private int poolSize = 64;

private int minIdleSize = 10;

private int idleTimeout = 10000;

private int connectTimeout = 10000;

private int retryAttempts = 3;

private int retryInterval = 1000;

private int pingInterval = 0;

private boolean keepAlive = true;

}
  • 各个参数的含义往下看

ignoreInvalidFields字段含义

当这个属性设置为true时,Spring会忽略配置文件中那些与配置类字段不匹配的属性。这意味着,如果配置文件中存在与配置类字段不存在的属性,Spring不会抛出异常,而是忽略这些属性。

注入容器

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
@Configuration
@EnableConfigurationProperties(RedisClientConfigProperties.class)
public class RedisClientConfig {

@Bean("redissonClient")
public RedissonClient redissonClient(ConfigurableApplicationContext applicationContext, RedisClientConfigProperties properties) {
Config config = new Config();

//设置编码器
config.setCodec(new JsonJacksonCodec());

config.useSingleServer()
.setAddress("redis://" + properties.getHost() + ":" + properties.getPort())
.setPassword(properties.getPassword())
.setConnectionPoolSize(properties.getPoolSize())
.setConnectionMinimumIdleSize(properties.getMinIdleSize())
.setIdleConnectionTimeout(properties.getIdleTimeout())
.setConnectTimeout(properties.getConnectTimeout())
.setRetryAttempts(properties.getRetryAttempts())
.setRetryInterval(properties.getRetryInterval())
.setPingConnectionInterval(properties.getPingInterval())
.setKeepAlive(properties.isKeepAlive())
;

return Redisson.create(config);
}

各个参数含义

  • setAddress:连接的地址和端口
  • setPassword:密码
  • setConnectionPoolSize:设置连接池的大小
  • setConnectionMinimumIdleSize:设置连接池的最小空闲连接数
  • setIdleConnectionTimeout:设置连接的最大空闲时间(单位:毫秒),超过该时间的空闲连接将被关闭
  • setConnectTimeout:设置连接超时时间(单位:毫秒)
  • setRetryAttempts:设置连接重试次数
  • setRetryInterval:设置连接重试的间隔时间(单位:毫秒)
  • setPingConnectionInterval:设置定期检查连接是否可用的时间间隔(单位:毫秒)
  • setKeepAlive:设置是否保持长连接
  • 标题: SpringBoot实现自定义Redis的连接
  • 作者: kyang
  • 创建于 : 2024-09-22 17:49:20
  • 更新于 : 2025-07-11 16:56:12
  • 链接: https://blog.kyang.top/2024/09/22/SpringBoot实现自定义Redis的连接/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论