注意事项: jedis2.9 以下不能使用密码;
配置过程:
添加jedis2.9以后发现 spring-data-redis 也需要升级,之后下载了spring-data-redis1.8.1.jar
1、升级spring4.3.6
升级spring 4.3.6 所需要的包
升级spring4.3.6后jackson也需要升级!
修改spring-mvc配置文件
MappingJacksonHttpMessageConverter 改为 MappingJackson2HttpMessageConverter
MappingJacksonJsonView 改为 MappingJackson2JsonView
升级高版本后
上图中的两个取消了set方法,需要找其他方式代替
2、配置redis集群
配置redis参数
#ip\u5730\u5740\u7aef\u53e3\u53f7 redis.host1=172.16.2.4 redis.host2=172.16.2.9 redis.host3=172.16.2.10 redis.host4=172.16.2.13 redis.host5=172.16.2.12 redis.host6=172.16.2.15 redis.port1=10011 redis.port2=10011 redis.port3=10011 redis.port4=10011 redis.port5=10011 redis.port6=10011 redis.password=hzs@redis redis.expiration=3000 #\u6700\u5927\u7a7a\u95f2\u6570 redis.maxIdle=300 #\u8fde\u63a5\u6c60\u7684\u6700\u5927\u6570\u636e\u5e93\u8fde\u63a5\u6570\u3002\u8bbe\u4e3a0\u8868\u793a\u65e0\u9650\u5236,\u5982\u679c\u662fjedis 2.4\u4ee5\u540e\u7528redis.maxTotal redis.maxActive=600 #\u6700\u5927\u5efa\u7acb\u8fde\u63a5\u7b49\u5f85\u65f6\u95f4 redis.maxWait=1000 #\u662f\u5426\u5728\u4ece\u6c60\u4e2d\u53d6\u51fa\u8fde\u63a5\u524d\u8fdb\u884c\u68c0\u9a8c,\u5982\u679c\u68c0\u9a8c\u5931\u8d25,\u5219\u4ece\u6c60\u4e2d\u53bb\u9664\u8fde\u63a5\u5e76\u5c1d\u8bd5\u53d6\u51fa\u53e6\u4e00\u4e2a redis.testOnBorrow=true #\u5ba2\u6237\u7aef\u8d85\u65f6\u65f6\u95f4\u5355\u4f4d\u662f\u6beb\u79d2 \u9ed8\u8ba4\u662f2000 redis.timeout=10000 #\u63a7\u5236\u4e00\u4e2apool\u53ef\u5206\u914d\u591a\u5c11\u4e2ajedis\u5b9e\u4f8b,\u7528\u6765\u66ff\u6362\u4e0a\u9762\u7684redis.maxActive,\u5982\u679c\u662fjedis 2.4\u4ee5\u540e\u7528\u8be5\u5c5e\u6027 redis.maxTotal=1000 #\u6700\u5927\u5efa\u7acb\u8fde\u63a5\u7b49\u5f85\u65f6\u95f4\u3002\u5982\u679c\u8d85\u8fc7\u6b64\u65f6\u95f4\u5c06\u63a5\u5230\u5f02\u5e38\u3002\u8bbe\u4e3a-1\u8868\u793a\u65e0\u9650\u5236\u3002 redis.maxWaitMillis=1000 #\u8fde\u63a5\u7684\u6700\u5c0f\u7a7a\u95f2\u65f6\u95f4 \u9ed8\u8ba41800000\u6beb\u79d2(30\u5206\u949f) redis.minEvictableIdleTimeMillis=300000 #\u6bcf\u6b21\u91ca\u653e\u8fde\u63a5\u7684\u6700\u5927\u6570\u76ee,\u9ed8\u8ba43 redis.numTestsPerEvictionRun=1024 #\u9010\u51fa\u626b\u63cf\u7684\u65f6\u95f4\u95f4\u9694(\u6beb\u79d2) \u5982\u679c\u4e3a\u8d1f\u6570,\u5219\u4e0d\u8fd0\u884c\u9010\u51fa\u7ebf\u7a0b, \u9ed8\u8ba4-1 redis.timeBetweenEvictionRunsMillis=30000 #\u5728\u7a7a\u95f2\u65f6\u68c0\u67e5\u6709\u6548\u6027, \u9ed8\u8ba4false redis.testWhileIdle=true
配置JedisPoolConfig
<!-- 配置JedisPoolConfig实例 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--连接池的最大数据库连接数 -->
<property name="maxTotal" value="${redis.maxActive}" />
<!--最大建立连接等待时间-->
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
<!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
<!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
</bean>
配置RedisClusterConfiguration
<!-- Redis集群配置 --> <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="6"></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>
<!-- 配置JedisConnectionFactory --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig"/> <constructor-arg name="clusterConfig" ref="redisClusterConfig" /> <property name="usePool" value="true"/> <property name="password" value="${redis.password}"></property> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <!-- redisTemplate模板 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> <!-- 配置RedisCacheManager --> <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg name="redisOperations" ref="redisTemplate" /> </bean>