当前位置: 移动技术网 > IT编程>开发语言>Java > Redis集群整合到springboot框架

Redis集群整合到springboot框架

2018年11月12日  | 移动技术网IT编程  | 我要评论

整合步骤

  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方法出现连接异常为例

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网