本次和大家分享的是在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. JavaScript单线程和异步机制

    随着对JavaScript学习的深入和实践经验的积累,一些原理和底层的东西也开始逐渐了解.早先也看过一些关于js单线程和事件循环的文章,不过当时看的似懂非懂,只留了一个大概的印象:浏览器中的js程序时 ...

  2. 最新的爬虫工具requests-html

    使用Python开发的同学一定听说过Requsts库,它是一个用于发送HTTP请求的测试.如比我们用Python做基于HTTP协议的接口测试,那么一定会首选Requsts,因为它即简单又强大.现在作者 ...

  3. pyc

    当运行一个高级程序的时候,需要一个翻译机把高级语言变成计算机能读懂的机器语言的过程.这个过程分为两类: 编译 在程序执行之前,先通过编译器对程序执行一个编译的过程,把程序变成机器语言,运行时就不需要翻 ...

  4. Django中模板过滤器总结

    一.形式:小写: {{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签: {{ my_text|escape|linebreaks } 三.过滤 ...

  5. STL--multiset用法

    multiset: multiset<int>s; 定义正向迭代器与正向遍历: multiset<int>::iterator it; for(it=s.begin();it! ...

  6. privoxy自动请求转发到多个网络

    有些时候我们需要通过不同的代理访问不同资源,比如某些ip或域名走本地网络,某些ip或域名走不可描述的代理等.当然这只是举个栗子! 我要解决的问题是:我的内网机器没有internet访问权限,但是我的应 ...

  7. hello spring春天来了

    这是小小小零食  废话不多说 现在看到 spring这个管家 先说第一个 ioc :在每个框架的中都有一个容器的概念,所谓的容器就是将常用的服务,封装起来,然后,用户只需要遵循一定的规则,就可以达到统 ...

  8. Java Servlet 2.5 设置 cookie httponly

    Servlet 3.0 有 cookie.setHttpOnly(true); 多么人性化, Servlet 2.5 是没有这个方法的要这个曲线救国:cookie.setPath("; Ht ...

  9. ASP.NET Core 实现带认证功能的Web代理服务器

    引言 最近在公司开发了一个项目,项目部署架构图如下: 思路 如图中文本所述,公司大数据集群不允许直接访问外网,需要一个网关服务器代理请求,本处服务器A就是边缘代理服务器的作用. 通常技术人员最快捷的思 ...

  10. mysql报错mmap(137428992 bytes) failed; errno 12,Cannot allocate memory for the buffer pool

    mysql以`systemctl start mysqld.service`的方式启动一段时间后发现突然无法启动,尝试重新启动也不能解决问题,排查问题时,先后通过`systemctl status m ...