最近在项目中需要使用redis,经过考虑之后决定选用 redisson作为客户端.
项目中使用的是spring boot版本是2.1.1.RELEASE 版本,引入jar包.
由于最新版本的redisson starter使用的spring data是22的,而2.1.1版本支持redisson-spring-data-21,所以这边进行了一个排除与引入操作.
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.13.1</version>
<exclusions>
<exclusion>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-22</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-21</artifactId>
<version>3.13.1</version>
</dependency>
对应的配置文件中引入
# Spring配置
spring:
redis:
redisson:
config: classpath:redisson.yaml
redsson.yaml文件,需要放入resources文件中.
# 单节点配置
singleServerConfig:
# 连接空闲超时,单位:毫秒
idleConnectionTimeout: 10000
# 连接超时,单位:毫秒
connectTimeout: 10000
# 命令等待超时,单位:毫秒
timeout: 3000
# 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
# 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
retryAttempts: 3
# 命令重试发送时间间隔,单位:毫秒
retryInterval: 1500
# # 重新连接时间间隔,单位:毫秒
# reconnectionTimeout: 3000
# # 执行失败最大次数
# failedAttempts: 3
# 密码
password:
# 单个连接最大订阅数量
subscriptionsPerConnection: 5
# 客户端名称
clientName: null
# # 节点地址
address: redis://10.0.252.153:6379
# 发布和订阅连接的最小空闲连接数
subscriptionConnectionMinimumIdleSize: 1
# 发布和订阅连接池大小
subscriptionConnectionPoolSize: 50
# 最小空闲连接数
connectionMinimumIdleSize: 32
# 连接池大小
connectionPoolSize: 64
# 数据库编号
database: 0
# DNS监测时间间隔,单位:毫秒
dnsMonitoringInterval: 5000
# 线程池数量,默认值: 当前处理核数量 * 2
threads: 0
# Netty线程池数量,默认值: 当前处理核数量 * 2
nettyThreads: 0
# 编码
codec: !<org.redisson.codec.JsonJacksonCodec> {}
# 传输模式
transportMode : "NIO"
这样就可以进行测试了.
测试代码如下
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RBucket;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* redission 测试类 *
*
* @author wyzhang
* @date 2020/9/16 17:01
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ClientApplication.class})
@Slf4j
public class ClientTest {
@Autowired private RedissonClient redissonClient;
@Autowired private CacheProperties cacheProperties;
@Test
public void redissionPutTest() {
RBucket<String> bucket = redissonClient.getBucket("test");
bucket.set("123");
boolean isUpdated = bucket.compareAndSet("123", "4934");
String prevObject = bucket.getAndSet("321");
boolean isSet = bucket.trySet("901");
long objectSize = bucket.size();
// set with expiration
bucket.set("value", 10, TimeUnit.SECONDS);
boolean isNewSet = bucket.trySet("nextValue", 10, TimeUnit.SECONDS);
log.info("size {}", objectSize);
}
}
集群配置
clusterServersConfig:
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
password: null
subscriptionsPerConnection: 5
clientName: nicsp-client
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
# slaveSubscriptionConnectionMinimumIdleSize: 1
# slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: "MASTER_SLAVE"
nodeAddresses:
- redis://10.0.9.120:7000
- redis://10.0.9.121:7000
- redis://10.0.9.122:7000
scanInterval: 1000
threads: 0
nettyThreads: 0
#codec: !<org.redisson.client.codec.StringCodec> {}
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO" #传输模式nio
配置说明
natMapper = {DefaultNatMapper@8024}
nodeAddresses = {ArrayList@8025} size = 3 # 节点配置 Redis cluster node urls list
scanInterval = 1000 # Redis cluster scan interval in milliseconds redis集群扫描间隔
checkSlotsCoverage = true #
loadBalancer = {RoundRobinLoadBalancer@8026} 负载算法
slaveConnectionMinimumIdleSize = 24 # slave node 最小连接数 Redis 'slave' node minimum idle connection amount for <b>each</b> slave node
slaveConnectionPoolSize = 64 # Redis 'slave' node maximum connection pool size for <b>each</b> slave node
failedSlaveReconnectionInterval = 3000 # 失败重连时间
failedSlaveCheckInterval = 180000 # 失败检查间隔
masterConnectionMinimumIdleSize = 32 # Redis 'master' node minimum idle connection amount for <b>each</b> slave node
masterConnectionPoolSize = 64 # Redis 'master' node maximum connection pool size
readMode = {ReadMode@8027} "MASTER_SLAVE" # 读模式
subscriptionMode = {SubscriptionMode@8028} "MASTER" # 具体是啥的订阅模式? 订阅模式
subscriptionConnectionMinimumIdleSize = 1
#Redis 'slave' node minimum idle subscription (pub/sub) connection amount for <b>each</b> slave node
subscriptionConnectionPoolSize = 50
dnsMonitoringInterval = 5000
/**
* If pooled connection not used for a <code>timeout</code> time
* and current connections amount bigger than minimum idle connections pool size,
* then it will closed and removed from pool.
* Value in milliseconds.
*
*/
idleConnectionTimeout = 10000
/**
* Timeout during connecting to any Redis server.
* Value in milliseconds.
*
*/
connectTimeout = 10000
/**
* Redis server response timeout. Starts to countdown when Redis command was succesfully sent.
* Value in milliseconds.
*
*/
timeout = 3000
retryAttempts = 3
retryInterval = 1500
/**
* Password for Redis authentication. Should be null if not needed
*/
password = null
username = null
/**
* Subscriptions per Redis connection limit
*/
subscriptionsPerConnection = 5
/**
* Name of client connection
*/
clientName = null
sslEnableEndpointIdentification = true
sslProvider = {SslProvider@8029} "JDK"
sslTruststore = null
sslTruststorePassword = null
sslKeystore = null
sslKeystorePassword = null
pingConnectionInterval = 0
keepAlive = false
tcpNoDelay = false
查看数据结构映射
http://www.voidcc.com/redisson/redisson-integration-with-hibernate
参考:https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter#spring-boot-starter
https://blog.csdn.net/kq1983/article/details/105215738
https://github.com/redisson/redisson/wiki/2.-Configuration 官方wiki