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 ...
随机推荐
- ansible软件部署
每一个软件或集群都创建一个单独的角色. mkdir -pv ansible/roles/{jdk,hadoop,spark}/{tasks,files,templates,meta,handlers, ...
- vs2015配置link.exe环境变量
https://www.cnblogs.com/johnwii/p/4966086.html
- Linux下通过shell进MySQL执行SQL或导入脚本
这条命令表示通过用户名和密码执行shell然后在shell里面执行一个建表语句: USER="root" PASS="root" mysql -u $USER ...
- MediaElement 不能显示的问题
1.记得在窗体的 Load 事件里面调用 Player1.Play();方法,就可以在窗体加载后就直接播放视频 2.WPF MediaElement其实和Windows系统自带的播放器是一样的,因此W ...
- 欧姆龙NX1P 输送马达功能块
一个简单的马达输送轨道功能块,需要的小伙伴可以参考下,个人能力有限,不足的地方还请包涵. 下载链接:https://pan.baidu.com/s/1V1gioE0boDpaUsR5cqQ5dg
- js类似新闻图片轮换(带有文字介绍,其实可以放任何内容)
js类似新闻图片轮换(带有文字介绍,其实可以放任何内容) 主要是利用style="display:block;" 属性<script language="javas ...
- Hadoop配置环境变量Program~2的用法
[学习笔记] 3)配置环境变量:(环境变量中的~1,~2,~3的用法)i)JAVA_HOME:注意C:\Program Files目录存在空格,变成C:\Progra~1\Java\jdk1.8.0_ ...
- Spring Boot系列教程十二:Spring boot集成Redis
一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...
- Centos 修改IP地址、网关、DNS
一.CentOS 修改IP地址 修改对应网卡的IP地址的配置文件 # vi /etc/sysconfig/network-scripts/ifcfg-eth0 电信 # vi /etc/sys ...
- python reportlab 生成table
''' Table(data, colWidths=None, rowHeights=None, style=None, splitByRow=, repeatRows=, repeatCols=, ...