有時,為了提升整個網站的性能,我們會将經常需要通路資料緩存起來,這樣,在下次查詢的時候,能快速的找到這些資料。
緩存的使用與系統的時效性有着非常大的關系。當我們的系統時效性要求不高時,則選擇使用緩存是極好的。當系統要求的時效性比較高時,則并不适合用緩存。
本文,我們将示範如何通過內建 Redis 伺服器來進行資料的緩存,以提高微服務的并發通路能力。
天氣資料接口,本身時效性不是很高,而且又因為是 Web 服務,在調用過程中,本身是存在延時的。是以,采用緩存,一方面可以有效減輕通路天氣接口服務帶來的延時問題,另一方面,也可以減輕天氣接口的負擔,提高并發通路量。
特别地,我們是使用的第三方免費的天氣 API,這些 API 往往對使用者的調用次數及頻率有一定的限制。是以為了減輕天氣 API 提供方的負荷,我們并不需要實時去調用其第三方接口。
在<code>micro-weather-basic</code>的基礎上,我們建構了一個<code>micro-weather-redis</code>項目,作為示例。
為了示範本例子,需要采用如下開發環境:
JDK 8
Gradle 4.0
Spring Boot Web Starter 2.0.0.M4
Apache HttpClient 4.5.3
Spring Boot Data Redis Starter 2.0.0.M4
Redis 3.2.100
Spring Boot Data Redis 提供了 Spring Boot 對 Redis 的開箱即用的功能。在原有的依賴的基礎上,添加 Spring Boot Data Redis Starter 的依賴。
安裝後,Redis 預設運作在
<a href="http://localhost:8080/weather/cityId/101280601"></a>
<a href="https://waylau.com/spring-boot-weather-report/"></a>
<a href="https://github.com/waylau/spring-boot-tutorial"></a>
<a href="https://github.com/waylau/spring-cloud-tutorial"></a>
<a href="https://waylau.com/spring-boot-blog-video-release/"></a>
<a href="https://waylau.com/spring-cloud-video-release/"></a>
<a href="https://waylau.com/spring-boot-weather-report-with-redis/"></a>