• Redis集群整合到springboot框架


    整合步骤

      1 配置application.properties

    spring.redis.cluster.nodes=192.168.60.131:8000,192.168.60.131:8001,192.168.60.131:8002
    spring.redis.maxTotal=200
    spring.redis.maxIdle=8
    spring.redis.minIdle=1

      2 编写配置类(完成初始化对象的过程)

     4     @Bean
     5         public JedisCluster getInstance(){
     6             //收集信息
     7             Set<HostAndPort> infoSet=new HashSet<HostAndPort>();
     8             String[] node=nodes.split(",");//192.168.60.131:8000数组
     9             for (String hostAndPort : node) {
    10                 //每次获取192.168.60.131:8000
    11                 String host=hostAndPort.split(":")[0];
    12                 Integer port=Integer.parseInt(hostAndPort.split(":")[1]);
    13                 infoSet.add(new HostAndPort(host, port));}
    14             //配置对象
    15             GenericObjectPoolConfig config=new GenericObjectPoolConfig();
    16             config.setMaxIdle(maxIdle);
    17             config.setMaxTotal(maxTotal);
    18             config.setMinIdle(minIdle);
    19             JedisCluster cluster=new JedisCluster(infoSet,config);
    20             return cluster;}

    3 封装底层api的类(RedisClusterService)

    4 测试

        需求:
        • 存数据,从浏览器传递一些参数id,name
        • 在代码中生成key值,将value存储在cluster集群
        • 通过key值获取集群的value,返回浏览器;

        测试代码端的高可用
      将前面存储的key值所在的节点宕机,再来访问查询的功能观察结果(不能查到,能查到,过一段时间能查到)

      jedisCluster代码客户端高可用

      初始化过程

      

      以set方法出现连接异常为例

  • 相关阅读:
    RocketMQ消息存储(一) mmap零拷贝
    RocketMQ消息存储(二) MappedFile
    RocketMQ(六) 服务端请求与响应的处理 源码分析
    shell脚本中的 [ 和 [[ 的区别(单中括号和双中括号的区别)
    版本回退
    红队字典收集整理
    武装你的 Burp Suite 好用插件分享
    RMI 2021【杂题】
    XXII Open Cup, Grand Prix of IMO【杂题】
    原生渲染自插入列表
  • 原文地址:https://www.cnblogs.com/nanlinghan/p/9945904.html
Copyright © 2020-2023  润新知