创建JedisCluster类连接redis集群。

@Test

public void testJedisCluster() throws Exception {

     //创建一连接,JedisCluster对象,在系统中是单例存在

     Set<HostAndPort> nodes = new HashSet<>();

     nodes.add(new HostAndPort("127.0.0.1", 7001));

     nodes.add(new HostAndPort("127.0.0.1", 7002));

     nodes.add(new HostAndPort("127.0.0.1", 7003));

     nodes.add(new HostAndPort("127.0.0.1", 7004));

     nodes.add(new HostAndPort("127.0.0.1", 7005));

     nodes.add(new HostAndPort("127.0.0.1", 7006));

     JedisCluster cluster = new JedisCluster(nodes);

     //执行JedisCluster对象中的方法,方法和redis一一对应。

     cluster.set("cluster-test", "my jedis cluster test");

     String result = cluster.get("cluster-test");

     System.out.println(result);

     //程序结束时需要关闭JedisCluster对象

     cluster.close();

}

使用spring

  • 配置applicationContext.xml
<!-- 连接池配置 -->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

     <!-- 最大连接数 -->

     <property name="maxTotal" value="30" />

     <!-- 最大空闲连接数 -->

     <property name="maxIdle" value="10" />

     <!-- 每次释放连接的最大数目 -->

     <property name="numTestsPerEvictionRun" value="1024" />

     <!-- 释放连接的扫描间隔(毫秒) -->

     <property name="timeBetweenEvictionRunsMillis" value="30000" />

     <!-- 连接最小空闲时间 -->

     <property name="minEvictableIdleTimeMillis" value="1800000" />

     <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->

     <property name="softMinEvictableIdleTimeMillis" value="10000" />

     <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->

     <property name="maxWaitMillis" value="1500" />

     <!-- 在获取连接的时候检查有效性, 默认false -->

     <property name="testOnBorrow" value="true" />

     <!-- 在空闲时检查有效性, 默认false -->

     <property name="testWhileIdle" value="true" />

     <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->

     <property name="blockWhenExhausted" value="false" />

</bean>

<!-- redis集群 -->

<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">

     <constructor-arg index="0">

         <set>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7001"></constructor-arg>

              </bean>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7002"></constructor-arg>

              </bean>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7003"></constructor-arg>

              </bean>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7004"></constructor-arg>

              </bean>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7005"></constructor-arg>

              </bean>

              <bean class="redis.clients.jedis.HostAndPort">

                   <constructor-arg index="0" value="192.168.101.3"></constructor-arg>

                   <constructor-arg index="1" value="7006"></constructor-arg>

              </bean>

         </set>

     </constructor-arg>

     <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>

</bean>
  • 测试代码
private ApplicationContext applicationContext;

     @Before

     public void init() {

         applicationContext = new ClassPathXmlApplicationContext(

                   "classpath:applicationContext.xml");

     }

     // redis集群

     @Test

     public void testJedisCluster() {

         JedisCluster jedisCluster = (JedisCluster) applicationContext

                   .getBean("jedisCluster");

         jedisCluster.set("name", "zhangsan");

         String value = jedisCluster.get("name");

         System.out.println(value);

     }

Java操作redis客户端Jedis连接集群(Cluster)的更多相关文章

  1. 通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...

  2. redis客户端可以连接集群,但JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...

  3. Java操作redis客户端Jedis使用

    1.1   jedis介绍 Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java的客户端,有 ...

  4. java操作redis之jedis篇

    首先来简单介绍一下jedis,其实一句话就可以概括的,就是java操作redis的一种api.我们知道redis提供了基本上所有常用编程语言的clients,大家可以到http://redis.io/ ...

  5. 20190928-02使用Redis客户端Jedis连接Redis,以及用Java代码操作Redis 000 030

    启动redis package com.yujie.jedis; import java.util.HashMap; import java.util.Map; import java.util.Se ...

  6. java操作redis。jedis使用api

    package com.wujintao.redis; import java.util.Date; import java.util.HashMap; import java.util.Iterat ...

  7. redis客户端jedis连接和spring结合

    摘自传智博客课程 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="htt ...

  8. jedis连接集群

    /**        * 集群版        */       @Test       public  void JedisJiuQun()       {           HashSet< ...

  9. 关于Redis 分布式 微服务 集群Cluster

    一:Redis 1,redis是一个高性能的键值对存储方式的数据库,同时还提供list,set,zset,hash等数据结构的存储. 2,Redis运行在内存中但是可以持久化到磁盘,所以在对不同数据集 ...

随机推荐

  1. 吴裕雄--天生自然TensorFlow2教程:数据加载

    import tensorflow as tf from tensorflow import keras # train: 60k | test: 10k (x, y), (x_test, y_tes ...

  2. 吴裕雄--天生自然 PHP开发学习:常量

    <?php // 区分大小写的常量名 define("GREETING", "欢迎访问 Runoob.com"); echo GREETING; // 输 ...

  3. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    创建springboot项目,且不采用<parent>引入springboot时,pom.xml如下: <?xml version="1.0" encoding= ...

  4. PAT 2019 春

    算是第二次做这套题吧,感觉从上次考试到现在自己有了挺大提高,提前30min做完了. 7-1 Sexy Primes 读懂题意就行. #include <cstdio> #include & ...

  5. redis(四)----发布订阅

    发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合.pub /sub不仅仅解决发布者和订阅者直接代码级别耦合,也解决两者在物理部署上的耦合.废话不多说,直接 ...

  6. Tensorflow——用openpose进行人体骨骼检测

    https://blog.csdn.net/eereere/article/details/80176007 参考资料code:https://github.com/ildoonet/tf-pose- ...

  7. 用户使用API函数对创建的文件进行读写操作

    HANDLE handle; //定义文件句柄 ]; //定义缓冲区 int i; //接收实际操作的字节数 CString str; //定义字符串变量 handle = ::CreateFile( ...

  8. JS向固定数组中添加不重复元素并冒泡排序

    向数组{7,20,12,6,25}中添加一个不重复的数字,然后按照从小到大的顺序排列 源代码: <!DOCTYPE html> <html> <head> < ...

  9. Java:面向对象的理解

    面向对象 一切皆对象.程序是对象的集合,它们通过发送消息来告知彼此所要做的.也就是说:以对象为中心,以消息(发送消息即为函数调用)为驱动.对象具有状态,行为和标识. 状态:指类的数据成员,即属性: 行 ...

  10. maven镜像仓库

    国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...