spring项目(SSM/SSH)整合redis集群

前几天接手一个ssm工程需要使用redis集群,配置的过程中踩过几个坑,最终整合成功了,所以将这次配置redis集群的经验分享出来,如果你也在这掉坑了也希望能够帮你从这个坑中拉出来;

pom文件中添加所需要的依赖:

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.8.9.RELEASE</version>
			<!--去除Lettuce-->
			<exclusions>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- Redis 连接工具 -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.1</version>
		</dependency>

该工程使用的spring版本是 4.2.5.RELEASE

application.properties文件中添加redis的配置:

#Redis database settings
#集群模式下,集群最大转发的数量
redis.maxRedirects=3
#server IP
redis.host1=172.25.5.54
redis.port1=6310
redis.host2=172.25.5.54
redis.port2=6311
redis.host3=172.25.5.54
redis.port3=6312
#server pass
redis.pass=
#use dbIndex
redis.database=0
#连接池中的最大空闲连接
redis.maxIdle=8
#连接池中的最小空闲连接
redis.minIdle=0
#连接池最大阻塞等待时间(使用负值表示没有限制)
redis.maxWaitMillis=-1
#连接池最大连接数(使用负值表示没有限制)
redis.maxTotal=50
#在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true

spring-context-redis.xml文件中添加redis的连接配置:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache.xsd">

    
    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration" >

        <property name="maxRedirects" value="${redis.maxRedirects}" />
        <property name="clusterNodes">
            <set>
                <bean id="node1" class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host1}"/>
                    <constructor-arg name="port" value="${redis.port1}"/>
                bean>
                <bean id="node2" class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host2}"/>
                    <constructor-arg name="port" value="${redis.port2}"/>
                bean>
                <bean id="node3" class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host3}"/>
                    <constructor-arg name="port" value="${redis.port3}"/>
                bean>
            set>
        property>
    bean>
    
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxTotal" value="${redis.maxTotal}"/>
        
        <property name="maxIdle" value="${redis.maxWait}"/>
        
        <property name="minIdle" value="${redis.minIdle}"/>
        
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>

		 
        <property name="blockWhenExhausted" value="false"/>
        <property name="numTestsPerEvictionRun" value="1024"/>
        <property name="testOnBorrow" value="true"/>
        <property name="softMinEvictableIdleTimeMillis" value="10000"/>
        <property name="timeBetweenEvictionRunsMillis" value="30000"/>
        <property name="testWhileIdle" value="true"/>
    bean>
    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
    bean>
    
    
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
    
    <bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
        <property name="hashKeySerializer" ref="genericJackson2JsonRedisSerializer"/>
        <property name="hashValueSerializer" ref="jdkSerializationRedisSerializer"/>
    bean>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
    bean>

beans>  

需要使用的话直接在类中注入RedisTemplate就可以
在这里插入图片描述

以上版本和配置,亲测可用!!!

你可能感兴趣的:(Redis,java,redis,jedis)