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. ASCII-->Ansi-->Unicode-->UTF8 关于编码 自己的总结

    各种不同的编码 无非就是效率 最大化. 我猜测编码的进化流程: ASCII(American Standard Code for Information Interchange)----满足了美国和西 ...

  2. HDU 1686 Oulipo(KMP变形求子串出现数目(可重))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...

  3. java.lang.reflect.UndeclaredThrowableExceptionjiang

    实例包含由调用处理程序抛出的经过检查的未声明异常,可以使用 getUndeclaredThrowable() 方法获取 String msg = null; if (e instanceof Unde ...

  4. 关于HTML5 boilerplate 的一些笔记

    最近在研究HTML5 boilerplate的模版,以此为线索可以有条理地学习一些前端的best practice,好过在W3C的文档汪洋里大海捞针……啊哈哈哈…… 开头的IE探测与no-js类是什么 ...

  5. css3 box-sizing属性值详解

    box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入widt ...

  6. day3 作业

    文件操作用户很广泛,我们经常对文件进行操作: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults ...

  7. Robot Framework自动化测试的应用

    Robot Framework自动化测试的应用(一) 最近尝试用Robot Framework代替之前全部采用python实现测试case,开始对Robot Framework进行些了解学习. 1. ...

  8. spectre漏洞代码分析-c代码

    下面一句话转自360: 现代处理器(CPU)的运作机制中存在两个用于加速执行的特性,推测执行( Speculative Execution)和间接分支预测(Indirect Branch Predic ...

  9. TextView部分文字可点击跳转

    效果图: 需求:每个item的文字都有两部分是连接可点击 当然需要用到SpannableString和ClickableSpan. import android.text.TextPaint; imp ...

  10. Python并发编程-进程间数据共享

    Manager中进程数据不安全 通过加锁解决 from multiprocessing import Manager,Process,Lock def main(dic,lock): lock.acq ...