×

redis集群 springboot SpringBoot

Redis集群整合到springboot框架

我的笔记 我的笔记 发表于2018-11-12 14:56:22 浏览3395 评论0

抢沙发发表评论

整合步骤

  1 配置application.properties

spring.redis.cluster.nodes=192.168.60.131:8000,192.168.60.131:8001,192.168.60.131:8002spring.redis.maxTotal=200spring.redis.maxIdle=8spring.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:800011                 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方法出现连接异常为例

 

我的笔记博客版权我的笔记博客版权