Spring-Boot使用RedisCluster
2019独角兽企业重金招聘Python工程师标准>>>
application.properties:
spring.redis.cluster.max-redirects=15 spring.redis.cluster.nodes=redis1:7000,redis1:7001,redis1:7002,redis1:7003,redis1:7004,redis2:7000,redis2:7001,redis2:7002,redis2:7003,redis2:7004,redis3:7000,redis3:7001,redis3:7002,redis3:7003,redis3:7004 spring.redis.database=0 spring.redis.pool.max-active=1000 spring.redis.pool.max-idle=800 spring.redis.pool.max-wait=3000 spring.redis.pool.min-idle=100
使用RedisTemplate:
package com.pasenger.redis.services;import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;/*** RedisCluster Operator Service* Created by Pasenger on 2017/3/16.*/@Service @Setter @Getter @Slf4j public class RedisClusterService {@Autowiredprivate RedisTemplate<String, String> redisTemplate;/*** Redis Set String** @param key* @param value*/public void setString(String key, String value) {redisTemplate.opsForValue().set(key, value);}/*** Redis Setex String** @param key* @param value* @param second*/public void setExString(String key, String value, long second) {redisTemplate.opsForValue().set(key, value, second, TimeUnit.SECONDS);}/*** @param key* @return*/public long getTtl(String key) {return redisTemplate.getExpire(key);}/*** Redis Get String** @param key* @return*/public String getString(String key) {return redisTemplate.opsForValue().get(key);}/*** Redis Delete** @param key*/public void delete(String key) {redisTemplate.delete(key);} }
单元测试:
package com.pasenger.redis.services;import org.junit.Assert; 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;/*** Created by Pasenger on 2017/3/16.*/ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class TestRedisClusterService {@Autowiredprivate RedisClusterService redisClusterService;@Testpublic void TestSetString() {redisClusterService.setString("TT1", "TT");Assert.assertEquals("TT", redisClusterService.getString("TT1"));}@Testpublic void TestSetExString() {redisClusterService.setExString("TT2", "TT", 60);try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}Assert.assertTrue(redisClusterService.getTtl("TT2") < 60L);} }
转载于:https://my.oschina.net/pasenger/blog/865339
Spring-Boot使用RedisCluster相关推荐
- 这 6 款能挣钱的 Spring Boot 开源后台管理系统,真TMD香!
不得不佩服 Spring Boot 的生态如此强大,今天我给大家推荐几款 Gitee 上优秀的后台开源版本的管理系统,小伙伴们再也不用从头到尾撸一个后台管理系统了,简直就是接私活,挣钱的利器啊. Sm ...
- spring boot项目 中止运行 最常用的几种方法
spring boot项目 中止运行 最常用的几种方法: 1. 调用接口,停止应用上下文 @RestController public class ShutdownController impleme ...
- html+spring boot简单的ajax数据传输实现
本篇讲解在前后端不分离情况下的html+spring boot的项目数据传输实现 首先,后台我写了三个接口 package com.demo.ajax.controller;import com.de ...
- Spring Boot整合Spring Data JPA操作数据
一. Sping Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套 JPA 应用框架,底层使用了 Hibernate 的 J ...
- SpringBoot (八) :Spring Boot多数据源(JdbcTemplate)配置与使用
什么是JdbcTemplate 为了使 JDBC 更加易于使用,Spring 在 JDBCAPI 上定义了一个抽象层, 以此建立一个JDBC存取框架. 作为 SpringJDBC 框架的核心, JDB ...
- 使用最新版(2020)IntelliJ IDEA 新建 Spring Boot 2.0 项目
使用最新版(2020)IntelliJ IDEA 新建 Spring Boot 2.0 项目 一:创建项目,并添加相应依赖 新建Project,并指定Project为Spring Initializr ...
- 狂神Spring Boot 员工管理系统 超详细完整实现教程(小白轻松上手~)
[SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...
- [JAVA EE]Spring Boot 控制层:参数传递方法
项目需要参照本人java ee系列前文建立,请读者自行前往本人主页查看. 当然,您自己创建一个项目也是可以的. bean包下的Student.java package com.example.demo ...
- spring boot中的日志入门
日志通常不会在需求阶段作为一个功能单独提出来,也不会在产品方案中看到它的细节.但是,这丝毫不影响它在任何一个系统中的重要地位. 报警系统与日志系统的关系 为了保证服务的高可用,发现问题一定要及时,定位 ...
- spring boot整合spring security笔记
最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...
最新文章
- laravel判断HTTP请求是否ajax
- tp连接mysql mysql_thinkphp学习简易教程(二) thinkphp连接读取MySQL数据库
- Java提高篇 —— Java关键字之final的几种用法
- 各种initcall的执行先后顺序(module_init、postcore_initcall、arch_initcall、subsys_initcall、 fs_initcall)【转】...
- [Usaco2009 Feb]Bullcow 牡牛和牝牛
- python利用什么写模块_python模块是什么?写法及作用分析
- 如何在 webpack 项目中使用绝对路径
- jQuery导入Eclipse后报错解决方法
- 2022跨年代码(有烟花)
- Lumerical官方案例、FDTD时域有限差分法仿真学习(一)——闪耀光栅(Blazed grating)
- HTTP 401和403区别
- TCP/IP Attack Lab(SEED实验)
- 谈谈创业这点事(3)
- 毕业设计 基于stm32的灯光控制系统 物联网
- 百万钱包借款时填资料一直显示服务器异常,百万钱包有连续放款失败,然后突然推过的吗?我已经失败四五...
- 01矩阵最大正方形面积、01矩阵最大矩形面积、直方图最大矩形面积
- 以上”和“以下 是否包含本身
- 数学物理方法·基础⑦基本初等复变函数的计算公式/方法
- OpenMAX编程-组件
- 【JAVA8】新时间API操作大全
热门文章
- MYSQL查询语句待优化_mysql语句查询优化
- ajxs跨域 php_php设置header头允许ajax跨域请求
- 安卓应用用户数据_Android手机用户注意,你要知道的中国十大安卓应用商店
- centos mysql pmm_【MySQL】MySQL监控利器PMM
- java map join_HashMap 常见应用:实现 SQL JOIN
- 批量生成数组_JavaScript【重温基础】13.迭代器和生成器
- 安卓模拟器运行python_利用python+Appium 之 如何在设备(模拟器)上自动安装并启动APP...
- signature=42f2498bc8fd40eb63568566c79f37e7,新思维综合英语Ⅰ学习指导
- oracle写入导出文件时出错,帮忙!EXP-00015:错误!EXP-00002: 写入导出文件时出错
- 一个sql题目, 统计每年每月的信息