1、添加maven的pom.xml依赖
需要注意导入依赖的版本一定要匹配,像下面这样。1.8.14对应2.9.0的。如不知道自己的版本相匹配的那个版本,可到maven官网去查看 https://mvnrepository.com/
如图,找到Compile Dependencies 下jedis2.9.0的版本,说明spring-data-redis2.1.0对应jedis2.9.0的版本
pom.xml中添加
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.14.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、配置redis-cluster.xml文件
写好redis.properties
#连接配置
redis.adapter.master=mymaster
redis.adapter.address=ip1:port1,ip2:port2,ip3:port3
redis.adapter.password=password
#最大能够保持idle的数量,控制一个pool最多有多少个状态为idle的jedis实例,默认值8
redis.adapter.maxIdle=100
#在指定时刻通过pool能够获取到的最大的连接的jedis个数,默认值8
redis.adapter.maxTotal=200
#连接最小空闲时间
redis.adapter.minEvictableIdleTimeMillis=200000
#每次释放连接的最大数目
redis.adapter.numTestsPerEvictionRun=1024
# 在获取连接的时候检查有效性, 默认false 数据量大的时候建议关闭
redis.adapter.testOnBorrow=true
#获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
redis.adapter.maxWaitMillis=1500
#在空闲时检查有效性, 默认false
redis.adapter.testWhileIdle=true
redis-cluster.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="${redis.adapter.master}"/>
<constructor-arg name="sentinelHostAndPorts" value="${redis.adapter.address}"/>
</bean>
<bean id="pool" class="redis.clients.jedis.JedisPoolConfig">
<!--最大能够保持idle的数量,控制一个pool最多有多少个状态为idle的jedis实例,默认值8-->
<property name="maxIdle" value="${redis.adapter.maxIdle}"/>
<!--在指定时刻通过pool能够获取到的最大的连接的jedis个数,默认值8-->
<property name="maxTotal" value="${redis.adapter.maxTotal}"/>
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="${redis.adapter.minEvictableIdleTimeMillis}"/>
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="${redis.adapter.numTestsPerEvictionRun}"/>
<!-- 在获取连接的时候检查有效性, 默认false 数据量大的时候建议关闭-->
<property name="testOnBorrow" value="${redis.adapter.testOnBorrow}"/>
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="${redis.adapter.maxWaitMillis}"/>
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="${redis.adapter.testWhileIdle}"/>
</bean>
<!--注入配置 JedisConnectionFactory 和 StringRedisTemplate -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/>
<constructor-arg name="poolConfig" ref="pool"/>
<property name="password" value="${redis.adapter.password}"/>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
spring-config.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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 引入属性配置文件 -->
<bean class="org.ice.jee.spring.common.core.util.PropertiesUtil">
<property name="locations">
<list>
<value>classpath:/properties/redis.properties</value>
</list>
</property>
<!-- 引入SPRING配置文件 -->
<import resource="classpath:redis-cluster.xml"/>
</beans>
3、测试效果
package com.test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
public class Test {
private final static Logger log = LogManager.getLogger(Test .class);
@Autowired
private StringRedisTemplate redisTemplate;
public void testRedis() {
// set key
redisTemplate.opsForValue().set("keyname", "value", "100000", TimeUnit.MILLISECONDS);
// get key
log.info("redis,key:" + redisTemplate.opsForValue().get("keyname") );
}
}
结果成功输出。使用传统的spring项目整合redis集群就完成了。
spring boot整合redis更简单,可进入博主主页查看博主下一篇博文。
如有问题可以加上博主qq:244156219,微信:zuo995518,可以拉你进技术交流群,大家一起进步。