Spring Data Redis入門樣本:程式配置(五)

來源:互聯網
上載者:User

標籤:時間   shard   屬性   work   otf   xml配置   高可用   cluster   mod   

單機配置

redis.properties配置

#redis的伺服器位址redis.host=127.0.0.1#redis的服務連接埠redis.port=6379#用戶端逾時時間單位是毫秒redis.timeout=100000#最大建立串連等待時間redis.maxWaitMillis=1000#最小空閑數redis.minIdle=5#最大空閑數redis.maxIdle=20#最大串連數redis.maxTotal=100

xml配置

<!-- 讀取properties檔案 --><bean id="propertyConfigurerForProject1"      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="order" value="1" />  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />  <property name="ignoreResourceNotFound" value="true" />  <property name="ignoreUnresolvablePlaceholders" value="true" />  <property name="locations">    <list>      <value>classpath*:redis.properties</value>    </list>  </property></bean><!-- Jedis串連池的設定物件 --><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="testOnBorrow" value="true" />    <!--最大建立串連等待時間 -->    <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /></bean><!--jedis伺服器資訊 --><bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">    <constructor-arg index="0" value="${redis.host}" />    <constructor-arg index="1" value="${redis.port}" type="int" />    <constructor-arg index="2" value="${redis.timeout}"        type="int" /></bean><!--jedis串連池 --><bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">    <constructor-arg index="0" ref="jedisPoolConfig" />    <constructor-arg index="1">        <list>            <ref bean="jedisShardInfo" />        </list>    </constructor-arg></bean><!-- Redis串連工廠 --><bean id="jedisConnectionFactory"    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <property name="shardInfo" ref="jedisShardInfo" />    <property name="poolConfig" ref="jedisPoolConfig" /></bean><!-- 緩衝序列化方式 --><!--對key的預設序列化器。預設值是StringSerializer --><bean id="keySerializer"    class="org.springframework.data.redis.serializer.StringRedisSerializer" /><!--是對value的預設序列化器,預設值是取自DefaultSerializer的JdkSerializationRedisSerializer。 --><bean id="valueSerializer"    class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /><!-- redis操作模板,對Jedis進行的通用API操作 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">    <property name="connectionFactory" ref="jedisConnectionFactory" />    <property name="keySerializer" ref="keySerializer" />    <property name="valueSerializer" ref="valueSerializer" />    <property name="hashKeySerializer" ref="keySerializer" />    <property name="hashValueSerializer" ref="valueSerializer" /></bean>

自2.0開始,針對單機配置引入RedisStandaloneConfiguration 並且廢棄了JedisConnectionFactoryHostNamePortPassword等屬性的配置。

    /** * 串連工廠類 * @return */@Beanpublic JedisConnectionFactory jedisConnectionFactory(){    // 單機配置    RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();    standaloneConfig.setHostName("127.0.01");    standaloneConfig.setPort(6379);        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(standaloneConfig);        return connectionFactory;}
哨兵配置

為了處理高可用性的Redis,可以使用RedisSentinelConfiguration支援Redis Sentinel

/*** jedis*/@Beanpublic RedisConnectionFactory jedisConnectionFactory() {    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()      .master("mymaster")      .sentinel("127.0.0.1", 26379)      .sentinel("127.0.0.1", 26380);    return new JedisConnectionFactory(sentinelConfig);}

可以配置的屬性:
spring.redis.sentinel.master:master節點的名字
spring.redis.sentinel.nodes:主機:連接埠的逗號分隔列表

叢集配置
 @Beanpublic JedisConnectionFactory redisConnectionFactory() {   JedisConnectionFactory connectionFactory = new JedisConnectionFactory();   // 叢集配置   RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();   RedisNode redisNode7000 = new RedisNode("10.1.21.4", 7000);   RedisNode redisNode7001 = new RedisNode("10.1.21.4", 7001);   RedisNode redisNode7002 = new RedisNode("10.1.21.4", 7002);   RedisNode redisNode7003 = new RedisNode("10.1.21.4", 7003);   RedisNode redisNode7004 = new RedisNode("10.1.21.4", 7004);   RedisNode redisNode7005 = new RedisNode("10.1.21.4", 7005);   redisClusterConfiguration.addClusterNode(redisNode7000);   redisClusterConfiguration.addClusterNode(redisNode7001);   redisClusterConfiguration.addClusterNode(redisNode7002);   redisClusterConfiguration.addClusterNode(redisNode7003);   redisClusterConfiguration.addClusterNode(redisNode7004);   redisClusterConfiguration.addClusterNode(redisNode7005);   connectionFactory = new JedisConnectionFactory(redisClusterConfiguration);   // 設定串連池配置   connectionFactory.setPoolConfig(jedisConfig());   // 設定資料庫,預設0   connectionFactory.setDatabase(2);   // 設定使用串連池   connectionFactory.setUsePool(true);   return connectionFactory;}
配置說明
1. 以上配置是基於Jedis用戶端的2. 使用javaCode或者xml的配置方式依據個人喜好

Spring Data Redis入門樣本:程式配置(五)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.