码迷,mamicode.com
首页 > 编程语言 > 详细

redis实战_08_redis集群与spring整合

时间:2017-02-07 13:46:48      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:4.0   span   pac   tor   配置   getbean   pid   配置文件   client   

需要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整合

标签:4.0   span   pac   tor   配置   getbean   pid   配置文件   client   

原文地址:http://www.cnblogs.com/yucongblog/p/6373477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!