spring-data-redis配制


1:單redis模式下

    properties文件 配制

#JedisPoolConfig的參數
#最大連接數
redis.pool.maxTotal=30
#最大空閑時間
redis.pool.maxIdle=10
#每次最大連接數
redis.pool.numTestsPerEvictionRun=1024
#釋放掃描的掃描間隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#連接的最小空閑時間
redis.pool.minEvictableIdleTimeMillis=1800000
#連接控歘按時間多久后釋放,當空閑時間>該值且空閑連接>最大空閑連接數時直接釋放
redis.pool.softMinEvictableIdleTimeMillis=10000
#獲得鏈接時的最大等待毫秒數,小於0:阻塞不確定時間,默認-1
redis.pool.maxWaitMillis=1500
#在獲得鏈接的時候檢查有效性,默認false
redis.pool.testOnBorrow=true
#在空閑時檢查有效性,默認false
redis.pool.testWhileIdle=true
#連接耗盡時是否阻塞,false報異常,true阻塞超時,默認true
redis.pool.blockWhenExhausted=false

#JedisConnectionFactory的參數
#主機地址,默認:localhost
redis.hostName=192.168.200.128
#主機端口,默認:6379
redis.port=6379
#超時時間,默認:2000
redis.timeout=3000
#密碼
#redis.password
#是否使用連接池,默認true
redis.usePool=true
#使用數據庫的索引,0-15之間的數字,默認:0
redis.dbIndex=0
#是否使用數據類型的轉換,默認:true
#redis.convertPipelineAndTxResults
#哨兵配置
#redis.sentinelConfig
#集群配置
#redis.clusterConfig
<?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">


    <!--引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>
<!--配置 jedis pool-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大連接數 -->
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <!-- 最大空閑時間 -->
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <!-- 每次最大連接數 -->
        <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
        <!-- 釋放掃描的掃描間隔 -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
        <!-- 連接的最小空閑時間 -->
        <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
        <!-- 連接控歘按時間多久后釋放,當空閑時間>該值且空閑連接>最大空閑連接數時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
        <!-- 獲得鏈接時的最大等待毫秒數,小於0:阻塞不確定時間,默認-1 -->
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
        <!-- 在獲得鏈接的時候檢查有效性,默認false -->
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <!-- 在空閑時檢查有效性,默認false -->
        <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
        <!-- 連接耗盡時是否阻塞,false報異常,true阻塞超時 默認:true-->
        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
    </bean>
<!--spring data redis -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostName}"/>
        <property name="port" value="${redis.port}"/>
        <property name="timeout" value="${redis.timeout}"/>
        <property name="database" value="${redis.dbIndex}"/>
        <property name="usePool" value="${redis.usePool}"/>
        <!--可以通過構造注入或者Set注入兩種方式-->
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

    <!--redisTemplate-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>

</beans>

2:集群模式下 

 properties文件 配制

#JedisPoolConfig的參數
#最大連接數
redis.pool.maxTotal=30
#最大空閑時間
redis.pool.maxIdle=10
#每次最大連接數
redis.pool.numTestsPerEvictionRun=1024
#釋放掃描的掃描間隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#連接的最小空閑時間
redis.pool.minEvictableIdleTimeMillis=1800000
#連接控歘按時間多久后釋放,當空閑時間>該值且空閑連接>最大空閑連接數時直接釋放
redis.pool.softMinEvictableIdleTimeMillis=10000
#獲得鏈接時的最大等待毫秒數,小於0:阻塞不確定時間,默認-1
redis.pool.maxWaitMillis=1500
#在獲得鏈接的時候檢查有效性,默認false
redis.pool.testOnBorrow=true
#在空閑時檢查有效性,默認false
redis.pool.testWhileIdle=true
#連接耗盡時是否阻塞,false報異常,true阻塞超時,默認true
redis.pool.blockWhenExhausted=false
#RedisClusterConfiguration配置
redis.maxRedirects=5
#主機和端口號
redis.host1=192.168.200.128
redis.port1=7000
redis.host2=192.168.200.128
redis.port2=7001
redis.host3=192.168.200.128
redis.port3=7002
redis.host4=192.168.200.128
redis.port4=7003
redis.host5=192.168.200.128
redis.port5=7004
redis.host6=192.168.200.128
redis.port6=7005
<?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">


    <!--引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:redis-cluster.properties</value>
            </list>
        </property>
    </bean>
<!--配置 jedis pool-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大連接數 -->
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <!-- 最大空閑時間 -->
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <!-- 每次最大連接數 -->
        <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
        <!-- 釋放掃描的掃描間隔 -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
        <!-- 連接的最小空閑時間 -->
        <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
        <!-- 連接控歘按時間多久后釋放,當空閑時間>該值且空閑連接>最大空閑連接數時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
        <!-- 獲得鏈接時的最大等待毫秒數,小於0:阻塞不確定時間,默認-1 -->
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
        <!-- 在獲得鏈接的時候檢查有效性,默認false -->
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <!-- 在空閑時檢查有效性,默認false -->
        <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
        <!-- 連接耗盡時是否阻塞,false報異常,true阻塞超時 默認:true-->
        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
    </bean>
<!--配置RedisClusterConfiguration-->
    <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.RedisNode">
                    <constructor-arg name="host" value="${redis.host1}"/>
                    <constructor-arg name="port" value="${redis.port1}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host2}"/>
                    <constructor-arg name="port" value="${redis.port2}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host3}"/>
                    <constructor-arg name="port" value="${redis.port3}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host4}"/>
                    <constructor-arg name="port" value="${redis.port4}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host5}"/>
                    <constructor-arg name="port" value="${redis.port5}"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.host6}"/>
                    <constructor-arg name="port" value="${redis.port6}"/>
                </bean>
            </set>
        </property>
    </bean>
<!--配置JedisConnectionFactory-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
    </bean>

    <!--redisTemplate-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>

</beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM