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 ...
随机推荐
- LeetCode_83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Easy Given a sorted linked list, delete all duplicates such t ...
- 用Python做一个简单的小游戏
学习总是枯燥的,对于Python小白的我来讲,更是乏味的.为了提高学习的兴趣,今天我就来写一个小程序练练手. 数字猜谜游戏相信大家都不陌生,A给出最小值最大值,B写一个该范围内的数,A猜测写下的是多少 ...
- 优化apk的odex处理
前文讲到了apk的反编译流程,但在有的情况下会出现apk的资源文件和代码文件分开的情况,这种情况会出现如下目录结构,并且在apk文件里面并没有dex文件. 所需工具 smali.jar和baksmal ...
- python安装第三方的包
转载自:http://blog.csdn.net/xyqzki/article/details/38414433 第一种基本安装方法 1 下载第三方包,解压 2 在命令提示符里输入cmd,然后用cd进 ...
- mac 10.14.5 [vue create的时候 mkdir没有权限]
1.vue create的时候 mkdir没有权限 2.第一步.先检查系统是否开启了安全模式 csrutil status 3.如果是disabled.进去步骤5,直接查看文件夹权限.如果是enabl ...
- .NET细节知识总结,不断更新
1.catch (Exception)和catch (Exception e) Exception 类包含许多子类 程序执行的时候要将每一个类都搜索一遍 以找到符合的异常类 这样是蛮消耗资源的 影响效 ...
- java多线程上篇(三) -- 进程通信和线程死锁简单介绍
进程通信指的是进程间的信息交换 ,IPC(Inter-Process Communication,进程间通信) 进程通信就相当于一种工作方式.沟通形式,进程通信主要指的就是操作系统提供的进程通信工具( ...
- [DEBUG] ubuntu mysql root@localhost改了密码还是进不去ERROR 1698 (28000)
之前用skip-grant-tables的方法免密进入Mysql,修改了root的密码, 当时重启服务后是可以用密码进入Mysql的.结果昨天突然又进不去了:) 所以更换方法,特此记录. ====== ...
- Essential C++ Reading Notes
Chapter1 P6, 1.2 Why //#include<string> we still can use "string user_name"? -->c ...
- Dining(POJ-3281)【最大流】
题目链接:https://vjudge.net/problem/POJ-3281 题意:厨师做了F种菜各一份,D种饮料各一份,另有N头奶牛,每只奶牛只吃特定的菜和饮料,问该厨师最多能满足多少头奶牛? ...