spring + redis 集群

项目中的reids要从单机版更换到集群配置,我使用的是spring-data-redis.jar  主要难点就是jar包各种冲突。

<properties>
<springframework>4.3.3.RELEASE</springframework>
<log4j>1.2.17</log4j>
<springRedis>1.7.2.RELEASE</springRedis>
<jedis>2.9.0</jedis>
</properties>

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${springframework}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${springframework}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${springframework}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${springframework}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${springframework}</version>
		</dependency>
<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>${springRedis}</version>
		</dependency>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>${jedis}</version>
		</dependency>

jar包引对了剩下的就是基础配置了 redis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
 
 
    <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="maxRedirects" value="3"></property>
        <property name="clusterNodes">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host2}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host3}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
                </bean>
                 <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host4}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host5}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host6}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port6}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
 
    <bean id="redis4CacheConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
        <property name="timeout" value="${redis.timeout}" />
        <property name="poolConfig" ref="poolConfig"/>
    </bean>
 
 
    <bean name="keyStringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean name="valueStringRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redis4CacheConnectionFactory" />
        <property name="keySerializer" ref="keyStringRedisSerializer" />
        <property name="hashKeySerializer" ref="keyStringRedisSerializer" />
        <property name="valueSerializer" ref="valueStringRedisSerializer" />
        <property name="hashValueSerializer" ref="valueStringRedisSerializer" />
    </bean>
</beans>
有一点要注意 如果value值存储的有对象的需要加上:

<bean name="valueStringRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

然后就是redis操作工具类:

@Autowired
	private RedisTemplate redisTemplate;
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	public void setCacheObject(String key, Object value) {
		if (key == null || "".equals(key.trim()) || null == value) {
			logger.info("key为空或者值为空");
			return;
		}
		ValueOperations<String, Object> valueops = redisTemplate.opsForValue();
		valueops.set(key, value);
	}
	
	public void setCacheObject(String key, Object value, long timeout, TimeUnit unit) {
		
		if("".endsWith(key) || null == value)
		{
			logger.info("key为空或者值为空");
			return;
		}
		
		ValueOperations<String, Object> valueops = redisTemplate.opsForValue();
		
		if(timeout <= 0)
		{
			valueops.set(key, value);
		}
		else{
			
			valueops.set(key, value, timeout, unit);
		}
		
	}
	
	public Object getCacheObject(String key)
	{
		
		if("".equals(key) )
		{
			logger.info("key为空或者数据类型为空");
			return null;
		}
		
		Object object= null;
		
		if (!"".equals(key) && redisTemplate.hasKey(key)) {
			
			ValueOperations<String, Object> valueops = redisTemplate.opsForValue();
			object = valueops.get(key);
		}
		return object;
	}

搭建完成。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值