• [转]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>
     

  • 相关阅读:
    poj2528Mayor's posters(线段树加离散化)第一次接触离散化 做的挺辛苦的
    poj1789Truck History(简单最小生成树)
    【洛谷P2468】粟粟的书架
    【洛谷P3523】DYNDynamite
    【SP1716】GSS3 Can you answer these queries III
    【洛谷P3320】寻宝游戏
    【洛谷P3322】排序
    【GMOJ6293】迷宫
    【GMOJ4051】序列统计
    【洛谷P4719】【模板】动态 DP
  • 原文地址:https://www.cnblogs.com/UUUz/p/10900666.html
Copyright © 2020-2023  润新知