須要jar包redis
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.9.0</version> 5 </dependency>
spring-redis.xml配置文件:spring
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:jee="http://www.springframework.org/schema/jee" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation="http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 11 http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context-4.0.xsd 15 http://www.springframework.org/schema/jee 16 http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 17 http://www.springframework.org/schema/tx 18 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 19 20 <context:property-placeholder location="classpath:redis.properties"/> 21 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 22 <property name="maxIdle" value="${redis.maxIdle}"/> 23 <property name="maxTotal" value="${redis.maxActive}" /> 24 <property name="maxWaitMillis" value="${redis.maxWait}" /> 25 <property name="testOnBorrow" value="${redis.testOnBorrow}"/> 26 </bean> 27 28 <bean id="hostport1" class="redis.clients.jedis.HostAndPort"> 29 <constructor-arg name="host" value="192.168.1.171" /> 30 <constructor-arg name="port" value="7001" /> 31 </bean> 32 33 <bean id="hostport2" class="redis.clients.jedis.HostAndPort"> 34 <constructor-arg name="host" value="192.168.1.171" /> 35 <constructor-arg name="port" value="7002" /> 36 </bean> 37 38 <bean id="hostport3" class="redis.clients.jedis.HostAndPort"> 39 <constructor-arg name="host" value="192.168.1.171" /> 40 <constructor-arg name="port" value="7003" /> 41 </bean> 42 43 <bean id="hostport4" class="redis.clients.jedis.HostAndPort"> 44 <constructor-arg name="host" value="192.168.1.171" /> 45 <constructor-arg name="port" value="7004" /> 46 </bean> 47 48 <bean id="hostport5" class="redis.clients.jedis.HostAndPort"> 49 <constructor-arg name="host" value="192.168.1.171" /> 50 <constructor-arg name="port" value="7005" /> 51 </bean> 52 53 <bean id="hostport6" class="redis.clients.jedis.HostAndPort"> 54 <constructor-arg name="host" value="192.168.1.171" /> 55 <constructor-arg name="port" value="7006" /> 56 </bean> 57 58 <bean id="redisCluster" class="redis.clients.jedis.JedisCluster"> 59 <constructor-arg name="jedisClusterNode"> 60 <set> 61 <ref bean="hostport1"/> 62 <ref bean="hostport2"/> 63 <ref bean="hostport3"/> 64 <ref bean="hostport4"/> 65 <ref bean="hostport5"/> 66 <ref bean="hostport6"/> 67 </set> 68 </constructor-arg> 69 <constructor-arg name="connectionTimeout" value="6000" /> 70 <constructor-arg name="soTimeout" value="2000" /> 71 <constructor-arg name="maxAttempts" value="3" /> 72 <constructor-arg name="password" value="123456" /> 73 <constructor-arg name="poolConfig"> 74 <ref bean="jedisPoolConfig"/> 75 </constructor-arg> 76 </bean> 77 78 </beans>
因爲是測試用的,redis.properties中的參數隨意寫的,須要根據項目的實際狀況調優:測試
1 redis.maxIdle=10 2 redis.maxActive=1000 3 redis.maxWait=30000 4 redis.testOnBorrow=true
以上配置好了,就能夠開始測試了。。。spa
1 package com.yucong.redis; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 import redis.clients.jedis.JedisCluster; 5 6 public class TestClusterRedis { 7 8 public static void main(String[] args) { 9 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring-redis.xml"); 10 JedisCluster jc = context.getBean(JedisCluster.class); 11 System.out.println(jc.set("name", "yucong")); 12 System.out.println(jc.set("age", "28")); 13 System.out.println(jc.set("sex", "男")); 14 15 System.out.println(jc.get("name")); 16 System.out.println(jc.get("age")); 17 System.out.println(jc.get("sex")); 18 } 19 }
想一想,也真是夠簡單了...可是若是項目在運行的過程當中,須要增長主從節點的時候,在不停掉項目的狀況下,如何動態地添加主從節點,是頗有必要的,且聽下回分解。。。code
Zookeeper能夠解決此問題。xml