Spring Boot系列教程十:Spring boot集成Sentinel Redis
前言
实现:
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的更多相关文章
- Spring Boot2 系列教程(十)Spring Boot 整合 Freemarker
今天来聊聊 Spring Boot 整合 Freemarker. Freemarker 简介 这是一个相当老牌的开源的免费的模版引擎.通过 Freemarker 模版,我们可以将数据渲染成 HTML ...
- Spring Boot系列教程十二:Spring boot集成Redis
一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...
- Spring Boot系列教程十:Spring boot集成MyBatis
一.创建项目 项目名称为 "springboot_mybatis_demo",创建过程中勾选 "Web","MyBatis" ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
- Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
在 Java 领域,数据持久化有几个常见的方案,有 Spring 自带的 JdbcTemplate .有 MyBatis,还有 JPA,在这些方案中,最简单的就是 Spring 自带的 JdbcTem ...
- Spring Boot系列教程十四:Spring boot同时支持HTTP和HTTPS
自签证书 openssl生成服务端证书,不使用CA证书直接生成 -in server.csr -signkey server.key -out server.crt # 5.server证书转换成ke ...
- Spring Boot2 系列教程(八)Spring Boot 中配置 Https
https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...
- Spring Boot2 系列教程(五)Spring Boot中的 yaml 配置
搞 Spring Boot 的小伙伴都知道,Spring Boot 中的配置文件有两种格式,properties 或者 yaml,一般情况下,两者可以随意使用,选择自己顺手的就行了,那么这两者完全一样 ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
随机推荐
- 【mysql优化】mysql count(*)、count(1)、count(主键字段)、count(非主键字段)哪个性能最佳
测试结果为:count(*)和count(1)基本相等,count(非主键字段)最耗性能 -- 数据量 708254select count(*) from tmp_test1;-- avg 0.22 ...
- Boss直聘邮件通知小脚本
Boss 基于Python3的找工作利器--Boss直聘来消息邮件通知, 自动发送简历脚本,O(∩_∩)O~ 无聊写的,因为有时候觉得找工作心急如焚,想自动回复自动发简历啊有木有~~~ github地 ...
- 20130501-Twitter向全美用户开放广告平台Twitter Ads
腾讯科技讯(晁晖)北京时间5月1日消息,据国外媒体报道,Twitter今天向所有美国用户开放了广告平台Twitter Ads.自2012年3月发布以来,Twitter Ads只向受邀请用户开放.Twi ...
- 第八次作业psp
psp 进度条 代码累积折线图 博文累积折线图 psp饼状图
- 作业MathExamV2.0
MathExam233 211614269 林凯 211601233张康凌 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时( ...
- HDU 5672 String
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- JAVA里面json和java对象之间的相互转换
1. 把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromObject(list); String jsonstr = ar ...
- ArrayList和LinkedList源码分析
ArrayList 非线程安全 ArrayList内部是以数组存储元素的.类有以下变量: /*来自于超类AbstractList,使用迭代器时可以通过该值判断集合是否被修改*/ protected t ...
- 浅析GCC下C++多重继承 & 虚拟继承的对象内存布局
继承是C++作为OOD程序设计语言的三大特征(封装,继承,多态)之一,单一非多态继承是比较好理解的,本文主要讲解GCC环境下的多重继承和虚拟继承的对象内存布局. 一.多重继承 先看几个类的定义: 01 ...
- 运维学习笔记(三)之T01-03TCP/IP
TCP/IP协议 简介 通信协议 信息从源传递到目的地的过程中,网络上各设备需要通信,描述网络通信“语言”的规范就是协议. 数据通信协议 决定数据的格式和传输的一组规则. TCP/IP协议简介 一组通 ...