前言
上一篇內容講了如何整合 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 實現緩存操作。