spring集成redis cluster

客户端采用最新的jedis 2.7

1.

maven依赖:

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.7.3</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=*:*
address2=*:*
address3=*:*
address4=*:*
address5=*:*
address6=*:*

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

JedisCluster jedisCluster;

				
时间: 2024-11-01 09:43:32

spring集成redis cluster的相关文章

Spring集成redis集群

Spring集成redis集群 有密码 Maven <jedis.version>2.9.0</jedis.version> <spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version> <spring.version>3.2.17.RELEASE</spring.version> <dependency> <groupId>org.s

Spring 集成Redis哨兵模式

Spring 集成Redis哨兵模式 1.pom文件添加以下jar <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- redis客户端jar -->

spring 集成redis客户端jedis(java)

jedis是redis的java客户端,spring将redis连接池作为一个bean配置. "redis.clients.jedis.JedisPool",这是单机环境适用的redis连接池. 1.maven导入相关包: <!-- redis依赖包 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> &l

Spring集成Redis使用注解

转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apac

升级spring&amp;集成Redis 二: spring4集成redis

1 添加Redis依赖 引入需要集成的redis版本 <!--spring redis 2.8.2 start--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <dependenc

Spring 集成Redis

官网: http://projects.spring.io/spring-data-redis/ 第一步:pom.xml中 导入依赖包 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.

spring集成redis

spring版本:4.1.6.RELEASE redis客户端:2.4.2 redis服务端:2.6.12 spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

Spring 集成 Redis

pom.xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.0.4.RELEASE</version> <exclusions> <exclusion> <groupId>org.slf4j</groupI

Spring Cache集成redis

Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set –有序集合)和hash(哈希类型).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据