关于 spring redis 集群的两个错误
1. Jedis does not support password protected Redis Cluster configurations!
spring-data-redis 1.8版本以下是不支持jedis 集群
2. spring-data-redis 换成1.8 以后报错org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
这是spring-core 版本太低造成的,换成4.3以上既可以,我用的是4.3.6
本人spring 操作redis 集群的配置,做记录
<properties> <org.springframework.version>4.3.6.RELEASE</org.springframework.version> </properties> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <scope>test</scope> </dependency> <dependency><!-- JUnit单元测试框架 --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.0.RELEASE</version> <exclusions> <exclusion> <artifactId>jcl-over-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
redis 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <description>redis数据源</description> <!-- redis数据源 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲数 --> <property name="maxIdle" value="${redis.maxIdle}" /> <!-- 最大空连接数 --> <property name="maxTotal" value="${redis.maxTotal}" /> <!-- 最大等待时间 --> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <!-- 连接超时时是否阻塞,false时报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" /> <!-- 返回连接时,检测连接是否成功 --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource"> <constructor-arg name="name" value="redis.properties"/> <constructor-arg name="resource" value="classpath:redis.properties"/> </bean> <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <constructor-arg name="propertySource" ref="resourcePropertySource"/> </bean> <!-- Spring-redis连接池管理工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="clusterConfig" ref="redisClusterConfig"/> <!-- IP地址 --> <property name="hostName" value="${redis.host}" /> <!-- 端口号 --> <property name="port" value="${redis.port}" /> <property name="password" value="${redis.password}" /> <!-- 超时时间 默认2000--> <property name="timeout" value="${redis.timeout}" /> <!-- 连接池配置引用 --> <property name="poolConfig" ref="poolConfig" /> <!-- usePool:是否使用连接池 --> <property name="usePool" value="true"/> </bean> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <!-- 开启事务 --> <!-- <property name="enableTransactionSupport" value="true"/> --> </bean> <!--自定义redis工具类,在需要缓存的地方注入此类 --> <bean id="redisService" class="com.shdy.utils.RedisService"> <property name="redisTemplate" ref="redisTemplate" /> </bean> </beans>
redis.properties
#redis配置
redis.host=
redis.port=
redis.password=
redis.maxIdle=400
redis.maxTotal=6000
redis.maxWaitMillis=1000
redis.blockWhenExhausted=true
redis.testOnBorrow=true
redis.timeout=100000
defaultCacheExpireTime=60
spring.redis.cluster.nodes=ip:port,ip:port
spring.redis.cluster.max-redirects=2
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; public class RedisService { private static final Logger logger = LoggerFactory.getLogger(RedisService.class); private RedisTemplate redisTemplate; /** * 指定缓存失效时间*/ private boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { logger.error("redis err:",e); return false; } }/** * 判断key是否存在*/ public boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { logger.error("redis err:",e); return false; } } /** * 删除缓存*/ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete(CollectionUtils.arrayToList(key)); } } } /** * 普通缓存获取*/ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } @SuppressWarnings("unchecked") public <T> T get(String key, Class<T> cls) { return key == null ? null : (T) redisTemplate.opsForValue().get(key); } /** * 普通缓存放入*/ public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { logger.error("redis err:",e); return false; } } /** * 普通缓存放入并设置时间*/ public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { logger.error("redis err:",e); return false; } } /** * 获取某个key的锁 */ public boolean lock(String key){ boolean lock = redisTemplate.opsForValue().setIfAbsent(key,key); if(lock){ redisTemplate.expire(key,60,TimeUnit.SECONDS); } return lock; } /** * 删除锁 */ public void unLock(String key){ redisTemplate.delete(key); } public RedisTemplate getRedisTemplate() { return redisTemplate; } public void setRedisTemplate(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } }