Spring+Redis Cluster3.0.x
2016-04-28 00:00
696 查看
摘要: Spring集成Redis Cluster
Redis properties:
Redis哨兵模式配置方法:
http://my.oschina.net/manmao/blog/550428
Redis properties:
RedisCacheUtil.java
实现代码
http://www.oschina.net/code/snippet_1403215_55824
Redis 3.0集群搭建方法:
http://hot66hot.iteye.com/blog/2050676Redis 3.0.x使用方法
1.Maven依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency>
2.Spring配置
<bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" > <property name="maxWaitMillis" value="-1" /> <property name="maxTotal" value="1000" /> <property name="minIdle" value="8" /> <property name="maxIdle" value="100" /> </bean> <bean id="jedisCluster" class="xxx.JedisClusterFactory"> <property name="addressConfig"> <value>classpath:connect-redis.properties</value> </property> <property name="addressKeyPrefix" value="address" /> <!-- 属性文件里 key的前缀 --> <property name="timeout" value="300000" /> <property name="maxRedirections" value="6" /> <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" /> </bean>
3.增加connect-redis.properties 配置文件
这里配置了6个节点address1=172.16.23.27:6379 address2=172.16.23.27:6380 address3=172.16.23.27:6381 address4=172.16.23.27:6382 address5=172.16.23.27:6383 address6=172.16.23.27:6384
4.增加实现的Java类
import java.util.HashSet; import java.util.Properties; import java.util.Set; import java.util.regex.Pattern; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean { private Resource addressConfig; private String addressKeyPrefix ; private JedisCluster jedisCluster; private Integer timeout; private Integer maxRedirections; private GenericObjectPoolConfig genericObjectPoolConfig; private Pattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$"); @Override public JedisCluster getObject() throws Exception { return jedisCluster; } @Override public Class<? extends JedisCluster> getObjectType() { return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class); } @Override public boolean isSingleton() { return true; } private Set<HostAndPort> parseHostAndPort() throws Exception { try { Properties prop = new Properties(); prop.load(this.addressConfig.getInputStream()); Set<HostAndPort> haps = new HashSet<HostAndPort>(); for (Object key : prop.keySet()) { if (!((String) key).startsWith(addressKeyPrefix)) { continue; } String val = (String) prop.get(key); boolean isIpPort = p.matcher(val).matches(); if (!isIpPort) { throw new IllegalArgumentException("ip 或 port 不合法"); } String[] ipAndPort = val.split(":"); HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1])); haps.add(hap); } return haps; } catch (IllegalArgumentException ex) { throw ex; } catch (Exception ex) { throw new Exception("解析 jedis 配置文件失败", ex); } } @Override public void afterPropertiesSet() throws Exception { Set<HostAndPort> haps = this.parseHostAndPort(); jedisCluster = new JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig); } public void setAddressConfig(Resource addressConfig) { this.addressConfig = addressConfig; } public void setTimeout(int timeout) { this.timeout = timeout; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public void setAddressKeyPrefix(String addressKeyPrefix) { this.addressKeyPrefix = addressKeyPrefix; } public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) { this.genericObjectPoolConfig = genericObjectPoolConfig; } }
5.自动注入
@Autowired private JedisCluster jedisCluster;
附录:
1.Redis Sentinel模式
Spring配置<!-- Redis Sentinel 集群配置 --> <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <constructor-arg name="master" value="${redis.master}"/> <constructor-arg name="sentinelHostAndPorts"> <set> <value>${redis.sentinels}</value> </set> </constructor-arg> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> <property name="usePool" value="true"></property> <property name="password" value="${redis.password}" /> <property name="timeout" value="${redis.timeout}" /> <property name="database" value="${redis.db}" /> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="defaultSerializer" ref="stringRedisSerializer" /> </bean> <bean id="cacheUtil" class="com.top.biz.util.RedisCacheUtil"> <property name="redisTemplate" ref="redisTemplate" /> </bean>
Redis properties:
redis.master=mymaster redis.sentinels=127.0.0.1:26379 redis.password= redis.db=0 redis.timeout=2000 redis.maxIdle=5 redis.maxTotal=20 #redis.testOnBorrow=true redis.testOnBorrow=false
Redis哨兵模式配置方法:
http://my.oschina.net/manmao/blog/550428
2.Redis单机配置
<!-- 配置Redis方法 单机配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}"></property> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="usePool" value="true"></property> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="password" value="${redis.password}" /> <property name="timeout" value="${redis.timeout}" /> <property name="database" value="${redis.db}"></property> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="defaultSerializer" ref="stringRedisSerializer" /> </bean> <bean id="cacheUtil" class="com.top.biz.util.RedisCacheUtil"> <property name="redisTemplate" ref="redisTemplate" /> </bean> <!--StringRedis Template 使用方法 --> <!-- <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> --> <!-- @Resource(name='stringRedisTemplate') private StringRedisTemplate redisTemplate; -->
Redis properties:
redis.host=127.0.0.1 redis.port=6379 redis.password= redis.db=0 redis.timeout=2000 redis.maxIdle=5 redis.maxTotal=20 #redis.testOnBorrow=true redis.testOnBorrow=false
3.RedisUtil封装类
com.top.biz.util.RedisCacheUtilRedisCacheUtil.java
实现代码
http://www.oschina.net/code/snippet_1403215_55824
相关文章推荐
- Linux下Redis服务器安装配置
- 安装Redis完整过程
- flask+pypy+setuptools+redis+MySQLdb安装
- redis持久化
- Redis的主从模式-Redis学习笔记五
- 【Redis源码剖析】 - Redis之事务的实现原理
- linux安装phpredis扩展
- linux 安装redis
- ridis集群搭建部署详细文档
- redis安全与管理
- Redis在CentOS6.4中的安装
- redis3集群安装剖析
- redis学习
- redis集群安装提高
- redis3集群安装入门
- Redis 安装与简单示例(转)
- redis-stat
- Redis Sentinel集群方案--单机测试
- Redis命令-有序集合-zscan
- springMVC整合jedis+redis