redis实战_08_redis集群与spring整合
Posted 喻聪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis实战_08_redis集群与spring整合相关的知识,希望对你有一定的参考价值。
需要jar包
1 <dependency> 2 <groupId>redis.clients</groupId> 3 <artifactId>jedis</artifactId> 4 <version>2.8.0</version> 5 </dependency>
spring-redis.xml配置文件:
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="nodes"> 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="timeout" value="6000" /> 70 <constructor-arg name="poolConfig"> 71 <ref bean="jedisPoolConfig"/> 72 </constructor-arg> 73 </bean> 74 75 </beans>
由于是测试用的,redis.properties中的参数随意写的,需要根据项目的实际情况调优:
1 redis.maxIdle=10 2 redis.maxActive=1000 3 redis.maxWait=30000 4 redis.testOnBorrow=true
以上配置好了,就可以开始测试了。。。
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("set", "男")); 14 15 System.out.println(jc.get("name")); 16 System.out.println(jc.get("age")); 17 System.out.println(jc.get("sex")); 18 } 19 }
想想,也真是够简单了...但是如果项目在运行的过程中,需要增加主从节点的时候,在不停掉项目的情况下,如何动态地添加主从节点,是很有必要的,且听下回分解。。。
以上是关于redis实战_08_redis集群与spring整合的主要内容,如果未能解决你的问题,请参考以下文章
Redis_13_Redis集群实现RedisCluster应对大数据量