创建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. vue中的axios请求

    1.get请求 // get this.$axios.get('请求路径') .then(function (response) { console.log(response); // 成功 }) . ...

  2. Maven:Eclipse导入从SVN上检出的Maven多模块工程

    大致步骤: 1.从SVN中检出多模块项目,名称随意(Eclipse中可以在[Window ==>>Show View==>>Other==>>SVN==>&g ...

  3. mysql快速搭建从库

    基于mysqldump快速搭建从库 https://blog.csdn.net/leshami/article/details/44994329 使用xtrbackup克隆从库 https://blo ...

  4. 选择排序&冒泡排序

    //直接选择排序 #include<stdio.h> void SelectionSort(int arr[],int len) { int i,j; int k,min; int tem ...

  5. G6:AntV 的图可视化与图分析

    导读 G6 是 AntV 旗下的一款专业级图可视化引擎,它在高定制能力的基础上,提供简单.易用的接口以及一系列设计优雅的图可视化解决方案,是阿里经济体图可视化与图分析的基础设施.今年 AntV 11. ...

  6. 201509-1 数列分段 Java

    思路: 后一个和前一个不相等就算一段 import java.util.Scanner; public class Main { public static void main(String[] ar ...

  7. JavaWeb部分视频\2-12JSP,EL和JSTL

    JavaWeb知识结构图 第3节 EL介绍和运算符 && 第4节 EL获取域中存储的数据 ## EL表达式 1. 概念:Expression Language 表达式语言 2. 作用: ...

  8. PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  9. java反射使用详细例子

    1. 概念 反射,一种计算机处理方式.是程序可以访问.检测和修改它本身状态或行为的一种能力. 2. 反射机制的作用 通过反机制访问java类的属性,方法,构造方法等: 3.反射机制中的类 (1) ja ...

  10. JavaSE--对象克隆

    当拷贝一个变量时,原始变量与拷贝变量引用同一个对象,这就是说,改变一个变量所引用的对象将会对另一个变量产生影响. 如果创建一个对象的新的 copy,他的最初状态与 original 一样,但以后将可以 ...