第06课:WebFlux 整合 Redis

前言

上一篇内容讲了如何整合 MongoDB,这里继续讲如何操作 Redis 这个数据源,那什么是 Reids?

Redis 是一个高性能的 key-value 数据库,GitHub 地址详见这里。GitHub 是这么描述的:

Redis is an in-memory database that persists on disk. The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, HyperLogLogs, Bitmaps.

Redis 是内存式数据库,存储在磁盘,支持的数据类型很多:Strings、Lists、Sets、Sorted Sets、Hashes、HyperLogLogs、Bitmaps 等。

安装简易教程(适用 Mac/Linux)

下载并解压:

下载安装包 redis-x.x.x.tar.gz
## 解压
tar zxvf redis-2.8.17.tar.gz

编译安装:

cd redis-x.x.x/
make ## 编译

启动 Redis:

cd src/
redis-server

如果需要运行在守护进程,设置 daemonize 从 no 修改成 yes,并指定运行:redis-server redis.conf。

结构

类似上面讲的工程搭建,新建一个工程编写此案例,工程如图:

目录核心如下:

  • pom.xml maven 配置
  • application.properties 配置文件
  • domain 实体类
  • controller 控制层,本文要点

新增 POM 依赖与配置

在 pom.xml 配置新的依赖:

    <!-- Spring Boot 响应式 Redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

类似 MongoDB 配置,在 application.properties 配置连接 Redis:

## Redis 配置
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=5000

默认 密码为空,这里注意的是连接超时时间不能太少或者为 0,不然会引起异常 RedisCommandTimeoutException: Command timed out。

对象

修改 org.spring.springboot.domain 包里面的城市实体对象类,城市(City)对象 City,代码如下:

import org.springframework.data.annotation.Id;

import java.io.Serializable;

/**
* 城市实体类
*
*/
public class City implements Serializable { private static final long serialVersionUID = -2081742442561524068L; /**
* 城市编号
*/
@Id
private Long id; /**
* 省份编号
*/
private Long provinceId; /**
* 城市名称
*/
private String cityName; /**
* 描述
*/
private String description; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public Long getProvinceId() {
return provinceId;
} public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
} public String getCityName() {
return cityName;
} public void setCityName(String cityName) {
this.cityName = cityName;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
}
}

值得注意点:

  • @Id 注解标记对应库表的主键或者唯一标识符。因为这个是我们的 DO,数据访问对象一一映射到数据存储。
  • City 必须实现序列化,因为需要将对象序列化后存储到 Redis。如果没实现 Serializable,会引出异常:java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type。
  • 如果不是用默认的序列化,需要自定义序列化实现,只要实现 RedisSerializer 接口去实现即可,然后在使用 RedisTemplate.setValueSerializer 方法去设置你实现的序列化实现,支持 JSON、XML 等。

控制层 CityWebFluxController

代码如下:

import org.spring.springboot.domain.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono; import java.util.concurrent.TimeUnit; @RestController
@RequestMapping(value = "/city")
public class CityWebFluxController { @Autowired
private RedisTemplate redisTemplate; @GetMapping(value = "/{id}")
public Mono<City> findCityById(@PathVariable("id") Long id) {
String key = "city_" + id;
ValueOperations<String, City> operations = redisTemplate.opsForValue();
boolean hasKey = redisTemplate.hasKey(key);
City city = operations.get(key); if (!hasKey) {
return Mono.create(monoSink -> monoSink.success(null));
}
return Mono.create(monoSink -> monoSink.success(city));
} @PostMapping()
public Mono<City> saveCity(@RequestBody City city) {
String key = "city_" + city.getId();
ValueOperations<String, City> operations = redisTemplate.opsForValue();
operations.set(key, city, 60, TimeUnit.SECONDS); return Mono.create(monoSink -> monoSink.success(city));
} @DeleteMapping(value = "/{id}")
public Mono<Long> deleteCity(@PathVariable("id") Long id) {
String key = "city_" + id;
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
redisTemplate.delete(key);
}
return Mono.create(monoSink -> monoSink.success(id));
}
}

代码详解:

  • 使用 @Autowired 注入 RedisTemplate 对象,这个对象和 Spring 的 JdbcTemplate 功能十分相似,RedisTemplate 封装了 RedisConnection,具有连接管理、序列化和各个操作等,还有针对 String 的支持对象 StringRedisTemplate。
  • 删除 Redis 某对象,直接通过 key 值调用 delete(key)。
  • Redis 操作视图接口类用的是 ValueOperations,对应的是 Redis String/Value 操作,get 是获取数据;set 是插入数据,可以设置失效时间,这里设置的失效时间是 60 s。
  • 还有其他的操作视图,ListOperations、SetOperations、ZSetOperations 和 HashOperations。

运行工程

一个操作 Redis 工程就开发完毕了,下面运行工程验证一下,使用 IDEA 右侧工具栏,单击 Maven Project Tab,单击使用下 Maven 插件的 install 命令。或者使用命令行的形式,在工程根目录下,执行 Maven 清理和安装工程的指令:

cd springboot-webflux-6-redis
mvn clean install

在控制台中看到成功的输出:

... 省略
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:30 min
[INFO] Finished at: 2018-10-15T10:00:54+08:00
[INFO] Final Memory: 31M/174M
[INFO] ------------------------------------------------------------------------

在 IDEA 中执行 Application 类启动,任意正常模式或者 Debug 模式,可以在控制台看到成功运行的输出:

... 省略
2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)

打开 POST MAN 工具,开发必备。进行下面操作:

新增城市信息 POST http://127.0.0.1:8080/city

获取城市信息 GET http://127.0.0.1:8080/city/2

如果等待 60s 以后,再次则会获取为空,因为保存的时候设置了失效时间是 60 s。

总结

这里探讨了 Spring WebFlux 的如何整合 Redis,介绍了如何通过 RedisTemplate 去操作 Redis。因为 Redis 在获取资源性能极佳,常用 Redis 作为缓存存储对象,下面我们利用 Reids 实现缓存操作。

代码在 GiHub 上

Spring Boot WebFlux-06——WebFlux 整合 Redis的更多相关文章

  1. spring boot(三)整合 redis

    Spring boot 集成redis 为什么要用redis,它解决了什么问题? Redis 是一个高性能的key-value内存数据库.它支持常用的5种数据结构:String字符串.Hash哈希表. ...

  2. Spring Boot 知识笔记(整合Redis)

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  4. spring boot与jdbcTemplate的整合案例2

    简单入门了spring boot后,接下来写写跟数据库打交道的案例.博文采用spring的jdbcTemplate工具类与数据库打交道. 下面是搭建的springbootJDBC的项目的总体架构图: ...

  5. Spring Boot 中使用 MyBatis 整合 Druid 多数据源

    2017 年 10 月 20 日   Spring Boot 中使用 MyBatis 整合 Druid 多数据源 本文将讲述 spring boot + mybatis + druid 多数据源配置方 ...

  6. 阿里P7级教你如何在Spring Boot应用程序中使用Redis

    在Spring Boot应用程序中使用Redis缓存的步骤: 1.要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-s ...

  7. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  8. Spring Boot数据访问之整合Mybatis

    在Mybatis整合Spring - 池塘里洗澡的鸭子 - 博客园 (cnblogs.com)中谈到了Spring和Mybatis整合需要整合的点在哪些方面,需要将Mybatis中数据库连接池等相关对 ...

  9. Spring Boot 2.0 WebFlux 教程 (一) | 入门篇

    目录 一.什么是 Spring WebFlux 二.WebFlux 的优势&提升性能? 三.WebFlux 应用场景 四.选 WebFlux 还是 Spring MVC? 五.异同点 六.简单 ...

  10. 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web

    Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...

随机推荐

  1. Mac FTP工具推荐-Transmit

    Transmit 是专为mac用户设计的一款功能强大的FTP客户端,Transmit5 mac兼容于FTP,SFTP和TLS/SSL协议,提供比Finder更加迅速的iDisk账户接入.与此同时,用户 ...

  2. java集合类介绍

    目录 集合类简介 List ArrayList LinkedList Vector Stack Set HashSet LinkedHashSet TreeSet Map HashMap Hashta ...

  3. google 谷歌Python语言规范

    Python语言规范 https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_lan ...

  4. 附: Python爬虫 数据库保存数据

    原文 1.笔记 #-*- codeing = utf-8 -*- #@Time : 2020/7/15 22:49 #@Author : HUGBOY #@File : hello_sqlite3.p ...

  5. 基于多主机的Web服务

    [Centos7.4版本] !!!测试环境我们首关闭防火墙和selinux [root@localhost ~]# systemctl stop firewalld [root@localhost ~ ...

  6. 分布式存储ceph---部署ceph(2)

    一.部署准备 准备5台机器(linux系统为centos7.6版本),当然也可以至少3台机器并充当部署节点和客户端,可以与ceph节点共用: 1台部署节点(配一块硬盘,运行ceph-depoly) 3 ...

  7. python基础之流程控制(if判断和while、for循环)

    程序执行有三种方式:顺序执行.选择执行.循环执行 一.if条件判断 1.语句 (1)简单的 if 语句 (2)if-else 语句 (3)if-elif-else 结构 (4)使用多个 elif 代码 ...

  8. 如何查看自己的电脑 CPU 是否支持硬件虚拟化

    引言 在你安装各种虚拟机之前,应该先测试一下自己的电脑 CPU 是否支持硬件虚拟化. 如果你的电脑比较老旧,可能不支持硬件虚拟化,那么将无法安装虚拟机软件. 如何查看自己 CPU 是否支持硬件虚拟化 ...

  9. 11.8 iotop:动态显示磁盘I/O统计信息

    iotop命令是一款实时监控磁盘I/O的工具,但必须以root用户的身份运行.使用iotop命令可以很方便地查看每个进程使用磁盘I/O的情况. 最小化安装系统一般是没有这个命令的,需要使用yum命令额 ...

  10. 网上的说TB6560存在的问题

    https://www.amobbs.com/thread-5506456-2-1.html