创建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. SPOJ FISHER + FPOLICE SPFA+背包

    当初第一次做的是FPLICE这个题,当时就觉得要用图论去搜索,但是当时陷入死思维就是 dp[][]两个维度都是点,这样就违背了题目的本意,题目给定了一个时间T,在不超过时间T的情况下求最小的消耗,这不 ...

  2. Callable、Future、线程池简单使用

    Callable.Future与线程池 在创建新线程的三种方式中,继承Thread和实现Runnable接口两种方式都都没有返回值,因此当我们想要获取子线程计算结果时只能设置共享数据,同时还需要考虑同 ...

  3. HashMap的fast-fail和ConcurrentHashMap的fail-safe实例

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 Java HashMap遍历过程中如果元素被修改会导致遍历失败,ConcurrentHashMap则不会有 ...

  4. jetty配置远程debug

    1.配置远程jetty 服务器的 bin/jetty.sh JAVA_OPTIONS+=("-Xdebug -Xrunjdwp:server=y,transport=dt_socket,ad ...

  5. Ubuntu apt install 下载软件很慢的解决办法

    1.打开/etc/apt/sources.list 将内容替换为以下内容(注意把sources.list文件备份一下) deb http://mirrors.aliyun.com/ubuntu/ xe ...

  6. 冲刺期末阶段一<公文档案流转管理系统>

    今天下午的四节课要求自己完成公文流转管理系统,并规定时间看个人进程,相对来说我对增删改查掌握的不彻底,对项目的逻辑框架不太熟练,所以我感觉今天的进度有点慢.有待继续学习. 完成进度:1.分步骤先理清整 ...

  7. Django2.0——请求与响应(下)

    上篇讲完了请求,这篇接着讲下响应,django响应类型大致有以下几种 HttpResponse:返回简单的字符串 render:渲染模板 redirect:重定向 JsonResponse:返回jso ...

  8. C语言-数组的深入学习

    深入学习一下数组1.从内存角度来讲:数组变量就是一次分配多个变量,而且这些变量的地址是连续的,也就是存放这些变量的存储单元是依次相连接的.而且这多个变量必须单独访问,不可以一起访问的.因为他们的地址彼 ...

  9. Delphi调用c++写的dll (me)

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  10. mysql not in 或 in 优化

    在MySQL 中,not in 或in 优化思路, 利用left join 来优化,类似如下的查询方式: select id from a where id in (select id from b ...