標籤:時間 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
並且廢棄了JedisConnectionFactory
的HostName
,Port
,Password
等屬性的配置。
/** * 串連工廠類 * @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入門樣本:程式配置(五)