本次和大家分享的是在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(单机版)的更多相关文章

  1. Redis单机版以及集群版的安装搭建以及使用

    1,redis单机版 1.1   安装redis n  版本说明 本教程使用redis3.0版本.3.0版本主要增加了redis集群功能. 安装的前提条件: 需要安装gcc:yum install g ...

  2. 十分钟搭建redis单机版 & java接口调用

    本次单机版redis服务器搭建采用的包为redis-3.0.0.tar.gz,主要是记录下安装的心得,不喜勿喷! 一.搭建redis服务器单机版 1.上传redis-3.0.0.tar.gz到服务器上 ...

  3. Redis单机版安装

    1.工具简单介绍 1.博主使用的是Xshell工具 ps:需要设置端口和连接名称,端口一般默认为22,需要的童鞋可以自行百度 2.Redis单机版安装 第一步:安装gcc编译环境 yum instal ...

  4. linux下redis单机版搭建

    1.1.什么是redis Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库.它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如下: ...

  5. 补习系列(14)-springboot redis 整合-数据读写

    目录 一.简介 二.SpringBoot Redis 读写 A. 引入 spring-data-redis B. 序列化 C. 读写样例 三.方法级缓存 四.连接池 小结 一.简介 在 补习系列(A3 ...

  6. SpringBoot+Redis整合

    SpringBoot+Redis整合 1.在pom.xml添加Redis依赖 <!--整合Redis--> <dependency> <groupId>org.sp ...

  7. springboot +redis配置

    springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  8. 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能

    springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...

  9. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

随机推荐

  1. 1. 开篇-springboot环境搭建

    最初学习strurs2时,虽然觉得也挺好用的,但也有一些不便的地方:1. 模型绑定必须要在Action中声明对应模型的成员变量:2. Action中对外提供调用的接口必须明确注明:3. 要声明一大堆的 ...

  2. html总结01

    lesson01~lesson02基础 <!DOCTYPE html> <html lang="en"> <!-- ################# ...

  3. Python 列表list

    列表list: [ ] 类似Java中的数组. 通过索引可以取到具体位置上的值. names = ["ZhangYang","WangGui","Li ...

  4. DNS Server Centos 7

    1.安裝服務 #yum update –y #yum install bind –y #systemctl  start named             開啟服務named #systemctl ...

  5. (4)STM32使用HAL库实现串口通讯——理论讲解

    一.查询模式 1. 二.中断模式 1.中断接收. 1.1先看中断接收的流程(以 USART2 为例) 在启动文件中找到中断向量 USART2_IRQHandler 找到USART2_IRQHandle ...

  6. com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after co ...

  7. k8s实践 - 如何优雅地给kong网关配置证书和插件。

    前言 从去年上半年微服务项目上线以来,一直使用kong作为微服务API网关,整个项目完全部署于k8s,一路走来,对于k8s,对于kong,经历了一个从无到有,从0到1的过程,也遇到过了一些坎坷,今天准 ...

  8. 兄弟俩畅游Tomcat城市的SpringMVC科技园区

    Tomcat城市 Tomcat这座城市的历史相当悠久了,经历过几次大的变迁后,呈现出非常明显的地域特征. 从城市往西走,过了城乡结合部以后,可以说是满目疮痍.一片破败,这就是Servlet地区,这座城 ...

  9. Tomcat 8.0的并发优化 - 优化server.xml的配置

    目录 1 Tomcat的3种运行模式 1.1 BIO - 同步阻塞IO模式 1.2 NIO - 同步非阻塞IO模式 1.3 APR - 可移植运行时模式 2 Tomcat的并发配置(配置Connect ...

  10. 从mysql中拿到的数据构造为列表

    最近测试接口遇到一个问题,用python2.7从mysql中取到的数据是元祖类型的,元祖内部的元素也是一个元祖(并且部分元素的编码格式是unicode的): 类似这样: ((10144, u''), ...