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

最新文章

  1. 【GSM】GTM900C的应用——短信
  2. easyui 下拉树改造
  3. (译)如何优化cocos2d程序的内存使用和程序大小:第二部分(完)
  4. #iPhone6与iPhone6Plus适配#如何在Xcode 6中创建 PCH 文件
  5. 【转】delphi程序只允许运行一个实例的三种方法:
  6. C# 理解lock
  7. CustomProgressBar
  8. iOS中判断消息推送是否打开
  9. Codeforces Round #277 (Div. 2) 解题报告
  10. 与html相关的知识点整理
  11. i++和i--运算符优先级
  12. Use PRODUCT_USER_PROFILE To Limit User
  13. 从拉动APP下载谈运营
  14. 深入理解 JavaScript(三)
  15. python捕获异常、处理异常
  16. 【安全性测试】Android测试中的一点小发现
  17. VSCode typescript ctrl+shift+b can&#39;t be compiled error:TS5007
  18. SQL 必知必会&#183;笔记&lt;19&gt;使用游标
  19. ORA-00600: internal error code, arguments: [kcblasm_1], [103], [] bug
  20. Java并发(二十二):定时任务ScheduledThreadPoolExecutor

热门文章

  1. 控制终端tcgetattr函数与tcsetattr函数
  2. shell之read命令
  3. json数据操作
  4. (二)Spring 之IOC 详解
  5. CSU 1412 Line and Circles
  6. jquery datatable的详细用法
  7. day1 作业二:多级菜单操作
  8. Java Switch Statement
  9. c# winform读取及发送串口信号
  10. QString 与中文问题