当前位置:   article > 正文

redisson在spring boot中的入门_redisson 与springboot2.1.1版本关系

redisson 与springboot2.1.1版本关系

最近在项目中需要使用redis,经过考虑之后决定选用 redisson作为客户端.

项目中使用的是spring boot版本是2.1.1.RELEASE 版本,引入jar包.

由于最新版本的redisson starter使用的spring data是22的,而2.1.1版本支持redisson-spring-data-21,所以这边进行了一个排除与引入操作.

  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson-spring-boot-starter</artifactId>
  4. <version>3.13.1</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.redisson</groupId>
  8. <artifactId>redisson-spring-data-22</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.redisson</groupId>
  14. <artifactId>redisson-spring-data-21</artifactId>
  15. <version>3.13.1</version>
  16. </dependency>

对应的配置文件中引入

  1. # Spring配置
  2. spring:
  3. redis:
  4. redisson:
  5. config: classpath:redisson.yaml

 

redsson.yaml文件,需要放入resources文件中.

  1. # 单节点配置
  2. singleServerConfig:
  3. # 连接空闲超时,单位:毫秒
  4. idleConnectionTimeout: 10000
  5. # 连接超时,单位:毫秒
  6. connectTimeout: 10000
  7. # 命令等待超时,单位:毫秒
  8. timeout: 3000
  9. # 命令失败重试次数,如果尝试达到 retryAttempts(命令失败重试次数) 仍然不能将命令发送至某个指定的节点时,将抛出错误。
  10. # 如果尝试在此限制之内发送成功,则开始启用 timeout(命令等待超时) 计时。
  11. retryAttempts: 3
  12. # 命令重试发送时间间隔,单位:毫秒
  13. retryInterval: 1500
  14. # # 重新连接时间间隔,单位:毫秒
  15. # reconnectionTimeout: 3000
  16. # # 执行失败最大次数
  17. # failedAttempts: 3
  18. # 密码
  19. password:
  20. # 单个连接最大订阅数量
  21. subscriptionsPerConnection: 5
  22. # 客户端名称
  23. clientName: null
  24. # # 节点地址
  25. address: redis://10.0.252.153:6379
  26. # 发布和订阅连接的最小空闲连接数
  27. subscriptionConnectionMinimumIdleSize: 1
  28. # 发布和订阅连接池大小
  29. subscriptionConnectionPoolSize: 50
  30. # 最小空闲连接数
  31. connectionMinimumIdleSize: 32
  32. # 连接池大小
  33. connectionPoolSize: 64
  34. # 数据库编号
  35. database: 0
  36. # DNS监测时间间隔,单位:毫秒
  37. dnsMonitoringInterval: 5000
  38. # 线程池数量,默认值: 当前处理核数量 * 2
  39. threads: 0
  40. # Netty线程池数量,默认值: 当前处理核数量 * 2
  41. nettyThreads: 0
  42. # 编码
  43. codec: !<org.redisson.codec.JsonJacksonCodec> {}
  44. # 传输模式
  45. transportMode : "NIO"

 

这样就可以进行测试了.

测试代码如下

  1. import java.net.InetAddress;
  2. import java.net.NetworkInterface;
  3. import java.net.UnknownHostException;
  4. import java.util.Enumeration;
  5. import java.util.concurrent.TimeUnit;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.redisson.api.RBucket;
  10. import org.redisson.api.RMapCache;
  11. import org.redisson.api.RedissonClient;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.boot.test.context.SpringBootTest;
  14. import org.springframework.test.context.junit4.SpringRunner;
  15. /**
  16. * redission 测试类 *
  17. *
  18. * @author wyzhang
  19. * @date 2020/9/16 17:01
  20. */
  21. @RunWith(SpringRunner.class)
  22. @SpringBootTest(classes = {ClientApplication.class})
  23. @Slf4j
  24. public class ClientTest {
  25. @Autowired private RedissonClient redissonClient;
  26. @Autowired private CacheProperties cacheProperties;
  27. @Test
  28. public void redissionPutTest() {
  29. RBucket<String> bucket = redissonClient.getBucket("test");
  30. bucket.set("123");
  31. boolean isUpdated = bucket.compareAndSet("123", "4934");
  32. String prevObject = bucket.getAndSet("321");
  33. boolean isSet = bucket.trySet("901");
  34. long objectSize = bucket.size();
  35. // set with expiration
  36. bucket.set("value", 10, TimeUnit.SECONDS);
  37. boolean isNewSet = bucket.trySet("nextValue", 10, TimeUnit.SECONDS);
  38. log.info("size {}", objectSize);
  39. }
  40. }

 

 

集群配置

 

 

  1. clusterServersConfig:
  2. idleConnectionTimeout: 10000
  3. connectTimeout: 10000
  4. timeout: 3000
  5. retryAttempts: 3
  6. retryInterval: 1500
  7. password: null
  8. subscriptionsPerConnection: 5
  9. clientName: nicsp-client
  10. loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  11. # slaveSubscriptionConnectionMinimumIdleSize: 1
  12. # slaveSubscriptionConnectionPoolSize: 50
  13. slaveConnectionMinimumIdleSize: 32
  14. slaveConnectionPoolSize: 64
  15. masterConnectionMinimumIdleSize: 32
  16. masterConnectionPoolSize: 64
  17. readMode: "MASTER_SLAVE"
  18. nodeAddresses:
  19. - redis://10.0.9.120:7000
  20. - redis://10.0.9.121:7000
  21. - redis://10.0.9.122:7000
  22. scanInterval: 1000
  23. threads: 0
  24. nettyThreads: 0
  25. #codec: !<org.redisson.client.codec.StringCodec> {}
  26. codec: !<org.redisson.codec.JsonJacksonCodec> {}
  27. transportMode: "NIO" #传输模式nio

 

配置说明

 

 

  1. natMapper = {DefaultNatMapper@8024}
  2. nodeAddresses = {ArrayList@8025} size = 3 # 节点配置 Redis cluster node urls list
  3. scanInterval = 1000 # Redis cluster scan interval in milliseconds redis集群扫描间隔
  4. checkSlotsCoverage = true #
  5. loadBalancer = {RoundRobinLoadBalancer@8026} 负载算法
  6. slaveConnectionMinimumIdleSize = 24 # slave node 最小连接数 Redis 'slave' node minimum idle connection amount for <b>each</b> slave node
  7. slaveConnectionPoolSize = 64 # Redis 'slave' node maximum connection pool size for <b>each</b> slave node
  8. failedSlaveReconnectionInterval = 3000 # 失败重连时间
  9. failedSlaveCheckInterval = 180000 # 失败检查间隔
  10. masterConnectionMinimumIdleSize = 32 # Redis 'master' node minimum idle connection amount for <b>each</b> slave node
  11. masterConnectionPoolSize = 64 # Redis 'master' node maximum connection pool size
  12. readMode = {ReadMode@8027} "MASTER_SLAVE" # 读模式
  13. subscriptionMode = {SubscriptionMode@8028} "MASTER" # 具体是啥的订阅模式? 订阅模式
  14. subscriptionConnectionMinimumIdleSize = 1
  15. #Redis 'slave' node minimum idle subscription (pub/sub) connection amount for <b>each</b> slave node
  16. subscriptionConnectionPoolSize = 50
  17. dnsMonitoringInterval = 5000
  18. /**
  19. * If pooled connection not used for a <code>timeout</code> time
  20. * and current connections amount bigger than minimum idle connections pool size,
  21. * then it will closed and removed from pool.
  22. * Value in milliseconds.
  23. *
  24. */
  25. idleConnectionTimeout = 10000
  26. /**
  27. * Timeout during connecting to any Redis server.
  28. * Value in milliseconds.
  29. *
  30. */
  31. connectTimeout = 10000
  32. /**
  33. * Redis server response timeout. Starts to countdown when Redis command was succesfully sent.
  34. * Value in milliseconds.
  35. *
  36. */
  37. timeout = 3000
  38. retryAttempts = 3
  39. retryInterval = 1500
  40. /**
  41. * Password for Redis authentication. Should be null if not needed
  42. */
  43. password = null
  44. username = null
  45. /**
  46. * Subscriptions per Redis connection limit
  47. */
  48. subscriptionsPerConnection = 5
  49. /**
  50. * Name of client connection
  51. */
  52. clientName = null
  53. sslEnableEndpointIdentification = true
  54. sslProvider = {SslProvider@8029} "JDK"
  55. sslTruststore = null
  56. sslTruststorePassword = null
  57. sslKeystore = null
  58. sslKeystorePassword = null
  59. pingConnectionInterval = 0
  60. keepAlive = false
  61. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/815208
推荐阅读
相关标签
  

闽ICP备14008679号