spring redis整合非常简单简单,本教程案例采用了redis集群的方式整合spring框架,项目采用maven加入redis jar包,是ssm框架的项目,ssm框架整合这里就不说了,废话不多说,开始吧!
1:在项目的pom.xml文件中加入redis客户端jar包,代码如下。
<!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.0</version> </dependency>
2:在src/main/resources下创建redis.properties的配置文件,例如使用两个redis集群,代码如下。
redis.maxTotal = 50 redis.node1.ip1 = 192.168.0.1 redis.node1.port1 = 6379 redis.node2.ip2 = 192.168.0.2 redis.node2.port2 = 6379
3:在applicationContext.xml中加载redis.properties的配置,跟加载mysql.properties配置文件是一样的,在以下这个位置中添加。这个不用我说吧。
<!-- 使用spring自带的占位符替换功能 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 允许JVM参数覆盖 --> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <!-- 忽略没有找到的资源文件 --> <property name="ignoreResourceNotFound" value="true" /> <!-- 配置资源文件 --> <property name="locations"> <list> <value>classpath:mysql.properties</value> <value>classpath:redis.properties</value> </list> </property> </bean>
4:在src/main/resources下创建applicationContext-redis.xml文件,用于配置redis连接池,redis集群配置等,便于以后@Autowired注入,代码如下。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 构建连接池配置信息 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"
destroy-method="close">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.node1.ip1}" />
<constructor-arg index="1" value="${redis.node1.port1}" />
</bean>
<bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.node2.ip2}" />
<constructor-arg index="1" value="${redis.node2.port2}" />
</bean>
</list>
</constructor-arg>
</bean>
</beans>注意:下面代码不在项目中,只是为了让你知道,如果你不理解上面的redis配置代码,可以对照下面redis集群的java类看看,以便你对上面的redis spring配置的由来有所了解。
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* 集群式的连接池
*
*/
public class ShardedJedisPoolDemo {
public static void main(String[] args) {
// 构建连接池配置信息
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 设置最大连接数
poolConfig.setMaxTotal(50);
// 定义集群信息
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("192.168.0.1", 6379));
shards.add(new JedisShardInfo("192.168.0.2", 6379));
// 定义集群连接池
ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, shards);
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
// 从redis中获取数据
String value = shardedJedis.get("mytest");
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
// 关闭连接池
shardedJedisPool.close();
}
}5:在service层中准备一个redis往redis写入数据和删除数据的方法,以便在用到的地方@Autowired注入进去使用,由于重复代码太多,这里自定义了一个Function.java接口类用于封装重复的数据。
//E传入的对象,T代表返回的对象类型
public interface Function<E,T> {
public T callback(E e);
}然后在RedisService.java封装我们的service增删redis数据的方法。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
public class RedisService {
//required=false当要用到时,才注入
@Autowired(required=false)
private ShardedJedisPool shardedJedisPool;
//required=false当要用到时,才注入
@Autowired(required=false)
private JedisPoolConfig jedisPoolConfig;
public <T> T execute(Function<ShardedJedis, T> fun){
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
// 从redis中获取数据
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}
/**
* set操作
* @param key
* @param value
* @return
*/
public String set(final String key,final String value){
return this.execute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**
* get操作
* @param key
* @return
*/
public String get(final String key){
return this.execute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 设置某个key的生存时间
* @param key
* @param seconds 生存时间
* @return
*/
public Long expire(final String key,final Integer seconds){
return this.execute(new Function<ShardedJedis, Long>() {
@Override
public Long callback(ShardedJedis e) {
return e.expire(key, seconds);
}
});
}
/**
* 执行del,删除一个键
*/
public Long del(final String key){
return this.execute(new Function<ShardedJedis, Long>() {
@Override
public Long callback(ShardedJedis e) {
// TODO Auto-generated method stub
return e.del(key);
}
});
}
/**
* 执行set key和value值,并设置保存时间
*/
public String set(final String key,final String value,final Integer seconds){
return this.execute(new Function<ShardedJedis, String>() {
@Override
public String callback(ShardedJedis e) {
String str = e.set(key, value);
expire(key, seconds);
return str;
}
});
}
}6:在需要使用redis缓存的地方注入我们定义好的RedisService类,代码如下。
@Autowired private RedisService redisService