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 ...
随机推荐
- NodeJs本地搭建服务器,模拟接口请求,获取json数据
最近在学习Node.js,虽然就感觉学了点皮毛,感觉这个语言还不错,并且也会一步步慢慢的学着的,这里实现下NodeJs本地搭建服务器,模拟接口请求,获取json数据. 具体的使用我就不写了,这个博客写 ...
- 容器版单个jenkins实现CI/CD----带solo博客开源项目
实验架构: 192.168.0.96 gitlab 192.168.0.97 jenkins.docker-1.7 192.168.0.98 harbor.docker-1.7集群 jenkins安装 ...
- centos7.5安装图形界面
1.centos7.4安装图形界面 yum check-update && yum install epel-release && yum groupinstall & ...
- java基础系列(二):java数据结构及常用方法
1.数组Array (1)创建数组 dataType[] arrayName = new dataType[length];必须指定大小,否则会报错:如果不想指定大小,应采用声明数组变量的方式 dat ...
- Vue.js—60分钟快速入门
本文摘自:http://www.cnblogs.com/keepfool/p/5619070.html Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的 ...
- hdu 1209 Clock
Clock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 用BERT做语义相似度匹配任务:计算相似度的方式
1. 自然地使用[CLS] 2. cosine similairity 3. 长短文本的区别 4. sentence/word embedding 5. siamese network 方式 1. 自 ...
- php iconv实现编码转换
php iconv实现编码转换 <pre><?php $content = iconv('GB2312', 'UTF-8//IGNORE', $content); ?> < ...
- Linux集群环境准备及20项基础优化
中小规模集群架构: 1.什么是集群? 简单地说,集群就是一堆机器 做同一件事, 例如:京东(www.jd.com)提供卖东西服务这就是一件事,可能是几千台服务器,在背后运转支撑这个网站. www.ba ...
- v-CheckBox
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...