SpingBoot之集成Redis集群
一、安装Redis集群
安装步骤参照网上教程,Mac安装步骤参照https://github.com/muyl/mac-docker-redis-cluster
二、创建SpringBoot工程

创建Redis配置类
package com.example.chapterredis.common.config; import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
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; /**
* @author tony
*/
@Configuration
public class RedisConfiguration { private static final Logger logger = LoggerFactory.getLogger(RedisConfiguration.class); @Value("${spring.redis.clusterNodes}")
private String clusterNodes;
@Value("${spring.redis.password}")
private String auth;
@Value("${spring.redis.pool.maxActive}")
private Integer maxTotal;
@Value("${spring.redis.pool.minIdle}")
private Integer minIdle;
@Value("${spring.redis.pool.maxIdle}")
private Integer maxIdle;
@Value("${spring.redis.pool.maxWait}")
private Long maxWaitMillis;
@Value("${spring.redis.pool.commandTimeout}")
private int commandTimeout; @Bean
public JedisCluster jedisCluster() {
String[] serverArray = clusterNodes.split(",");
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
} if (!nodes.isEmpty()) {
String password = getAuth(auth);
logger.info("redis password:{}", password);
GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
pool.setMaxTotal(maxTotal);
pool.setMinIdle(minIdle);
pool.setMaxIdle(maxIdle);
pool.setMaxWaitMillis(maxWaitMillis);
return new JedisCluster(nodes, commandTimeout, commandTimeout, 5, password, pool);
}
return null;
} private String getAuth(String auth) {
return "".equals(auth) ? null : auth;
}
}SpringBoot属性文件
spring.redis.clusterNodes=localhost:7000,localhost:7001,localhost:7002,localhost:7003,localhost:7004,localhost:7005
spring.redis.password=
spring.redis.pool.maxActive=5
spring.redis.pool.minIdle=5
spring.redis.pool.maxIdle=1
spring.redis.pool.maxWait=3000
spring.redis.pool.commandTimeout=5000
SpringBoot启动类
package com.example.chapterredis; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ChapterRedisApplication { public static void main(String[] args) {
SpringApplication.run(ChapterRedisApplication.class, args);
}
}测试类
package com.example.chapterredis; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.JedisCluster; @RunWith(SpringRunner.class)
@SpringBootTest(classes={ChapterRedisApplication.class})
public class ChapterRedisApplicationTests { @Autowired
private JedisCluster jedisCluster; @Test
public void test2() {
jedisCluster.set("aaa","123");
System.out.println(jedisCluster.get("aaa"));
} }
三、工程源代码
https://gitee.com/shanksV/chapter-redis.git
比你优秀的人比你还努力,你有什么资格不去奋斗!!!
SpingBoot之集成Redis集群的更多相关文章
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- springmvc3.2集成redis集群
老项目需要集成redis集群 因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成. jar包准备: jedis-2.9.0.jar -- 据说只有这 ...
- Spring Boot集成Redis集群(Cluster模式)
目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...
- spring集成redis,集成redis集群
原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...
- springboot集成redis集群
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置
Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...
- Spring-Session实现Session共享Redis集群方式配置教程
循序渐进,由易到难,这样才更有乐趣! 概述 本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster, ...
- Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)
文/朱季谦 背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯 ...
随机推荐
- [Luogu2422]良好的感觉
题目描述 kkk做了一个人体感觉分析器.每一天,人都有一个感受值Ai,Ai越大,表示人感觉越舒适.在一段时间[i, j]内,人的舒适程度定义为[i, j]中最不舒服的那一天的感受值 * [i, j]中 ...
- 利用requests和BeautifulSoup爬取菜鸟教程的代码与图片并保存为markdown格式
还是设计模式的开卷考试,我想要多准备一点资料,于是写了个爬虫爬取代码与图片,有巧妙地进行格式化进一步处理,最终变为了markdown的格式 import requests from bs4 impor ...
- Spring Boot项目中如何定制PropertyEditors
本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...
- MyBatis 示例-传递多个参数
映射器的主要元素: 本章介绍 select 元素中传递多个参数的处理方式. 测试类:com.yjw.demo.MulParametersTest 使用 Map 传递参数(不建议使用) 使用 MyBat ...
- < 配置jupyer notebook遇到的问题 - 500 : Internal Server Error >
< anaconda配置jupyer notebook遇到的问题 - 500 : Internal Server Error > 问题描述: 我的jupyer notebook是在anac ...
- 分布式监控CAT服务端的本地部署
一.CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在 ...
- MongoDB实现问卷/考试设计
MongoDB的特点 MongoDB是一个面向文档存储的数据库.在MongoDB中,一条记录叫做document(文档),由类似于JSON结构的键值对组成. 由于类似于MongoDB直接存储JSON的 ...
- Solr导入MongoDB数据
数据导入方式: 全量导入和增量导入: query 是全量导入时,把你的数据中查到的数据全部导入,deltaImportQuery 和 deltaQuery 是增量导入数据所需要的两个查询语句.delt ...
- django-模型之从数据库获取数据(二)
1.获取一条数据(字段值必须唯一) 2.条件查询filter 3.排除查询exclude 4.链式查询 5.查询后进行排序order_by 6.按字段查询values 7.插入数据create 8.数 ...
- vue-music 使用better-scroll遇到轮播图不能自动轮播
根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播 这是因为,better-scroll发布新版本之后,参数设置发生改变 这是旧版本: ...