pom引入jedis的jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- redis jedisCluster集群配置 -->
<!-- <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="cn.zsmy.palmdoctor.redis.JedisClusterFactory">
<property name="addressConfig">
<value>classpath:redis.properties</value>
</property>
<property name="addressKeyPrefix" value="address" /> 属性文件里 key的前缀
<property name="password" value="123456" /> redis密码
<property name="timeout" value="300000" />
<property name="maxRedirections" value="6" />
<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
</bean> -->
redis.properties文件内容:
address1=192.168.1.249:7000
address2=192.168.1.249:7001
address3=192.168.1.249:7002
address4=192.168.1.248:7003
address5=192.168.1.248:7004
address6=192.168.1.248:7005
@Autowired
private JedisCluster jedisCluster;
保存:
byte[] key = SerializeUtil.serialize(buildRedisSessionKey(session.getId()));
jedisCluster.set(SerializeUtil.serialize(buildRedisSessionKey(session.getId())), SerializeUtil.serialize(session));
jedisCluster.expire(key, SESSION_VAL_TIME_SPAN);
删除:
jedisCluster.del(SerializeUtil.serialize(buildRedisSessionKey(id)));
查询:
byte[] value = jedisCluster.get(SerializeUtil.serialize(buildRedisSessionKey(id)));*/
session = SerializeUtil.deserialize(value, Session.class);
以上示例中的key与value都是序列化的,因为保存的是session信息,保存普通对象时可以不序列化。
单个redis配置
<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1000"/>
<property name="minIdle" value="100"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<!-- redis的连接池pool,不是必选项:timeout/password -->
<bean id = "jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="192.168.1.249"/><!--host-->
<constructor-arg index="2" value="6379" type="int"/><!--port -->
<constructor-arg index="3" value="2000" type="int"/> <!-- timeout -->
<constructor-arg index="4" value="123456"/> <!-- password -->
</bean>
@Autowired private JedisPool jedisPool; public Jedis getJedis() { Jedis jedis = null; try { jedis = jedisPool.getResource(); } catch (Exception e) { throw new JedisConnectionException(e); } return jedis; } public byte[] getValueByKey(byte[] key) throws Exception { Jedis jedis = null; byte[] result = null; //boolean isBroken = false; try { jedis = getJedis(); //jedis.select(DB_INDEX); result = jedis.get(key); } catch (Exception e) { //isBroken = true; throw e; } finally { returnResource(jedis); } return result; } public void deleteByKey(byte[] key) throws Exception { Jedis jedis = null; //boolean isBroken = false; try { jedis = getJedis(); //jedis.select(DB_INDEX); jedis.del(key); } catch (Exception e) { //isBroken = true; throw e; } finally { returnResource(jedis); } } public void saveValueByKey(byte[] key, byte[] value, int expireTime) throws Exception { Jedis jedis = null; //boolean isBroken = false; try { jedis = getJedis(); //jedis.select(DB_INDEX); jedis.set(key, value); if (expireTime > 0) jedis.expire(key, expireTime); } catch (Exception e) { //isBroken = true; throw e; } finally { returnResource(jedis); } } public void returnResource(Jedis jedis) { try { if (jedis != null) { getPool().returnResource(jedis); } } catch (Exception e) { Constant.MY_LOG.error("return back jedis failed", e); } }
序列化代码:
public class SerializeUtil { public static byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); os.close(); bos.close(); rv = bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); Constant.MY_LOG.info("serialize error"); } finally { close(os); close(bos); } return rv; } public static Object deserialize(byte[] in) { return deserialize(in, Object.class); } @SuppressWarnings("unchecked") public static <T> T deserialize(byte[] in, Class<T> requiredType) { Object rv = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); rv = is.readObject(); } } catch (Exception e) { e.printStackTrace(); Constant.MY_LOG.info("deserialize error"); } finally { close(is); close(bis); } return (T) rv; } private static void close(Closeable closeable) { if (closeable != null) try { closeable.close(); } catch (IOException e) { e.printStackTrace(); Constant.MY_LOG.info("close stream error"); } } }
spring-context-jedis-cluster.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <description>Jedis Cluster Configuration集群</description> <!-- 加载配置属性文件 按需加载 --> <context:property-placeholder ignore-unresolvable="true" location="classpath:redis-cluster.properties" /> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="${redis.maxRedirects}"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <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.RedisClusterNode"> <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.RedisClusterNode"> <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.RedisClusterNode"> <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.RedisClusterNode"> <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.RedisClusterNode"> <constructor-arg name="host" value="${redis.host6}"></constructor-arg> <constructor-arg name="port" value="${redis.port6}"></constructor-arg> </bean> </set> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> </bean> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <constructor-arg ref="redisClusterConfiguration" /> <constructor-arg ref="jedisPoolConfig" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jeidsConnectionFactory" /> </bean> </beans>
redis-cluster.properties
#cluster configuration
redis.host1=xx.xx.xx.xx
redis.port1=7000
redis.host2=xx.xx.xx.xx
redis.port2=7001
redis.host3=xx.xx.xx.xx
redis.port3=7002
redis.host4=xx.xx.xx.xx
redis.port4=7000
redis.host5=xx.xx.xx.xx
redis.port5=7001
redis.host6=xx.xx.xx.xx
redis.port6=7002
redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600