配置Azure Redis缓存以提高Spring Boot性能(7/7)

此博客文章是“在Azure上部署Spring Boot和Angular应用程序”系列文章的一部分,这里是文章的完整列表:

Scaling out with Azure Web Apps

Since the beginnings of this series, we have used the "scale up" option of Azure Web Apps. As we have seen on part 3, we are able to change the instance that we use on-the-fly, so we can scale up (or down) depending on our load and budget. This is also why we set up Azure Application Insights on part 4, in order to better understand our needs.

现在,一种更好的扩展解决方案是“向外扩展”:我们将添加更多的实例,而不是用更大的实例代替我们的实例。 这样可以实现更大的可伸缩性(在某些时候您无法购买更大的实例...),并且由于我们仅自动运行所需数量的实例,因此更具成本效益。

为此,Azure提出了一个“横向扩展”选项,该选项位于“纵向扩展”选项的下方,您可以在其中设置要横向扩展的规则。 例如,这里我们定义了一个规则:当70%的CPU使用超过10分钟时,我们将自动启动新实例,最多20个实例:

此设置将允许我们的应用程序根据我们的工作量自动按比例放大(和缩小)。 但这只是问题的一部分。

The database is the problem

横向扩展非常棒,但是大多数时候性能问题都来自数据存储。 数十年来,我们一直致力于解决这一问题,这就是JHipster团队如此专注于使用缓存的原因,因为这是消除此数据存储问题的最佳方法。

让我们看一下我们在数据存储性能方面的选择:

  • We can buy a more expensive instance for our data store: of course this will work, at least for some time, like the "scale up" mechanism we studied with Azure Web App. The main issue here is that we wanted to be budget-conscious, and a quick check on the prices of high-end database instances will make you look for other solutions.
  • We can use NoSQL databases, typicpally CosmosDB. CosmosDB is a distributed, very efficient database that can scale out automatically. This is a great option, but it will require to change our current database and to depend on Azure: this is not the topic of this series, but be sure we will do a specific CosmosDB post in the future, as it's a very exciting technology.
  • Use the Hibernate 2nd level cache: this is the caching mechanism included inside Hibernate, and it is extremely powerful when used correctly. This would be our recommended solution, however the issue here is that we are going to scale out (see previous section), and that requires us to have a distributed cache.
  • Use the Spring Cache abstraction: it is a very simple mechanism, yet very powerful, that allows you to cache method calls. This can be more powerful than the Hibernate 2nd level cache when used correctly, as it works at the business level, so it can cache more complex objects. As for Hibernate, this will require us to use a distributed caching solution.

最后两个选项显然是最适合我们的用例的,但是它们都要求我们有一个分布式缓存解决方案,否则,一旦我们的Azure Web App实例扩展后,我们将看到一些无效的数据。

Caching options and issues

在Java中,我们通常有3种缓存:

  • 本地JVM缓存:它们是最快的,因为获取数据通常仅使用指向该数据的指针,该指针可直接在内存中使用。 但是它们从JVM堆中获取内存:随着缓存的增长,JVM的垃圾收集器将传递越来越多的麻烦,从而导致糟糕的应用程序性能。堆外缓存:它们在JVM旁边的另一个进程中运行。 因此他们不使用网络,但是他们要求数据在两个进程之间移动时要进行序列化/非序列化,这是非常昂贵的。 因此,此缓存比本地JVM缓存要慢得多,但是它可以增长到千兆字节的数据而不会影响JVM。远程缓存:它们类似于堆外缓存,但在另一台服务器上运行。 通常,这将允许具有更大的缓存(因为它们是在特定的高内存实例上设置的),但由于需要网络访问,因此会变慢。

存在几种众所周知的解决方案,并且通常由JHipster支持:

  • Ehcache, which unfortunately doesn't have an API to scale out (you can't add new nodes once the cluster is created).
  • Hazelcast and Infinispan, which can scale using an API, but which require a mechanism so new nodes register to the existing cluster. This is what JHipster provides with the JHipster Registry.

在这里,我们将使用Redis,Redis是一种众所周知的内存数据存储,通常用作缓存。 与Hazelcast和Infinispan相比,它具有一些独特的选项:

  • It is only used as a "remote cache", so it can store more data, and it will not pollute your JVM, but it will be slower for very frequently-used data.
  • As a managed service, it will "scale out" automatically, removing the need for something like the JHipster Registry to have nodes register in the cluster.
  • It is fully Open Source, so you can use it to store huge amount of data without needing to pay for the "enterprise version".

What is Azure Cache for Redis

一种zure Cache for Redis is a fully-managed Redis cache, hosted by Azure.

It has a very inexpensive first tier, as you can get a cache for about $16 per month, and of course it can grow to a huge, distributed cache with geo-replication if you spend more money. You can find all pricing details here.

对于我们当前的用例,这是一个很好的选择,因为我们尝试着精打细算,并且可以进行扩展而不会给我们带来任何麻烦。

If you want more information, here is the complete documentation on Azure Cache for Redis.

Setting up an Azure Cache for Redis is easy using the Azure portal, just use the search box to create it (and be careful to select to correct instance, as prices can go up quickly!):

Redis with the Hibernate 2nd level cache and the Spring Cache Abstraction

使用Redis,有几个Java库可用于支持Hibernate 2级缓存和Spring Cache抽象:

  • Redisson supports both Hibernate 2nd level cache and the Spring Cache abstraction. If you want to unlock its best features, you will need to buy the "pro" version, but the Open Source edition is already enough for most needs.
  • Jedis is a very well-known Redis client, that only supports the Spring Cache abstraction. It is often seen as the "de facto" standard Redis client in Java.
  • Lettuce is the default library used in Spring Boot, and only supports Spring Cache. It is based on Netty and is thread-safe (unlike Jedis), so it can arguably support more connections and is more performant.

在本文中,我们将使用Lettuce,因为它是Spring Boot的默认选项,并且确实确实是最好的完全开源选项。

Configuring Spring Boot and Azure Cache for Redis

首先,我们添加了弹簧启动启动器数据重新分配我们的图书馆pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

然后,我们添加了特定的Spring Boot配置类,因此Redis仅在生产模式下工作。 在开发模式下,没有缓存就可以正常工作,这将使我们的开发设置更加容易:

package io.github.jdubois.bugtracker.config;

import io.github.jhipster.config.JHipsterConstants;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@EnableCaching
@Profile(JHipsterConstants.SPRING_PROFILE_PRODUCTION)
public class CacheConfiguration {
}

然后我们在我们的配置Redis应用程序产品Spring Boot配置文件:

spring:
  cache:
    type: redis
  redis:
    ssl: true
    host: spring-on-azure.redis.cache.windows.net
    port: 6380
    password: Y27iYghxMcY1qoRVyjQkZExS8RaHdln4QfLsqHRNsHE=

重要的提示 :许多人使用不安全的Redis实例,通常是因为他们的Redis库不支持SSL。 上面的Yaml配置向您展示了如何正确执行此操作:请注意,用于Redis的Azure缓存使用端口6300进行SSL,并且默认情况下禁用了它的非安全端口(但是现在没有借口启用它!)。

为了测试我们的代码,我们还在REST方法的项目资源类:

@GetMapping("/projects")
@Cacheable("projects")
public List<Project> getAllProjects() {
    log.error("REST request to get all Projects");
    return projectRepository.findAll();
}

这仅用于测试,因为您很快就会在此代码中遇到缓存失效问题(您需要使用@CacheEvict注释以具有有效的缓存),但这将使您轻松测试缓存是否正常运行。

Once this is done, you should be able to monitor your cache through the Azure portal:

All changes done for this blog post are available on this commit.

from: https://dev.to//azure/configuring-azure-redis-cache-to-boost-spring-boot-performance-7-7-52dl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值