Spring Boot 整合Redis集群

我就是我 2021-10-19 02:36 440阅读 0赞

1. pom文件中引入Redis依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-redis</artifactId>
  4. </dependency>

2. 修改配置文件

application.yml

  1. spring:
  2. # Redis 缓存
  3. redis:
  4. cluster:
  5. nodes:
  6. - 10.7.12.23:7001
  7. - 10.7.12.23:7002
  8. - 10.7.12.23:7003
  9. database: 0
  10. jedis:
  11. pool:
  12. # 连接池最大连接数(使用负值表示没有限制)
  13. max-active: 130
  14. # 连接池中的最大空闲连接
  15. max-idle: 30
  16. # 连接池中的最小空闲连接
  17. min-idle: 10
  18. # 连接池最大阻塞等待时间(使用负值表示没有限制
  19. max-wait: 3s
  20. # 连接超时时间
  21. timeout: 6s

application.properties

  1. spring.redis.cluster.nodes=127.0.0.1:8881,127.0.0.1:8882,127.0.0.1:8883
  2. spring.redis.database=0
  3. spring.redis.jedis.pool.max-active=130
  4. spring.redis.jedis.pool.max-idle=30
  5. spring.redis.jedis.pool.min-idle=10
  6. spring.redis.jedis.pool.max-wait=3s
  7. spring.redis.timeout=6s

补充说明

使用lettuce连接池

  1. spring:
  2. # Redis 缓存
  3. redis:
  4. cluster:
  5. nodes:
  6. - 10.7.12.23:7001
  7. - 10.7.12.23:7002
  8. - 10.7.12.23:7003
  9. database: 0
  10. lettuce:
  11. pool:
  12. # 连接池最大连接数(使用负值表示没有限制)
  13. max-active: 130
  14. # 连接池中的最大空闲连接
  15. max-idle: 30
  16. # 连接池中的最小空闲连接
  17. min-idle: 10
  18. # 连接池最大阻塞等待时间(使用负值表示没有限制
  19. max-wait: 3s
  20. # 连接超时时间
  21. timeout: 6s

使用哨兵

  1. spring:
  2. # Redis 缓存
  3. redis:
  4. ##单机
  5. # host: 127.0.0.1
  6. # port: 6379
  7. ##
  8. ##集群
  9. cluster:
  10. nodes:
  11. - 127.0.0.1:7000
  12. - 127.0.0.1:7001
  13. - 127.0.0.1:7002
  14. ##哨兵
  15. sentinel:
  16. master: mymaster
  17. nodes:
  18. - 127.0.0.1:7003
  19. - 127.0.0.1:7004
  20. - 127.0.0.1:7005
  21. database: 0
  22. jedis:
  23. pool:
  24. # 连接池最大连接数(使用负值表示没有限制)
  25. max-active: 130
  26. # 连接池中的最大空闲连接
  27. max-idle: 30
  28. # 连接池中的最小空闲连接
  29. min-idle: 10
  30. # 连接池最大阻塞等待时间(使用负值表示没有限制
  31. max-wait: 3s
  32. # 连接超时时间
  33. timeout: 6s

发表评论

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

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

相关阅读