国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

spring-boot-redis-cluster-demo

2019-11-06 06:43:58
字體:
來源:轉載
供稿:網友

摘要

Redis在項目中使用頻率是很高的,使用的時候經常都是以Redis集群的形式。現整理一下SPRing-Boot整合redis cluster最基礎配置,方便以后查閱。

依賴包

下面2個依賴是spring-boot集成Redis的必備依賴。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.4.RELEASE</version></dependency>

如果啟用spring-boot單元測試,還需要加入下面的依賴。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring-boot.version}</version> <scope>test</scope></dependency>

配置

application.yml配置Redis集群節點信息

spring: redis: cluster: nodes: - redis1.itclj.com:7000 - redis1.itclj.com:7001 - redis2.itclj.com:7002 - redis2.itclj.com:7003 - redis3.itclj.com:7004 - redis3.itclj.com:7005

初始化

spring-boot默認都采用注解方式初始化bean。 首先建一個redis集群配置bean,從配置文件中讀取配置到配置bean里面。 其次建一個redis Cluster初始化配置bean,用于初始化Redis Cluster。

集群配置Bean

由于復雜配置項,如數組不能通過@Value注解直接讀取配置項,所有只能采用新建配置Bean通過@ConfigurationProperties注解讀取。

@Configuration@ConfigurationProperties(prefix = "spring.redis.cluster")public class RedisClusterProperties { //集群節點 private List<String> nodes=new ArrayList<>(); public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; }}

初始化RedisCluster

@Configuration@ConditionalOnClass(RedisClusterConfig.class)@EnableConfigurationProperties(RedisClusterProperties.class)public class RedisClusterConfig { @Resource private RedisClusterProperties redisClusterProperties; @Bean public JedisCluster redisCluster(){ Set<HostAndPort> nodes = new HashSet<>(); for (String node:redisClusterProperties.getNodes()){ String[] parts= StringUtils.split(node,":"); Assert.state(parts.length==2, "redis node shoule be defined as 'host:port', not '" + Arrays.toString(parts) + "'"); nodes.add(new HostAndPort(parts[0], Integer.valueOf(parts[1]))); } return new JedisCluster(nodes); }}

測試

@RunWith(SpringRunner.class)@SpringBootTestpublic class RedisTest { @Autowired private JedisCluster jedisCluster; @Test public void get(){ System.out.println("=============="+jedisCluster.get("youqian-spread-sync-to-MySQL-date")); }}

原文地址:http://www.itclj.com/blog/58bcf3f947508f786718d4f3 項目地址:https://github.com/clj198606061111/spring-boot-redis-cluster-demo


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 收藏| 仙居县| 菏泽市| 金沙县| 海安县| 瑞丽市| 乃东县| 西青区| 灵璧县| 金沙县| 固镇县| 星子县| 怀仁县| 阜康市| 灌阳县| 林周县| 钦州市| 定州市| 淳化县| 手机| 桂平市| 庆城县| 广宗县| 兖州市| 萨迦县| 永平县| 宜君县| 泸定县| 万盛区| 惠安县| 绩溪县| 娄烦县| 秀山| 贵港市| 伊通| 茌平县| 大竹县| 泾阳县| 苍梧县| 建德市| 武宁县|