redis实战_08_redis集群与spring整合
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了redis实战_08_redis集群与spring整合,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4639字,纯文字阅读大概需要7分钟。
内容图文
![redis实战_08_redis集群与spring整合](/upload/InfoBanner/zyjiaocheng/1057/28c0276698294b1c8b195d3208974aaf.jpg)
需要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", "男")); 1415 System.out.println(jc.get("name")); 16 System.out.println(jc.get("age")); 17 System.out.println(jc.get("sex")); 18 } 19 }
想想,也真是够简单了...但是如果项目在运行的过程中,需要增加主从节点的时候,在不停掉项目的情况下,如何动态地添加主从节点,是很有必要的,且听下回分解。。。
原文:http://www.cnblogs.com/yucongblog/p/6373477.html
内容总结
以上是互联网集市为您收集整理的redis实战_08_redis集群与spring整合全部内容,希望文章能够帮你解决redis实战_08_redis集群与spring整合所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。