前言
上一篇內容講了如何整合 MongoDB,這裏繼續講如何操做 Redis 這個數據源,那什麼是 Reids?css
Redis 是一個高性能的 key-value 數據庫,GitHub 地址詳見這裏。GitHub 是這麼描述的:java
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.react
Redis 是內存式數據庫,存儲在磁盤,支持的數據類型不少:Strings、Lists、Sets、Sorted Sets、Hashes、HyperLogLogs、Bitmaps 等。git
安裝簡易教程(適用 Mac/Linux)
下載並解壓:github
下載安裝包 redis-x.x.x.tar.gz ## 解壓 tar zxvf redis-2.8.17.tar.gz
編譯安裝:web
cd redis-x.x.x/ make ## 編譯
啓動 Redis:redis
cd src/ redis-server
若是須要運行在守護進程,設置 daemonize 從 no 修改爲 yes,並指定運行:redis-server redis.conf。spring
結構
相似上面講的工程搭建,新建一個工程編寫此案例,工程如圖:sql
目錄核心以下:數據庫
- 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 實現緩存操做。