前言

上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方案。
哨兵sentinel的地址如下:
192.168.12.194:26379
192.168.12.194:36379
192.168.12.194:46379

Redis的地址如下:
192.168.12.194:6379
192.168.12.194:6380
192.168.12.194:6381

实现:

properties配置文件中添加配置信息:
spring.redis.database=0
spring.redis.password=123456 # pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1 #哨兵监听redis server名称
spring.redis.sentinel.master=mymaster
#哨兵的配置列表
spring.redis.sentinel.nodes=192.168.12.194:26379,192.168.12.194:36379,192.168.12.194:46379

创建RedisComponent类
package com.woniu.RedisComponent;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import com.woniu.bean.User; @Component
public class RedisComponent { @Autowired
//操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
private StringRedisTemplate stringRedisTemplate; @Autowired
// RedisTemplate,可以进行所有的操作
private RedisTemplate<Object,Object> redisTemplate; public void set(String key, String value){
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
boolean bExistent = this.stringRedisTemplate.hasKey(key);
if (bExistent) {
System.out.println("this key is bExistent!");
}else{
ops.set(key, value);
}
} public String get(String key){
return this.stringRedisTemplate.opsForValue().get(key);
} public void del(String key){
this.stringRedisTemplate.delete(key);
} public void sentinelSet(User user){
String key = null;
try {
key = new String(user.getId().getBytes("gbk"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println(key);
redisTemplate.opsForValue().set(key, user.toString());
} public String sentinelGet(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}

添加测试类的测试代码


package com.woniu;

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 com.woniu.RedisComponent.RedisComponent;
import com.woniu.bean.User; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootSentinelredisApplicationTests { @Autowired
private RedisComponent redisComponet; @Test
public void sentinelSet(){
User user = new User();
user.setId("001");
user.setAge("30");
user.setName("wangpengfei"); redisComponet.sentinelSet(user);
} @Test
public void sentinelGet(){
String str = redisComponet.sentinelGet("001");
System.out.println(str);
}
}

工程springboot_sentinelredis源码下载地址:点击打开链接

spring boot讨论群:611262656,一键加群:点击加群

更多技术文章请关注微信公众号“Java架构师之路”:

Spring Boot系列教程十三:Spring boot集成Sentinel Redis的更多相关文章

  1. Spring Boot2 系列教程(十三)Spring Boot 中的全局异常处理

    在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...

  2. Spring Boot2 系列教程(十)Spring Boot 整合 Freemarker

    今天来聊聊 Spring Boot 整合 Freemarker. Freemarker 简介 这是一个相当老牌的开源的免费的模版引擎.通过 Freemarker 模版,我们可以将数据渲染成 HTML ...

  3. Spring Boot2 系列教程(八)Spring Boot 中配置 Https

    https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...

  4. Spring Boot2 系列教程(五)Spring Boot中的 yaml 配置

    搞 Spring Boot 的小伙伴都知道,Spring Boot 中的配置文件有两种格式,properties 或者 yaml,一般情况下,两者可以随意使用,选择自己顺手的就行了,那么这两者完全一样 ...

  5. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  6. Spring Boot2 系列教程(十一)Spring Boot 中的静态资源配置

    当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥 Spring Boot 中的静态资源加载问题:"松哥,我的 HTML 页面好像没有样 ...

  7. Spring Boot2 系列教程 (十三) | 整合 MyBatis (XML 版)

    前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前介绍过了 SpringBoot 整合MyBatis 注解版的使用,上一篇介绍过 MyBatis ...

  8. Spring Boot 系列教程9-swagger-前后端分离后的标准

    前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...

  9. Spring Boot 系列教程19-后台验证-Hibernate Validation

    后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...

随机推荐

  1. express中的中间件(middleware)、自定义中间件、静态文件中间件、路由中间件

    express文档地址 什么是中间件呢(middleware)?它是谁的中间件呢? 首先我们需要了解到请求和响应, 请求就是客户端发送请求给服务器, 响应就是,服务器根据客户端的请求返回给客户端的数据 ...

  2. c++ 将字符串转换为数字

    int string2int(string x); int string2int(string x){ int a; string res=x; stringstream ss; ss << ...

  3. linux ssh终端解决中文乱码的问题

    @1:第一种办法: 在linux服务器里 命令行修改Linux服务器文件: vi /etc/sysconfig/i18n 默认的内容为: LANG="zh_CN.UTF-8" ; ...

  4. 3.linux 别名 查找 压缩与解压缩

    1.别名     alias  别名=‘定义命令内容’     alias grep=‘grep --color’       vim  ~/.bashrc     关掉终端再开一次!!!!才能生效 ...

  5. Elasticsearch6.4.0-windows环境部署安装

    Elasticsearch可以轻松的实现全文检索,本文主要介绍Elasticsearch(ES)环境的安装部署,该文及后续使用的ES版本为6.4.0.希望能够帮助到大家. 一.安装Elasticsea ...

  6. go struct 自定义标签获取

    package main import ( "fmt" "reflect" ) type Test struct { Id int `json:"us ...

  7. OpenGL ES: (1) OpenGL ES的由来 (转)

    1. 电脑是做什么用的? 电脑又被称为计算机,那么最重要的工作就是计算.看过三体的同学都知道, 电脑中有无数纳米级别的计算单元,通过 0 和 1 的转换,完成加减乘除的操作. 2. 是什么使电脑工作? ...

  8. appium-FAQ(持续更新...)

    Q1:未安装APP直接启用appium sever,初始化driver :driver = new AndroidDriver(new URL("http://127.0.0.1:4723/ ...

  9. 单硬盘根分区扩容(非LVM)

    单用户模式(内核参数末尾加single)救援模式(用光盘启动,选第三个,rescue installed system) 救援模式有什么作用: 1可以更改root密码:2恢复硬盘.文件系统操作:3系统 ...

  10. 利用redis实现分布式事务锁,解决高并发环境下库存扣减

    利用redis实现分布式事务锁,解决高并发环境下库存扣减   问题描述: 某电商平台,首发一款新品手机,每人限购2台,预计会有10W的并发,在该情况下,如果扣减库存,保证不会超卖 解决方案一 利用数据 ...