springboot和Redis集群版的整合
此篇接上一个文章springboot和Redis单机版的整合
https://www.cnblogs.com/lin530/p/12019023.html
下面接着介绍和Redis集群版的整合。
1.第一步惯例先导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
2.配置application.yml文件,这里没有配置其他多余的东西,需要其他功能的自行加上。
spring:
redis:
cluster:
nodes: 192.168.159.104:7001,192.168.159.104:7002,192.168.159.104:7003,192.168.159.104:7004,192.168.159.104:7005,192.168.159.104:7006
注意格式。
3.为了方便springboot初始化,采用Java配置文件。
先提供一个pojo封装信息RedisProperties
package com.bai.springdemo1.config; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; //依赖注入
@Component
//该注解用于读取配置文件中的属性,其中prefix表示前缀;
@PropertySource("classpath:application.yml")
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisProperties {
private int expireSeconds;
private String nodes;
private int commandTimeout; public int getExpireSeconds() {
return expireSeconds;
} public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
} public String getNodes() {
return nodes;
} public void setNodes(String nodes) {
this.nodes = nodes;
} public int getCommandTimeout() {
return commandTimeout;
} public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
} @Override
public String toString() {
return "RedisProperties{" +
"expireSeconds=" + expireSeconds +
", nodes='" + nodes + '\'' +
", commandTimeout=" + commandTimeout +
'}';
}
}
此处介绍一下可能遇到的坑,ConfigurationProperties注解在现在的高版本中取消了一些功能,可能导致读取不到配置信息,需要配置一下。
手动在ConfigurationProperties上面添上注解@PropertySource("classpath:application.yml")。然后再添加一个依赖即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
如果idea上方仍有红色提示信息,不必理会,测试一下能访问到配置文件即可。若想不提示,可自行去除。
4.配置一下Java格式的配置文件RedisClusterConfig,通过注解可以实现随系统自动初始化。
package com.bai.springdemo1.config; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster; import java.util.HashSet;
import java.util.Set; @Configuration
public class RedisClusterConfig { @Autowired
private RedisProperties redisProperties; @Bean
public JedisCluster getJedisCluster(){
//获取redis集群的ip及端口号等相关信息;
String[] serverArray = redisProperties.getNodes().split(",");
Set<HostAndPort> nodes = new HashSet<>(); //遍历add到HostAndPort中;
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
//构建对象并返回;
return new JedisCluster(nodes, redisProperties.getCommandTimeout());
}
}
5.全部配置完成即可进行使用。别忘了调用的时候注入jedisCluster
@Autowired
private JedisCluster jedisCluster;
springboot和Redis集群版的整合的更多相关文章
- springboot+shiro+redis(集群redis版)整合教程
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3.springboot+shiro+redis(单机red ...
- 快速搭建redis单机版和redis集群版
单机版 第一步:需要安装redis所需的C语言环境,若虚拟机联网,则执行 yum install gcc-c++ 第二步:redis的源码包上传到linux系统 第三步:解压缩redis tar ...
- Redis集群版在Java中的应用
1.配置redis集群 <?xml version="1.0" encoding="UTF-8"?> <redisCluster> &l ...
- Linux 安装Redis<集群版>(使用Mac远程访问)
阅读本文需要先阅读安装Redis<准备> 一 架构细节 所有的redis节点彼此互联(PING-PONG机制) 内部使用二进制协议优化传输速度和带宽 节点的fail是通过集群中超过半数的节 ...
- springboot基础-redis集群
一.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- Redis 集群版
1.# yum install ruby -y 1.1 后面需要用到ruby脚本 2.# yum install rubygems -y 1.1 安装ruby包管理器 3.# gem install ...
- springboot集成redis集群
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 使用jedis客户端连接redis,单机版和集群版
单机版 1.入门实例 @Test public void testJedis(){ //创建一个jedis对象,需要指定服务的ip和端口号 Jedis jedis=new Jedis("19 ...
- 04.redis集群+SSM整合使用
redis集群+SSM整合使用 首先是创建redis-cluster文件夹: 因为redis最少需要6个节点(三主三从),为了更好的理解,我这里创建了两台虚拟机(192.168.0.109 192.1 ...
随机推荐
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- LeetCode_67. Add Binary
67. Add Binary Easy Given two binary strings, return their sum (also a binary string). The input str ...
- (二)UML之类图、接口、包
一.概念 类图(Class Diagram): 类图是面向对象系统建模中最常用和最重要的图,是定义其它图的基础.类图主要是用来显示系统中的类.接口以及它们之间的静态结构和关系的一种静态模型. 类图的3 ...
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- D-Link系列路由器漏洞挖掘
参考 http://www.freebuf.com/articles/terminal/153176.html https://paper.seebug.org/429/ http://www.s3c ...
- 不论报任何错误 都是网络源有问题,安装spacemacs报错的解决方式
不论报任何错误 都是网络源有问题 打开.spacemacs ### 这是原头部 (defun dotspacemacs/layers () "Configuration Layers d ...
- iOS开发系列之app的一天
本文主要讲述我对 iOS 开发的一些理解,希望能通过 app 从启动到退出,将一些的知识整合起来,形成一条知识链,目前涉及到的知识点有 runloop.runtime.文件存储.界面布局.离线推送.内 ...
- [转帖]如何保护你的 Python 代码 (一)—— 现有加密方案
如何保护你的 Python 代码 (一)—— 现有加密方案 Prodesire Python猫 1周前
- logback的xml配置文件模板(超详细)
<?xml version="1.0" encoding="UTF-8" ?> <!-- 在此未说明属性为非必须的,那就表示属性必须设置 -- ...
- 将dubbo中使用的动态代理作为工具类
ReflectUtils package per.qiao.util.javassistUtil; import java.lang.reflect.Constructor; import java. ...