redis+spring配置
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配置的更多相关文章
- 征服 Redis + Jedis + Spring —— 配置&常规操作
Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spri ...
- spring boot 2.0.4 Redis缓存配置
spring boot 2 使用RedisTemplate操作redis存取对象时,需要先进行序列化操作 import org.springframework.cache.CacheManager; ...
- spring集成redis——主从配置以及哨兵监控
Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...
- springmvc+spring+jpa(hibernate)+redis+maven配置
废话不多少 项目结构 pom.xml配置例如以下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...
- spring data redis jackson 配置,工具类
spring data redis 序列化有jdk .jackson.string 等几种类型,自带的jackson不熟悉怎么使用,于是用string类型序列化,把对象先用工具类转成string,代码 ...
- Spring配置redis及使用
一.redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. ...
- 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 ...
- Spring Boot Redis 集成配置(转)
Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...
- Spring Boot 结合 Redis 序列化配置的一些问题
前言 最近在学习Spring Boot结合Redis时看了一些网上的教程,发现这些教程要么比较老,要么不知道从哪抄得,运行起来有问题.这里分享一下我最新学到的写法 默认情况下,Spring 为我们提供 ...
随机推荐
- 洛谷P2827 蚯蚓 题解
洛谷P2827 蚯蚓 题解 题目描述 本题中,我们将用符号 ⌊c⌋ 表示对 c 向下取整. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现 ...
- 洛谷 P1359 租用游艇 题解
题目传送门 这道题还是上模板啦~~SPFA大法好(本人还是懒得打邻接表) 一道橙题: 上代码: #include<bits/stdc++.h> using namespace std; ] ...
- 洛谷 P2043质因子分解 题解
题目传送门 N的范围很小,所以使用O(n2)的算法就能过啦! #include<bits/stdc++.h> using namespace std; ],n; int main(){ c ...
- AC日记——[CQOI2009]DANCE跳舞 洛谷 P3153
[CQOI2009]DANCE跳舞 思路: 二分+最大流: 代码: #include <cstdio> #include <cstring> #include <iost ...
- umask相关内容
umask权限过滤符新创建的文件的权限为666,即- rw- rw- rw-新创建的目录的权限为777,即d rwxrwxrwx如umask值为0022,后三位022,即 --- -w- -w-上述默 ...
- vuejs学习——vue+vuex+vue-router项目搭建(一)
前言 快年底了却有新公司邀请了我,所以打算把上家公司的学到一下技术做一些总结和分享. 现在vuejs都2.0了,我相信也有很多朋友和我一样实际项目还是选择vue1.0的或者给新手一些参考,不管在选择哪 ...
- LoadRunner:VuGen开发脚本步骤(一)
一.测试过程 1.规划测试:分析应用程序.定义测试目标.方案实施. 2.创建Vuser脚本. 3.创建方案:方案包括运行 Vuser 的计算机的列表.运行 Vuser 脚本的列表以及在方案执行期间运行 ...
- 异步模型(APM)的注意事项
一.在没有线程池的前提下使用APM APM可以让线程池在异步操作完成时调用指定的回调方法.它使用很少的资源,并提供了出色的性能,然而,APM 还允许通过另外三种方式发现异步操作在何时完成. 首先,如果 ...
- Python网络模块Paramiko基本使用
一.Paramiko简介 首先来看谁创造了paramiko,是一个名叫Jeff Forcier创建了paramiko项目.项目主页:http://www.paramiko.org,可以去看上面有很多相 ...
- javascript 中关于function中的prototype
在javascrpit中每个函数中都有一个prototype属性,在其创建的时候,无论是用var method = function(){}或者 var method = new Function() ...