【Redis学习之十一】Java客户端实现redis集群操作
客户端: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; } }
学习技术不是用来写HelloWorld和Demo的,而是要用来解决线上系统的真实问题的.