前提环境:

  主从配置  http://www.cnblogs.com/zwcry/p/9046207.html

  哨兵配置  https://www.cnblogs.com/zwcry/p/9134721.html

1.配置pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>szw</groupId>
  4. <artifactId>springboot_redis_sentinel</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>springboot_redis_sentinel</name>
  7. <description>springboot整合redis哨兵</description>
  8.  
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  12. <java.version>1.8</java.version>
  13. <start-class>com.sze.redis.SzwRedisApplication</start-class>
  14. </properties>
  15.  
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>1.4.2.RELEASE</version>
  20. <relativePath></relativePath>
  21. </parent>
  22.  
  23. <dependencies>
  24. <!-- 使用web启动器 -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <!-- 使用aop模板启动器 -->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-aop</artifactId>
  33. </dependency>
  34. <!-- 测试 -->
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-redis</artifactId>
  43. </dependency>
  44. </dependencies>
  45.  
  46. <!-- deploy -->
  47. <distributionManagement>
  48. <repository>
  49. <id>releases</id>
  50. <name>Releases</name>
  51. <url>http://192.168.3.71:8081/nexus/content/repositories/releases/</url>
  52. </repository>
  53. <snapshotRepository>
  54. <id>snapshots</id>
  55. <name>Snapshots</name>
  56. <url>http://192.168.3.71:8081/nexus/content/repositories/snapshots/</url>
  57. </snapshotRepository>
  58. </distributionManagement>
  59.  
  60. <!-- download -->
  61. <repositories>
  62. <repository>
  63. <id>sicdt</id>
  64. <name>Sicdt</name>
  65. <url>http://192.168.3.71:8081/nexus/content/groups/public</url>
  66. </repository>
  67. <repository>
  68. <id>spring-releases</id>
  69. <url>https://repo.spring.io/libs-release</url>
  70. </repository>
  71. </repositories>
  72. <pluginRepositories>
  73. <pluginRepository>
  74. <id>spring-releases</id>
  75. <url>https://repo.spring.io/libs-release</url>
  76. </pluginRepository>
  77. </pluginRepositories>
  78.  
  79. <build>
  80. <plugins>
  81. <!-- 要将源码放上去,需要加入这个插件 -->
  82. <plugin>
  83. <groupId>org.apache.maven.plugins</groupId>
  84. <artifactId>maven-source-plugin</artifactId>
  85. <configuration>
  86. <attach>true</attach>
  87. </configuration>
  88. <executions>
  89. <execution>
  90. <phase>compile</phase>
  91. <goals>
  92. <goal>jar</goal>
  93. </goals>
  94. </execution>
  95. </executions>
  96. </plugin>
  97. <!-- 打包 -->
  98. <plugin>
  99. <groupId>org.springframework.boot</groupId>
  100. <artifactId>spring-boot-maven-plugin</artifactId>
  101. <configuration>
  102. <fork>true</fork>
  103. </configuration>
  104. </plugin>
  105. </plugins>
  106. </build>
  107. </project>

2.配置application.properties

  1. ##单服务器
  2. spring.redis.host=39.107.119.256
  3. ##单端口
  4. spring.redis.port=6381
  5. ## 连接池最大连接数(使用负值表示没有限制)
  6. spring.redis.pool.max-active=300
  7. ## Redis数据库索引(默认为0)
  8. spring.redis.database=0
  9. ## 连接池最大阻塞等待时间(使用负值表示没有限制)
  10. spring.redis.pool.max-wait=-1
  11. ## 连接池中的最大空闲连接
  12. spring.redis.pool.max-idle=100
  13. ## 连接池中的最小空闲连接
  14. spring.redis.pool.min-idle=20
  15. ## 连接超时时间(毫秒)
  16. spring.redis.timeout=60000
  17.  
  18. #哨兵的配置列表
  19. spring.redis.sentinel.master=mymaster
  20. spring.redis.sentinel.nodes=39.107.119.256:26379
    ##哨兵集群
    #spring.redis.sentinel.nodes=39.107.119.254:26379,39.107.119.254:26380

3.编写启动类

  1. package com.szw.redis;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
  6. @SpringBootApplication
  7. public class SzwRedisApplication {
  8. public static void main(String[] args) {
  9. System.setProperty("spring.devtools.restart.enabled", "false");
  10. SpringApplication.run(SzwRedisApplication.class, args);
  11. }
  12. }

4.单元测试

  1. package com.sze.redis;
  2.  
  3. import javax.annotation.PostConstruct;
  4.  
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. import org.springframework.data.redis.core.ValueOperations;
  11. import org.springframework.test.annotation.DirtiesContext;
  12. import org.springframework.test.context.junit4.SpringRunner;
  13.  
  14. @RunWith(SpringRunner.class)
  15. @SpringBootTest
  16. public class SentinelTest {
  17.  
  18. @Autowired
  19. StringRedisTemplate redisTemplate;
  20.  
  21. ValueOperations<String, String> stringRedis;
  22.  
  23. @PostConstruct
  24. public void init(){
  25. stringRedis=redisTemplate.opsForValue();
  26. }
  27.  
  28. @Test
  29. public void testString (){
  30. stringRedis.set("name", "丁洁");
  31. System.out.println(stringRedis.get("name"));
  32. }
  33. }

5.多个哨兵配置

  1. ##单服务器
  2. spring.redis.host=39.107.119.256
  3. ##单端口
  4. spring.redis.port=6381
  5. ## 连接池最大连接数(使用负值表示没有限制)
  6. spring.redis.pool.max-active=300
  7. ## Redis数据库索引(默认为0)
  8. spring.redis.database=0
  9. ## 连接池最大阻塞等待时间(使用负值表示没有限制)
  10. spring.redis.pool.max-wait=-1
  11. ## 连接池中的最大空闲连接
  12. spring.redis.pool.max-idle=100
  13. ## 连接池中的最小空闲连接
  14. spring.redis.pool.min-idle=20
  15. ## 连接超时时间(毫秒)
  16. spring.redis.timeout=60000
  17.  
  18. #哨兵的配置列表
  19. spring.redis.sentinel.master=mymaster
  20. spring.redis.sentinel.nodes=39.107.119.256:26379,39.107.119.256:26380

SpringBoot整合redis哨兵主从服务的更多相关文章

  1. SpringBoot整合Redis集群

    一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...

  2. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

  3. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

  4. 三:Springboot整合Redis

    一:springboot整合redis redis版本:3.0.0 运行环境:linux 1.安装redis 1.1安装gcc yum install gcc-c++ 1.2解压redis.3.0.0 ...

  5. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

  6. Spring Boot(十三):整合Redis哨兵,集群模式实践

    前面的两篇文章(Redis的持久化方案, 一文掌握Redis的三种集群方案)分别介绍了Redis的持久化与集群方案 -- 包括主从复制模式.哨兵模式.Cluster模式,其中主从复制模式由于不能自动做 ...

  7. SpringBoot整合Redis、mybatis实战,封装RedisUtils工具类,redis缓存mybatis数据 附源码

    创建SpringBoot项目 在线创建方式 网址:https://start.spring.io/ 然后创建Controller.Mapper.Service包 SpringBoot整合Redis 引 ...

  8. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  9. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

随机推荐

  1. import Vue form 'vue’的意思

    1.import Vue form ‘vue’ 写全的话是import Vue from ‘…/nodemouls/vue/list/vue.js’: 此时在webpack.base.conf.js中 ...

  2. SQL Server数据恢复准备之TRUNCATE TABLE理解

    SQL Server数据恢复准备之TRUNCATE TABLE理解 转自:https://blog.51cto.com/aimax/2142553 易语随风去关注0人评论6717人阅读2018-07- ...

  3. vant - 头部 - header【Layout 布局】【Icon 图标】

    安装 npm i vant -S [main.js] import Vant from 'vant'; import 'vant/lib/index.css'; Vue.use(Vant); [ind ...

  4. one order 理解

    1: one order core

  5. javascript封装animate动画

    面向对象式: Element.prototype.animate=animate; Element.prototype.getStyle=getStyle; function animate(json ...

  6. 算法笔记-PHP实现栈的操作

    [栈]后进先出,先进后出,这就是典型的“栈”结构.         任何数据结构都是对特定应用场景的抽象,数组和链表虽然使用起来更加灵活,但却暴露了几乎所有的操作,难免会引发错误操作的风险.      ...

  7. [vue]webpack3最佳实践篇

    vue-render: https://www.cnblogs.com/iiiiiher/articles/9465311.html es6模块的导入导出 https://www.cnblogs.co ...

  8. Python生态工具、文本处理和系统管理(虚拟)

    一.Python生态工具 一.Python内置小工具 1.秒级启动一个下载服务器 Python 内置了一个下载服务器就能够显著提升效率了 . 例如, 你的同事要让你传的文件位于某一个目录下,那么,你可 ...

  9. 【MySQL】-NO.21.MySQL.1.MySQL.1.001-【Install MySQL5.7 On Windows】

    1.0.0 Summary Tittle:[MySQL]-NO.21.MySQL.1.MySQL.1.001-[Install MySQL5.7 On Windows] Style:Web Serie ...

  10. gerrit设置非小组成员禁止下载代码

    对gerrit有所了解的同学,都知道gerrit 是我们常用的一个来做代码审核的工具,其中的权限管理,是一个非常重要的环节,关于每个权限的使用范围,可以参考博客https://blog.csdn.ne ...