码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot整合redis集群和redis单节点


    1. // 连接redis单节点配置类
    2. @Configuration
    3. public class RedisConfig {
    4. @Value("${spring.redis.host}")
    5. private String host;
    6. @Value("${spring.redis.port}")
    7. private Integer port;
    8. @Value("${spring.redis.password}")
    9. private String password;
    10. /**
    11. * @description: 非集群的Redis连接工厂
    12. * @return: org.springframework.data.redis.connection.RedisConnectionFactory
    13. * @author: hezha
    14. * @time: 2023/10/24 15:31
    15. */
    16. @Bean
    17. public RedisConnectionFactory redisSingleNodeConnectionFactory() {
    18. RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
    19. config.setHostName(host);
    20. config.setPort(port);
    21. config.setPassword(password);
    22. config.setDatabase(0);
    23. return new JedisConnectionFactory(config);
    24. }
    25. /**
    26. * @description: 连接非集群的Redis的RedisTemplate
    27. * @return: org.springframework.data.redis.core.RedisTemplate
    28. * @author: hezha
    29. * @time: 2023/10/24 15:31
    30. */
    31. @Bean
    32. public RedisTemplate redisSingleNodeTemplate(@Qualifier("redisSingleNodeConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
    33. RedisTemplate template = new RedisTemplate<>();
    34. template.setConnectionFactory(redisConnectionFactory);
    35. // 设置key的序列化方式
    36. RedisSerializer stringSerializer = new StringRedisSerializer();
    37. template.setKeySerializer(stringSerializer);
    38. template.setHashKeySerializer(stringSerializer);
    39. // 设置value的序列化方式
    40. Jackson2JsonRedisSerializer jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    41. template.setValueSerializer(jsonSerializer);
    42. template.setHashValueSerializer(jsonSerializer);
    43. return template;
    44. }
    45. }
      1. //连接redis集群的配置类
      2. @Configuration
      3. public class RedisClusterConfig {
      4. @Value("${spring.redis.cluster.nodes}")
      5. private String clusterNodes;
      6. @Value("${spring.redis.password}")
      7. private String redisAuth;
      8. @Bean
      9. @Primary
      10. public RedisConnectionFactory redisClusterConnectionFactory() {
      11. RedisClusterConfiguration redisClusterConfig = new RedisClusterConfiguration();
      12. String[] split = clusterNodes.split(",");
      13. for(int i = 0; i< split.length; i++){
      14. String[] strings = split[i].split(":");
      15. String host = strings[0];
      16. Integer post = Integer.parseInt(strings[1]);
      17. redisClusterConfig.clusterNode(host, post);
      18. }
      19. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfig);
      20. // 配置其他 JedisConnectionFactory 属性,如密码、超时等
      21. jedisConnectionFactory.setPassword(redisAuth);
      22. return jedisConnectionFactory;
      23. }
      24. @Bean
      25. public RedisTemplate redisClusterTemplate(RedisConnectionFactory redisClusterConnectionFactory) {
      26. RedisTemplate redisTemplate = new RedisTemplate<>();
      27. redisTemplate.setConnectionFactory(redisClusterConnectionFactory);
      28. // 设置key的序列化方式
      29. RedisSerializer stringSerializer = new StringRedisSerializer();
      30. redisTemplate.setKeySerializer(stringSerializer);
      31. redisTemplate.setHashKeySerializer(stringSerializer);
      32. // 设置value的序列化方式
      33. Jackson2JsonRedisSerializer jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
      34. redisTemplate.setValueSerializer(jsonSerializer);
      35. redisTemplate.setHashValueSerializer(jsonSerializer);
      36. return redisTemplate;
      37. }
      38. }
      39. pom.xml

        1. org.springframework.boot
        2. spring-boot-starter
        3. org.springframework.boot
        4. spring-boot-starter-web
        5. org.springframework.boot
        6. spring-boot-starter-data-redis
        7. org.springframework.boot
        8. spring-boot-starter-data-redis-reactive
        9. redis.clients
        10. jedis
        11. 3.6.1
        12. com.alibaba
        13. fastjson
        14. 1.2.54
        15. org.apache.commons
        16. commons-pool2
        17. org.projectlombok
        18. lombok
        19. true
        20. org.springframework.boot
        21. spring-boot-starter-test
        22. test

        使用如下:

        1. //直接注入即可使用
        2. @Autowired
        3. private RedisTemplate redisSingleNodeTemplate;
        4. @Autowired
        5. private RedisTemplate redisClusterTemplate;

      40. 相关阅读:
        jenkins 原理篇——pipeline流水线 声明式语法详解
        如何在jar包外设置配置文件
        可转债列表页与日频交易数据呈现:fastapi+antV G2
        2022“杭电杯”中国大学生算法设计超级联赛(8)签到题5题
        Docker基础语法学习笔记
        [SpringBoot系列]SSMP整合小项目
        canvas 画布绘制时钟
        雷电_安卓模拟器安装burpsuit_CA证书
        能源区块链实验室同俄罗斯碳基金签署重要战略合作协议
        2022 IDC中国未来企业大奖优秀奖颁布,华为云数据库助力德邦快递获奖
      41. 原文地址:https://blog.csdn.net/zhanghe687/article/details/134040147
        • 最新文章
        • 攻防演习之三天拿下官网站群
          数据安全治理学习——前期安全规划和安全管理体系建设
          企业安全 | 企业内一次钓鱼演练准备过程
          内网渗透测试 | Kerberos协议及其部分攻击手法
          0day的产生 | 不懂代码的"代码审计"
          安装scrcpy-client模块av模块异常,环境问题解决方案
          leetcode hot100【LeetCode 279. 完全平方数】java实现
          OpenWrt下安装Mosquitto
          AnatoMask论文汇总
          【AI日记】24.11.01 LangChain、openai api和github copilot
        • 热门文章
        • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
          奉劝各位学弟学妹们,该打造你的技术影响力了!
          五年了,我在 CSDN 的两个一百万。
          Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
          面试官都震惊,你这网络基础可以啊!
          你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
          心情不好的时候,用 Python 画棵樱花树送给自己吧
          通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
          13 万字 C 语言从入门到精通保姆级教程2021 年版
          10行代码集2000张美女图,Python爬虫120例,再上征途
        Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
        正则表达式工具 cron表达式工具 密码生成工具

        京公网安备 11010502049817号