JedisCluster连接redis集群(有密码)

蔚落 2022-05-29 03:39 431阅读 0赞

redis集群是通过redis-trib.rb方式构建,在连接之前需要导入Jedis包
1.配置文件

  1. ###############################redis数据库的相关配置##################################
  2. ##访问密码
  3. redis.auth = yangfuren
  4. ##控制一个pool最多可以有多少个状态为Idle(空)的jedis实例默认值为8
  5. redis.maxIdle = 200
  6. ##可用的最大连接实例数 默认为8
  7. redis.maxActive = 1024
  8. ##等待可用连接的最大时间单位为毫秒 默认为-1表示永不超时,一旦超过等待时间则直接抛出
  9. redis.maxWait = 10000
  10. redis.timeOut = 10000
  11. ##设置为true则会在borrow一个jedis实例时,提前做validate操作
  12. redis.testOnBorrow =true
  13. ##连接最小空闲时间(毫秒)
  14. redis.minEvictableIdleTimeMillis=1800000
  15. ##释放连接的扫描间隔(毫秒)
  16. redis.timeBetweenEvictionRunsMillis=30000
  17. ##每次释放连接的最大数目
  18. redis.numTestsPerEvictionRun=1024
  19. ##最大连接数
  20. redis.maxTotal=30
  21. ##在空闲时检查有效性, 默认false
  22. redis.testWhileIdle=true
  23. ##连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
  24. redis.blockWhenExhausted=false
  25. redis.sockettimeout=1000
  26. redis.maxAttempts=1000
  27. #集群
  28. cluster.host1=ip
  29. cluster.port1=6380
  30. cluster.port2=6381
  31. cluster.port3=6382
  32. cluster.port4=6383
  33. cluster.port5=6384
  34. cluster.port6=6385

spring的application.xml中配置

  1. <!-- redis连接池配置 -->
  2. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  3. <!-- 最大连接数 -->
  4. <property name="maxTotal" value="${redis.maxTotal}"/>
  5. <!-- 最大空闲连接数 -->
  6. <property name="maxIdle" value="${redis.maxIdle}"/>
  7. <!-- 每次释放连接的最大数目 -->
  8. <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"/>
  9. <!-- 释放连接的扫描间隔(毫秒) -->
  10. <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/>
  11. <!-- 连接最小空闲时间 -->
  12. <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/>
  13. <!-- 连接空闲多久后释放, 当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
  14. <property name="softMinEvictableIdleTimeMillis" value="10000"/>
  15. <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
  16. <property name="maxWaitMillis" value="${redis.maxWait}"/>
  17. <!-- 在获取连接的时候检查有效性, 默认false -->
  18. <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  19. <!-- 在空闲时检查有效性, 默认false -->
  20. <property name="testWhileIdle" value="${redis.testWhileIdle}"/>
  21. <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
  22. <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
  23. </bean>
  24. <!-- redis单机通过连接池 <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close"> <constructor-arg index="0" ref="jedisPoolConfig"/> <constructor-arg index="1" value="${redis.addr}"/> <constructor-arg index="2" value="${redis.port}" type="int"/> <constructor-arg index="3" value="${redis.timeOut}" type="int"/> <constructor-arg index="4" value="${redis.auth}"/> </bean> -->
  25. <!-- redis集群 -->
  26. <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
  27. <constructor-arg index="0">
  28. <set>
  29. <bean class="redis.clients.jedis.HostAndPort">
  30. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  31. <constructor-arg index="1" value="${cluster.port1}"></constructor-arg>
  32. </bean>
  33. <bean class="redis.clients.jedis.HostAndPort">
  34. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  35. <constructor-arg index="1" value="${cluster.port2}"></constructor-arg>
  36. </bean>
  37. <bean class="redis.clients.jedis.HostAndPort">
  38. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  39. <constructor-arg index="1" value="${cluster.port3}"></constructor-arg>
  40. </bean>
  41. <bean class="redis.clients.jedis.HostAndPort">
  42. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  43. <constructor-arg index="1" value="${cluster.port4}"></constructor-arg>
  44. </bean>
  45. <bean class="redis.clients.jedis.HostAndPort">
  46. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  47. <constructor-arg index="1" value="${cluster.port5}"></constructor-arg>
  48. </bean>
  49. <bean class="redis.clients.jedis.HostAndPort">
  50. <constructor-arg index="0" value="${cluster.host1}"></constructor-arg>
  51. <constructor-arg index="1" value="${cluster.port6}"></constructor-arg>
  52. </bean>
  53. </set>
  54. </constructor-arg>
  55. <constructor-arg index="1" value="${redis.timeOut}"></constructor-arg>
  56. <constructor-arg index="2" value="${redis.sockettimeout}"></constructor-arg>
  57. <constructor-arg index="3" value="${redis.maxAttempts}"></constructor-arg>
  58. <constructor-arg index="4" value="${redis.auth}"></constructor-arg>
  59. <constructor-arg index="5" ref="jedisPoolConfig"></constructor-arg>
  60. </bean>

调用时候通过
@Resource
private SimpleRedisCluster simpleRedisCluster;
注入后调用即可

发表评论

表情:
评论列表 (有 0 条评论,431人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis工具类JedisCluster

    此项目的集群指的是 六台服务器,三主三从的集群,并非是哨兵机制; 关于哨兵机制的稍后补充。鉴于实际开发中这种的集群模式真的用的很少,很多都是哨兵模式开发,此处也不详述