springboot + redis(单机版)
本次和大家分享的是在springboot集成使用redis,这里使用的是redis的jedis客户端(这里我docker运行的redis,可以参考 docker快速搭建几个常用的第三方服务),如下添加依赖:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
然后需要redis的相关配置(这里我的redis密码是空),在application.yml设置如:
spring:
redis:
single: 192.168.146.28:
jedis:
pool:
max-idle:
max-active:
max-wait:
timeout:
password:
这是redis的一般配置,具体调优可以设置这些参数,下面在JedisConfig类中读取这些设置:
@Value("${spring.redis.single}")
private String strSingleNode;
@Value("${spring.redis.jedis.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.jedis.pool.max-active}")
private Integer maxActive;
@Value("${spring.redis.jedis.pool.max-wait}")
private Integer maxAWait;
@Value("${spring.redis.timeout}")
private Integer timeout;
@Value("${spring.redis.password}")
private String password;
有上面的配置,就需要有代码里面设置下,这里创建一个返回JedisPoolConfig的方法
/**
* jedis配置
*
* @return
*/
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(maxIdle); #最大空闲数
config.setMaxWaitMillis(maxAWait); #最大等待时间
config.setMaxTotal(maxActive); #最大连接数
return config;
}
有了配置,接下来就创建JedisPool,这里把JedisPool托管到spring中
/**
* 获取jedispool
*
* @return
*/
@Bean
public JedisPool getJedisPool() {
JedisPoolConfig config = getJedisPoolConfig();
System.out.println("strSingleNode:" + this.strSingleNode);
String[] nodeArr = this.strSingleNode.split(":"); JedisPool jedisPool = null;
if (this.password.isEmpty()) {
jedisPool = new JedisPool(
config,
nodeArr[],
Integer.valueOf(nodeArr[]),
this.timeout);
} else {
jedisPool = new JedisPool(
config,
nodeArr[],
Integer.valueOf(nodeArr[]),
this.timeout,
this.password);
}
return jedisPool;
}
上面简单区分了无密码的情况,到此jedis的配置和连接池就基本搭建完了,下面就是封装使用的方法,这里以set和get为例;首先创建个JedisComponent组件,代码如下:
/**
* Created by Administrator on 2018/8/18.
*/
@Component
public class JedisComponent { @Autowired
JedisPool jedisPool; public boolean set(String key, String val) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.set(key, val).equalsIgnoreCase("OK");
} finally {
if (jedis != null) {
jedis.close();
}
}
} public <T> boolean set(String key, T t) {
String strJson = JacksonConvert.serilize(t);
if (strJson.isEmpty()) {
return false;
}
return this.set(key, strJson);
} public String get(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
return jedis.get(key);
} finally {
if (jedis != null) {
jedis.close();
}
}
} public <T> T get(String key, Class<T> tClass) {
String strJson = this.get(key);
return JacksonConvert.deserilize(strJson, tClass);
}
}
有了对jedis的调用封装,我们在Controller层的测试用例如下:
@Autowired
JedisComponent jedis; @GetMapping("/setJedis/{val}")
public boolean setJedis(@PathVariable String val) {
return jedis.set("token", val);
} @GetMapping("/getJedis")
public String getJedis() {
return jedis.get("token");
}
运行set和get的接口效果如:


springboot + redis(单机版)的更多相关文章
- Redis单机版以及集群版的安装搭建以及使用
1,redis单机版 1.1 安装redis n 版本说明 本教程使用redis3.0版本.3.0版本主要增加了redis集群功能. 安装的前提条件: 需要安装gcc:yum install g ...
- 十分钟搭建redis单机版 & java接口调用
本次单机版redis服务器搭建采用的包为redis-3.0.0.tar.gz,主要是记录下安装的心得,不喜勿喷! 一.搭建redis服务器单机版 1.上传redis-3.0.0.tar.gz到服务器上 ...
- Redis单机版安装
1.工具简单介绍 1.博主使用的是Xshell工具 ps:需要设置端口和连接名称,端口一般默认为22,需要的童鞋可以自行百度 2.Redis单机版安装 第一步:安装gcc编译环境 yum instal ...
- linux下redis单机版搭建
1.1.什么是redis Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如下: ...
- 补习系列(14)-springboot redis 整合-数据读写
目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...
- SpringBoot+Redis整合
SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...
- springboot +redis配置
springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- const命令,全局变量的属性,变量的解构赋值
const命令 1:声明常量(只在当前代码块中有效)---注意声明的常量可以是对象,但是常量储存的是对象的地址,地址声明后不可变,但是可以给常量对象添加属性 全局变量的属性 1:window和glob ...
- Latex数学公式中的空格表示方法
两个quad空格 a \qquad b 两个m的宽度 quad空格 a \quad b 一个m的宽度 大空格 a\ b 1/3m宽度 中等空格 a\;b 2/7m宽度 小空格 a\,b 1/6m宽度 ...
- Java - Instrumentation
使用JRebel启动工程时加上VM参数时有一个参数是"-javaagent:D:\jrebel_5.6.0\jrebel.jar". javaagent是什么? java -hel ...
- buffer_pool.go
package nsqd import ( "bytes" "sync" ) var bp sync.Pool func init() { ...
- layer的删除询问框的使用
删除是个很需要谨慎的操作 我们需要进行确认 对了删除一般使用ajax操作 因为如果同url请求 处理 再返回 会有空白页 1.js自带的样式 <button type="button& ...
- MyBatis新手教程(一)
MyBatis本是apache的一个开源项目iBatis,2010年这个项目由apache 迁移到了 google,并改名为MyBatis,2013年迁移到Github. MyBatis是一个优秀的持 ...
- java游戏开发杂谈 - 游戏物体
现实生活中,有很多物体,每个物体的长相.行为都不同. 物体存在于不同的空间内,它只在这个空间内发生作用. 物体没用了,空间就把它剔除,不然既占地方,又需要花精力管理. 需要它的时候,就把它造出来,不需 ...
- 『简单dp测试题解』
这一次组织了一场\(dp\)的专项考试,出了好几道经典的简单\(dp\)套路题,特开一篇博客写一下题解. Tower(双向dp) Description 信大家都写过数字三角形问题,题目很简单求最大化 ...
- 理解ASP.NET Core 依赖注入
目录: 一.什么是依赖注入 1.1.什么是依赖? 1.2. 什么是注入? 1.3.依赖注入解决的问题 二.服务的生命周期(.Net Core DI) 三.替换默认服务容器 3.1.为什么替换默认服务容 ...
- Celery异步调度框架(二)与Django结合使用
配置Celery与Django结合 需要安装的插件 # 用于在Django中执行任务 pip install django-celery-beat # 这个是把任务执行结果保存到django-orm中 ...