一、安装Redis集群

安装步骤参照网上教程,Mac安装步骤参照https://github.com/muyl/mac-docker-redis-cluster

二、创建SpringBoot工程

  1. 创建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;
    }
    }
  2. 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
  3. 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);
    }
    }
  4. 测试类

    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集群的更多相关文章

  1. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  2. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  3. springmvc3.2集成redis集群

    老项目需要集成redis集群 因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成. jar包准备: jedis-2.9.0.jar  -- 据说只有这 ...

  4. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

  5. spring集成redis,集成redis集群

    原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...

  6. springboot集成redis集群

    1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. Springboot 2.0.x 集成基于Centos7的Redis集群安装及配置

    Redis简介 Redis是一个基于C语言开发的开源(BSD许可),开源高性能的高级内存数据结构存储,用作数据库.缓存和消息代理.它支持数据结构,如 字符串.散列.列表.集合,带有范围查询的排序集,位 ...

  8. Spring-Session实现Session共享Redis集群方式配置教程

    循序渐进,由易到难,这样才更有乐趣! 概述 本篇开始继续上一篇的内容基础上进行,本篇主要介绍Spring-Session实现配置使用Redis集群,会有两种配置方式,一种是Redis-Cluster, ...

  9. Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)

    文/朱季谦 背景:最近在对一新开发Springboot系统做压测,发现刚开始压测时,可以正常对redis集群进行数据存取,但是暂停几分钟后,接着继续用jmeter进行压测时,发现redis就开始突然疯 ...

随机推荐

  1. Ubuntu Qt5.13 无法输入中文和中文显示乱码问题

    无法输入中文: sudo apt-get install libfcitx-qt5-dev cd /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminput ...

  2. Oracle报错注入总结

    0x00 前言 在oracle注入时候出现了数据库报错信息,可以优先选择报错注入,使用报错的方式将查询数据的结果带出到错误页面中. 使用报错注入需要使用类似 1=[报错语句],1>[报错语句], ...

  3. 渗透测试-基于白名单执行payload--Pcalua

    0x01 Pcalua简介 Windows进程兼容性助理(Program Compatibility Assistant)的一个组件. 说明:Pcalua.exe所在路径已被系统添加PATH环境变量中 ...

  4. GDAL集成对KML文件的支持

    目录 1. 正文 1.1. 编译LibKML 1.1.1. 第三方库支持 1.1.2. 编译错误 1.2. 配置GDAL 1.3. 链接问题 2. 参考 1. 正文 GDAL可以支持将KML作为矢量文 ...

  5. jmeter-定时器使用

    在一般性能测试过程中,往往在前一个请求之后等待一段时间再执行下一个请求,这时会用到定时器. 以下列举常用的3中: 1.固定定时器: 2.

  6. 利用Arthas定位线上问题实例

    前言 Arthas是一个类似于Btrace的JVM在线调试分析工具,具体可参考我之前写的一篇博客:利用JVM在线调试工具排查线上问题.本文分享笔者刚遇到的一个问题,虽然不复杂,但是很典型. 问题与分析 ...

  7. 17.Linux搭建网络仓库

    1.搭建一个网络仓库 服务端:10.0.0.201 1.准备软件包(1.光盘 2.缓存 3.联网下载 4.同步) 1.挂载光盘 mount /dev/cdrom 2.通过ftp共享软件包存放的目录 y ...

  8. JAVA学习笔记-1.Tomcat&Servlet

    ##web相关概念 1.软件架构 1.C/S 2.B/S 2.资源分类 1.静态资源:所有用户访问后,得到的结果都是一样的,称为静态资源, 静态资源可以直接被浏览器解析. * 如:html, css, ...

  9. 时序数据库InfluxDB(I)- 搭建与采集信息demo操作

    搭建环境:vmware workstation pro15.5.0, ubuntu18.04.3 实践时间:2019.10.12-10.27 (一)时序数据库InfluxDB准备 (1)安装 曾出现问 ...

  10. CSP考场Emacs使用指南[原创]

    前言: 据说,CSP考试,之后不再支持windows了呢. windows用户真得劲! 那用什么系统? Ubuntu上场了 Ubuntu编译指南 进入Ubuntu系统,在你想存的文件夹中新建一个空白文 ...