Redis的Spring配置

文章目录

  • 一、redis.properties
  • 二、redis单机版
  • 三、redis集群版


一、redis.properties

#redis集群数量
redis.maxRedirects=3
#redis集群ip
redis.host1=127.0.0.1
redis.host2=127.0.0.2
redis.host3=127.0.0.3

#host
redis.host=localhost
#访问端口
redis.port=6379
#redis密码
redis.password=123
#连接池的最大数据库连接数。设为0表示无限制
redis.maxTotal=1024
#最大空闲数,超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。  
redis.maxIdle=100
#最小空闲数,超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。  
redis.minIdle=1
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。  
redis.maxWaitMillis=5000
#在获取连接的时候检查有效性, 默认false 数据量大的时候建议关闭
redis.testOnBorrow=true
redis.testOnReturn=true

二、redis单机版


<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxTotal" value="${redis.maxTotal}" />
	<property name="maxIdle" value="${redis.maxIdle}" />
	<property name="minIdle" value="${redis.minIdle}" />
	<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
	<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	<property name="testOnReturn" value="${redis.testOnReturn}" />
bean>


<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
	<property name="hostName" value="${redis.host}">property>
	<property name="port" value="${redis.port}">property>
	<property name="password" value="${redis.password}">property>
	<property name="poolConfig" ref="jedisConfig">property>
bean>


<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="enableTransactionSupport" value="false" />
bean>

三、redis集群版

  • spring-redis.xml 集群配置,用 spring-data-redis 包做集成
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" />


<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
    <constructor-arg ref="redisClusterConfiguration"/>
    <constructor-arg ref="jedisPoolConfig"/>
    <property name="password" value="${redis.password}">property>
bean>


<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="${redis.maxIdle}"/>
    <property name="minIdle" value="${redis.minIdle}"/>
    <property name="maxTotal" value="${redis.maxTotal}"/>
    <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
    <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    <property name="testOnReturn" value="${redis.testOnReturn}"/>
bean>


<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
    <property name="maxRedirects" value="${redis.maxRedirects}">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.port}">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.port}">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.port}">constructor-arg>
            bean>
        set>
    property>
bean>



<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="enableTransactionSupport" value="false" />
bean>


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

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