客户端:jedis-2.7.2.jar

配置文件两种方式:

properties:

redis.cluster.nodes1=192.168.1.117
redis.cluster.port1=7001
redis.cluster.nodes2=192.168.1.117
redis.cluster.port2=7002
redis.cluster.nodes3=192.168.1.117
redis.cluster.port3=7003
redis.cluster.nodes4=192.168.1.117
redis.cluster.port4=7004
redis.cluster.nodes5=192.168.1.117
redis.cluster.port5=7005
redis.cluster.nodes6=192.168.1.117
redis.cluster.port6=7006
redis.cluster.nodes1=192.168.1.117
redis.cluster.config.max-total=100
redis.cluster.config.max-idle=20
redis.cluster.config.max-waitmillis=-1
redis.cluster.config.onborrow=true
redis.cluster.timeout=6000
redis.cluster.max-redirections=100

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:redis.properties" />
<context:component-scan base-package="com.x.redis.dao">
</context:component-scan>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="hostport1" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7770" />
</bean>
<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7771" />
</bean>
<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7772" />
</bean>
<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7773" />
</bean>
<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7774" />
</bean>
<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7775" />
</bean> <bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<ref bean="hostport1" />
<ref bean="hostport2" />
<ref bean="hostport3" />
<ref bean="hostport4" />
<ref bean="hostport5" />
<ref bean="hostport6" />
</set>
</constructor-arg>
<constructor-arg name="timeout" value="6000" />
<constructor-arg name="poolConfig">
<ref bean="jedisPoolConfig" />
</constructor-arg>
</bean>
</beans>

创建集群bean:

package test;

import java.util.HashSet;
import java.util.Set; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig; @Configuration
//启动缓存配置
@ComponentScan("bhz")
@PropertySource("classpath:redis.properties")
public class ClusterConfig { @Autowired
private Environment environment; @Bean
public JedisCluster getRedisCluster(){
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes1"), Integer.parseInt(environment.getProperty("redis.cluster.port1"))));
jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes2"), Integer.parseInt(environment.getProperty("redis.cluster.port2"))));
jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes3"), Integer.parseInt(environment.getProperty("redis.cluster.port3"))));
jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes4"), Integer.parseInt(environment.getProperty("redis.cluster.port4"))));
jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes5"), Integer.parseInt(environment.getProperty("redis.cluster.port5"))));
jedisClusterNode.add(new HostAndPort(environment.getProperty("redis.cluster.nodes6"), Integer.parseInt(environment.getProperty("redis.cluster.port6"))));
JedisPoolConfig cfg = new JedisPoolConfig();
cfg.setMaxTotal(Integer.parseInt(environment.getProperty("redis.cluster.config.max-total")));
cfg.setMaxIdle(Integer.parseInt(environment.getProperty("redis.cluster.config.max-idle")));
cfg.setMaxWaitMillis(Integer.parseInt(environment.getProperty("redis.cluster.config.max-waitmillis")));
cfg.setTestOnBorrow(Boolean.parseBoolean(environment.getProperty("redis.cluster.config.onborrow")));
JedisCluster jc = new JedisCluster(jedisClusterNode, Integer.parseInt(environment.getProperty("redis.cluster.timeout")), Integer.parseInt(environment.getProperty("redis.cluster.max-redirections")), cfg);
return jc;
} }

【Redis学习之十一】Java客户端实现redis集群操作的更多相关文章

  1. Redis集群环境使用的是redis4.0.x的版本,在用java客户端jedisCluster启动集群做数据处理时报java.lang.NumberFormatException: For input string: "7003@17003"问题解决

    java.lang.NumberFormatException: For input string: "7003@17003" at java.lang.NumberFormatE ...

  2. Java客户端访问HBase集群解决方案(优化)

    测试环境:Idea+Windows10 准备工作: <1>.打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户 ...

  3. golang redis集群操作:redis-go-cluster

    背景 感觉redis-cli desktop及其难用,最近用golang做了个redis查询工具,支持单例和集群操作,终于不再卡顿!!! 用到的包 "github.com/garyburd/ ...

  4. 使用java客户端调用redis

    Redis支持很多编程语言的客户端,有C.C#.C++.Clojure.Common Lisp.Erlang.Go.Lua.Objective-C.PHP.Ruby.Scala,甚至更时髦的Node. ...

  5. 15套java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、Spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩展. ...

  6. 15套java架构师、集群、高可用、高可扩 展、高性能、高并发、性能优化Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程

    * { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...

  7. Redis——学习之路三(初识redis config配置)

    我们先看看config 默认情况下系统是怎么配置的.在命令行中输入 config get *(如图) 默认情况下有61配置信息,每一个命令占两行,第一行为配置名称信息,第二行为配置的具体信息.     ...

  8. Redis——学习之路二(初识redis服务器命令)

    上一章我们已经知道了如果启动redis服务器,现在我们来学习一下,以及如何用客户端连接服务器.接下来我们来学习一下查看操作服务器的命令. 服务器命令: 1.info——当前redis服务器信息   s ...

  9. 分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

  10. 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)

    本文是redis学习系列的第四篇,前面我们学习了redis的数据结构和一些高级特性,点击下面链接可回看 <详细讲解redis数据结构(内存模型)以及常用命令> <redis高级应用( ...

随机推荐

  1. CF891C Envy 最小生成树/虚树

    正解:最小生成树/虚树 解题报告: 传送门! sd如我就只想到了最暴力的想法,一点儿优化都麻油想到,,,真的菜到爆炸了QAQ 然后就分别港下两个正解QAQ 法一,最小生成树 这个主要是要想到关于最小生 ...

  2. MySQL前缀索引和索引选择性

    有时候需要索引很长的字符列,这会让索引变得大且慢.通常可以索引开始的部分字符,这样可以大大节约索引空间,从而提高索引效率.但这样也会降低索引的选择性.索引的选择性是指不重复的索引值(也称为基数,car ...

  3. FastReport快速安装教程

    安装后需要覆盖的文件全部复制到安装目录 替换后的安装目录 选中Recompile右键必须以管理员运行 看图选择 编译后,需要汉化的话看下图选择 怎么添加Library直接看图按顺序进行 选择insta ...

  4. 下载pywin32

    下载pywin32 链接:sourceforge.net/projects/pywin32/files/ 1.找到一个pywin32的文件夹 2.下一级目录里面有多个文件夹. 3.打开Build222 ...

  5. 前端文档汇总(含代码规范、开发流程、知识分享,持续更新) front-end-Doc

    https://juejin.im/post/5b1205b1f265da6e1a602a62 https://juejin.im/post/5b1205b1f265da6e1a602a62 http ...

  6. jquery中把一串字符串分割,如:123456789后者abcdefg类型的

    函数:slice() 功能:arrayObject.slice(start,end) start:必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说,-1 指最后一个元 ...

  7. Spark SQL 函数全集

    org.apache.spark.sql.functions是一个Object,提供了约两百多个函数. 大部分函数与Hive的差不多. 除UDF函数,均可在spark-sql中直接使用. 经过impo ...

  8. Py打开pkl文件【转载】

    转自:https://blog.csdn.net/u010041824/article/details/78391683 1.打开带有中文的pkl文件或者其他字符的文件不能用默认的ascii格式打开, ...

  9. [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  10. securecrt配置经验总结(home.key和颜色)

    还是用securecrt portable方便,配好了,换个机器,打包拷贝过去就行了.不用从两个地方去打包. 参考了网上的资料,颜色参考http://www.jackxiang.com/post/58 ...