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

redis+spring配置的更多相关文章

  1. 征服 Redis + Jedis + Spring —— 配置&常规操作

    Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spri ...

  2. spring boot 2.0.4 Redis缓存配置

    spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作 import org.springframework.cache.CacheManager; ...

  3. spring集成redis——主从配置以及哨兵监控

    Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...

  4. springmvc+spring+jpa(hibernate)+redis+maven配置

    废话不多少 项目结构 pom.xml配置例如以下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  5. spring data redis jackson 配置,工具类

    spring data redis 序列化有jdk .jackson.string 等几种类型,自带的jackson不熟悉怎么使用,于是用string类型序列化,把对象先用工具类转成string,代码 ...

  6. Spring配置redis及使用

    一.redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. ...

  7. spring data jpa和spring data redis同时配置时,出现Multiple Spring Data modules found, entering strict repository configuration mode错误

    问题说明 data jpa和data redis同时配置时,出现Spring modules spring Spring Data Release Train <dependencyManage ...

  8. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  9. Spring Boot 结合 Redis 序列化配置的一些问题

    前言 最近在学习Spring Boot结合Redis时看了一些网上的教程,发现这些教程要么比较老,要么不知道从哪抄得,运行起来有问题.这里分享一下我最新学到的写法 默认情况下,Spring 为我们提供 ...

随机推荐

  1. mac 安装mongodb与常用操作

    1.安装 brew update brew install mongodb 2.启动mongo mongod --config /usr/local/etc/mongod.conf 3.启动 mong ...

  2. BestCoder #88(1001 1002)

    Find Q Accepts: 392 Submissions: 780 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131 ...

  3. **[权限控制] 利用CI钩子实现权限认证

    http://codeigniter.org.cn/forums/thread-10877-1-1.html 一直没找到CI的权限认证扩展,以前好像找到过一个老外的扩展,不过不怎么好用,现在记不清了, ...

  4. Js添加、读取、删除cookie,判断cookie是否有效,指定domain域下主路径path下设置cookie,设置expires过期时间

    有时我们需要用cookie保存用户名,记录登录状态,如何正确判断该机用户cookie是否存在呢?不能简单使用a!=”这样的写法. 正确方法是:判断是否存在名为username3的cookie,使用do ...

  5. html 条件注释判断浏览器版本<!--[if lt IE 9]>

    <!--[if lte IE 8]><script>window.location.href='http://support.dmeng.net/upgrade-your-br ...

  6. CyclicBarrier 简介

    CyclicBarrier 简介 CyclicBarrier 的字面意思是可循环使用(Cyclic)的屏障(Barrier). 它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被阻塞,直到最 ...

  7. sublime text3 如何在多行前面快速插入序号

    sublime text3 如何在多行前面快速插入序号 1.需要安装InsertNums插件 首选项 -> Package Control -> Install Package -> ...

  8. db2部署与数据仓库应用

    概念特性 安装 基础命令 连接 监控 存储过程 数据合并 Merge Into是增量备份 结果集分组 row_number() OVER (PARTITION BY COL1 ORDER BY COL ...

  9. Vue.js 系列教程 3:Vue

    原文:intro-to-vue-3-vue-cli-lifecycle-hooks 译者:nzbin 这是 JavaScript 框架 Vue.js 五篇教程的第三部分.在这一部分,我们将学习 Vue ...

  10. python解决组合问题

    1.问题描述 比如9个数中取4个数的组合以及列出各种组合,该如何做? 我们可以考虑以下一个简单组合:从1,2,3,4,5,6中,如何选取任意四个数的组合. 固定:1   2  3  ,组合有1234 ...