前言

上一篇文章介绍了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);
}
}
 

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

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

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

  2. Spring Boot系列教程十二:Spring boot集成Redis

    一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...

  3. Spring Boot系列教程十:Spring boot集成MyBatis

    一.创建项目         项目名称为 "springboot_mybatis_demo",创建过程中勾选 "Web","MyBatis" ...

  4. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

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

    在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...

  6. Spring Boot系列教程十四:Spring boot同时支持HTTP和HTTPS

    自签证书 openssl生成服务端证书,不使用CA证书直接生成 -in server.csr -signkey server.key -out server.crt # 5.server证书转换成ke ...

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

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

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

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

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

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

随机推荐

  1. Hibernate5使用注解方式(转)

    用Hibernate5使用映射文件时存在一个问题没有解决,在映射文件中配置了student_sequence,但找不到映射文件自增长的序列的sequence(Oracle)数据库. 输出的是 Hibe ...

  2. python常用模块详解2

    序列化模块补充: 1.json格式的限制,json格式的key必须是字符串数据类型 2.json格式的字符串必须是"" 如果数字是key,那么dump之后会强转成字符串数据类型 i ...

  3. C#入门经典第十章例题 - - 卡牌

    1.库 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  4. Unity —— 通过鼠标点击控制物体移动

    //ClickMove - - 通过鼠标点击控制物体移动 using System.Collections; using System.Collections.Generic; using Unity ...

  5. zabbix搭建并结合mikoomi插件监控hadoop集群

    一.环境说明 系统: CentOS release 6.4 mysql 5.1.67 Apache/2.2.25 二.安装php 由于zabbix提供的frontends是php编写的,因此需要搭建l ...

  6. Redis的数据类型以及每种数据类型的使用场景

    人就是很奇怪的动物,很简单的问题往往大家都容易忽略,当我们在使用分布式缓存Redis的时候,一个最简单的问题Redis的数据类型以及每种数据类型的使用场景是什么? 是不是觉得这个问题很基础?我也这么觉 ...

  7. hadoop之mapper类妙用

    1. Mapper类 首先 Mapper类有四个方法: (1) protected void setup(Context context) (2) Protected void map(KEYIN k ...

  8. 记录一次爬虫报错:Message: Failed to decode response from marionette

    由于标题中的错误引发: Message: Tried to run command without establishing a connection 解释: 先说一下我的爬虫架构,用的是firefo ...

  9. Python3 解压序列

    一 普遍情况: x,y,z = 1,2,3 print("x:",x) # x:1 print("y:",y) # y:2 print("z:&quo ...

  10. Python Requests库简单入门

    我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...