• [转]Spring Redis 配置


    单Redis,Sentinel 哨兵模式,Cluster集群,Sharding集群
    1、单机版 不解释  配置如下:

    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties" />

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="300" /> <!-- 最大能够保持idel状态的对象数 -->
    <property name="maxTotal" value="60000" /> <!-- 最大分配的对象数 -->
    <property name="testOnBorrow" value="true" /> <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="jedisPoolConfig" />
    <constructor-arg index="1" value="${redis.host}" />
    <constructor-arg index="2" value="${redis.port}" type="int" />
    <constructor-arg index="3" value="${redis.timeout}"/>
    <constructor-arg index="4" value="${redis.password}"/>
    </bean>
    2、Sentinel 哨兵模式 主从模式,一主多从模式

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="${redis.maxTotal}" />
    <property name="minIdle" value="${redis.minIdle}" />
    <property name="maxWaitMillis" value="${redis.maxWaitTime}" />
    <property name="maxIdle" value="${redis.maxIdle}" />
    <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    <property name="testOnReturn" value="true" />
    <property name="testWhileIdle" value="true" />
    </bean>
    <bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
    <constructor-arg index="0" value="mymaster" />
    <constructor-arg index="1">
    <set>
    <value>${redis.sentinel1.host}:${redis.sentinel1.port}</value> <!-- 该节点为哨兵的端口 -->
    <value>${redis.sentinel2.host}:${redis.sentinel2.port}</value>
    </set>
    </constructor-arg>
    <constructor-arg index="2" ref="poolConfig" />
    <constructor-arg index="3" value="${redis.password}" />
    </bean>
    3、Redis Cluster, Redis官方集群方案 

    <!-- Jedis链接池配置,注意:Jedis版本建议升级到最新 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxTotal" value="100" />
    <property name="maxIdle" value="20" />
    <property name="minIdle" value="10" />
    <property name="blockWhenExhausted" value="true"></property>
    <property name="maxWaitMillis" value="3000" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" />
    <property name="minEvictableIdleTimeMillis" value="60000" />
    <property name="timeBetweenEvictionRunsMillis" value="30000" />
    <property name="numTestsPerEvictionRun" value="-1" />
    </bean>

    <!-- JedisCluster -->
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
    <constructor-arg index="0">
    <set>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.111" />
    <constructor-arg index="1" value="7111" type="int" />
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.112" />
    <constructor-arg index="1" value="7112" type="int" />
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.113" />
    <constructor-arg index="1" value="7113" type="int" />
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.114" />
    <constructor-arg index="1" value="7114" type="int" />
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.115" />
    <constructor-arg index="1" value="7115" type="int" />
    </bean>
    <bean class="redis.clients.jedis.HostAndPort">
    <constructor-arg index="0" value="192.168.1.116" />
    <constructor-arg index="1" value="7116" type="int" />
    </bean>
    </set>
    </constructor-arg>
    <constructor-arg index="1" value="2000" type="int"></constructor-arg>
    <constructor-arg index="2" value="100" type="int"></constructor-arg>
    <constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>
    </bean>
    4、Redis Sharding集群

    <!-- jedis 连接池配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxActive" value="${redis.pool.maxActive}" />
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
    <property name="maxWait" value="${redis.pool.maxWait}" />
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <!-- jedis 多个服务器配置-->
    <bean id="jedisShardInfo1" class="redis.clients.jedis.JedisShardInfo">
    <constructor-arg index="0" value="${redis2.ip}" />
    <constructor-arg index="1" value="${redis.port}" type="int" />
    <property value="${redis.password}" name="password"/>
    </bean>

    <bean id="jedisShardInfo2" class="redis.clients.jedis.JedisShardInfo">
    <constructor-arg index="0" value="${redis.ip}" />
    <constructor-arg index="1" value="${redis.port}" type="int" />
    <property value="${redis.password}" name="password"/>
    </bean>

    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
    <constructor-arg index="0" ref="jedisPoolConfig" />
    <constructor-arg index="1">
    <list>
    <ref bean="jedisShardInfo1" />
    <ref bean="jedisShardInfo2"/>
    </list>
    </constructor-arg>
    </bean>
     

  • 相关阅读:
    python基础知识(集合)
    python基础知识(字典)
    python基础知识(循环语句)
    Python基础知识(程序结构)
    python基础知识(正则表达式)
    python基础知识(字符串)
    jmeer_09_JDBC Request
    性能总结_01
    性能02_
    jmeter_07正则提取器_JDBC_Connection_Configuration
  • 原文地址:https://www.cnblogs.com/UUUz/p/10900666.html
Copyright © 2020-2023  润新知