SpringBoot--配置Redisson的方法
原创
©著作权归作者所有:来自51CTO博客作者IT利刃出鞘的原创作品,请联系作者获取转载授权,否则将追究法律责任
简介
说明
本文介绍如何在SpringBoot中配置Redisson。
官网网址
redisson-spring-boot-starter配置方案:redisson/redisson-spring-boot-starter at master · redisson/redisson · GitHub
spring-boot-starter-data-redis配置方案:Spring Boot Reference Documentation
配置详解:2. 配置方法 · redisson/redisson Wiki · GitHub
备注
以下方法的推荐度由高到低。
方案1:用配置文件
简介
对于配置文件方案,引入的依赖必须是redisson-spring-boot-starter;
法1:application.yml(通用配置)
直接使用spring-boot-starter-data-redis的配置。
spring:
redis:
host: 127.0.0.1
port: 6379
# password:
# database: 0 #指定数据库,默认为0
# timeout: 3000 #连接超时时间,单位毫秒,默认为0。也可以这么写:3s
# ssl: false # 是否启用SSL连接,默认false
# pool: #连接池配置
# max-active: 8 #最大活跃连接数,默认8个。
# max-idle: 8 #最大空闲连接数,默认8个。
# max-wait: -1 #获取连接的最大等待时间,默认-1,表示无限制,单位毫秒。
# #默认值可能会因为获取不到连接,导致事务无法提交,数据库被锁,大量线程处于等待状态的情况。
# min-idle: 0 #最小空闲连接数,默认0。
# sentinel:
# master: myMaster #哨兵master
# nodes: host1:port,host2:port #哨兵节点
# cluster:
# max-redirects: # 集群模式下,集群最大转发的数量
# nodes: host1:port,host2:port # 集群节点
法2:application.yml(redisson专用配置)
写法1:都写在application.yml
spring:
redis:
redisson:
config: |
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
failedSlaveReconnectionInterval: 3000
failedSlaveCheckInterval: 60000
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 24
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 24
masterConnectionPoolSize: 64
readMode: "SLAVE"
subscriptionMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7000"
scanInterval: 1000
pingConnectionInterval: 0
keepAlive: false
tcpNoDelay: false
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.MarshallingCodec> {}
transportMode: "NIO"
写法2:redisson配置单独拿出来
applicaion.yml
spring:
redis:
redisson:
config: classpath:redisson.yml
redisson.yml
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
slaveSubscriptionConnectionMinimumIdleSize: 1
slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7000"
scanInterval: 1000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
"transportMode":"NIO"
优先级问题
redisson配置文件优先级高于springboot配置文件优先级,比如下边这样写,则会取redisson中的配置:
spring:
redis:
host: 127.0.0.1
port: 6379
database: 0
# password: 12345
# ssl: false
# timeout: 3s
# cluster:
# nodes:
# sentinel:
# master:
# nodes:
redisson:
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
slaveSubscriptionConnectionMinimumIdleSize: 1
slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7004"
- "redis://127.0.0.1:7001"
- "redis://127.0.0.1:7000"
scanInterval: 1000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
"transportMode":"NIO"
方案2:用代码
简介
编程中有个思路:约定大于配置大于编程。能用配置则用配置,不推荐用代码去配置。
对于配置类配置方案,引入的依赖可以是redisson-spring-boot-starter也可以是redisson。
法1:编写配置类
需要在Resource目录下创建redisson.yml。
@Configuration
public class RedssonConfig {
@Bean(destroyMethod="shutdown")
public RedissonClient redisson() throws IOException {
RedissonClient redisson = Redisson.create(
Config.fromYAML(new ClassPathResource("redisson-single.yml").getInputStream()));
return redisson;
}
}
法2:用代码配置
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedissonConfig {
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec
//config.setCodec(new org.redisson.client.codec.StringCodec());
config.useSingleServer()
.setAddress("redis://47.108.105.159:6379")
.setPassword("cen66lyz")
.setConnectionPoolSize(50)
.setIdleConnectionTimeout(10000)
.setConnectTimeout(3000)
.setTimeout(3000)
.setDatabase(5);
return Redisson.create(config);
}
}