天天看点

Spring Boot 使用 Redis 提升天气预报应用的并发访问能力

有时,为了提升整个网站的性能,我们会将经常需要访问数据缓存起来,这样,在下次查询的时候,能快速的找到这些数据。

缓存的使用与系统的时效性有着非常大的关系。当我们的系统时效性要求不高时,则选择使用缓存是极好的。当系统要求的时效性比较高时,则并不适合用缓存。

本文,我们将演示如何通过集成 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>