Spring-boot配置JedisShardInfo

 2023-09-05 阅读 74 评论 0

摘要:2019独角兽企业重金招聘Python工程师标准>>> 配置类: @Configuration public class RedisConfig {@Autowiredprivate Environment env;@Bean@ConfigurationProperties(prefix = "spring.redis.pool")public JedisPoolConfig getJedisPo

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

配置类:

@Configuration
public class RedisConfig {@Autowiredprivate Environment env;@Bean@ConfigurationProperties(prefix = "spring.redis.pool")public JedisPoolConfig getJedisPoolConfig() {return new JedisPoolConfig();}@Beanpublic ShardedJedisPool getJedisPool() {try {List<JedisShardInfo> shardList = new ArrayList<>();int index = 1;while(true){//读取hostString host = env.getProperty("spring.redis.shard."+index+".host");if(StringUtils.isEmpty(host)){break;}//读取portString port = env.getProperty("spring.redis.shard."+index+".port");JedisShardInfo info = new JedisShardInfo(host, Integer.valueOf(port), 0, "");//读取passwordString password = env.getProperty("spring.redis.shard."+index+".password");if(!StringUtils.isEmpty(password)){info.setPassword(password);}shardList.add(info);index++;}if(shardList.size() == 0){//无法加载redisthrow new IOException();}return new ShardedJedisPool(getJedisPoolConfig(), shardList);} catch (Exception e) {throw new RuntimeException("无法加载资源文件!");}}}

Properties信息如下 : 


# Redis config
spring.redis.shard.1.host = 127.0.0.1
spring.redis.shard.1.password = 
spring.redis.shard.1.port = 6379spring.redis.pool.maxIdle = 20
spring.redis.pool.maxTotal = 20
spring.redis.pool.numTestsPerEvictionRun = 3
spring.redis.pool.testOnBorrow = true
spring.redis.pool.blockWhenExhausted = false
spring.redis.pool.testOnReturn = false

通过 Environment 对象,可以获得property信息

 

转载于:https://my.oschina.net/foreverZx/blog/674185

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/1133.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息