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是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- 最近最久未使用页面淘汰算法———LRU算法(java实现)
请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. LRU算法,即Last Recently Used ---选择最后一次访问时间距离当前时间最长的一页并淘汰之--即淘汰最长时间没有使用的页 按照 ...
- QM3_Statistics Concepts and Market Returns
Basic Concepts Terms Descriptive Statistics Describes the important aspects of large data sets. 统计 概 ...
- 查询订阅某topic的所有consumer group(Java API)
在网上碰到的问题,想了下使用现有的API还是可以实现的. 首先,需要引入Kafka服务器端代码,比如加入Kafka 1.0.0依赖: Maven <dependency> <grou ...
- Github管理自己的代码-远程篇
一.名词解释 Git Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版 ...
- Dubbo中集群Cluster,负载均衡,容错,路由解析
Dubbo中的Cluster可以将多个服务提供方伪装成一个提供方,具体也就是将Directory中的多个Invoker伪装成一个Invoker,在伪装的过程中包含了容错的处理,负载均衡的处理和路由的处 ...
- Elasticsearch笔记三之版本控制和插件
版本控制 1:关系型数据库使用的是悲观锁,数据被读取后就被锁定其他的线程就无法对其进行修改. 2:ex使用的是乐观锁,数据被读取后其他程序还可以对其进行修改,而执行修改时发现此数据已经被修改则修改就会 ...
- 【bzoj 1407】【Noi2002】Savage
Description Input 第1行为一个整数N(1<=N<=15),即野人的数目. 第2行到第N+1每行为三个整数Ci, Pi, Li表示每个野人所住的初始洞穴编号,每年走过的洞穴 ...
- 【Unity游戏开发】Lua中的os.date和os.time函数
一.简介 最近马三在工作中经常使用到了lua 中的 os.date( ) 和 os.time( )函数,不过使用的时候都是不得其解,一般都是看项目里面怎么用,然后我就模仿写一下.今天正好稍微有点空闲时 ...
- HTTP VISUAL HTTP请求可视化工具、HTTP快照工具(公测)
先啰嗦几句,最近工作比较忙,再加上自己又开设了一个小站(简单点),没时间写博客,都快憋坏了,趁着周末有时间,抓紧来一篇~ HTTP VISUAL是一款HTTP可视化工具,它可以记录HTTP请求,包括请 ...
- Python基础练习题100例(Python 3.x)
1:题目:有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源 ...