如何扩展 Spring Boot 应用程序? [英] How can you scale a Spring Boot application?

查看:54
本文介绍了如何扩展 Spring Boot 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Spring Boot 有一个内置的 Tomcat 服务器(或 Jetty),可以促进快速开发.但是,当您因为流量增加而需要扩展应用程序时,您会怎么做?

I understand that Spring Boot has a built-in Tomcat server (or Jetty) which facilitates rapid development. But what do you do when you need to scale out your application because traffic has increased?

推荐答案

正如评论中所指出的,这里没有灵丹妙药,这取决于您的基础架构,并且有多种工具可以帮助您,您只需要选择最适合您的方式.

As pointed out in the comments, there is no silver bullet here, it depends on your infrastructure and there are several tools out there to help you, you only need to choose what works best for you.

对于负载平衡,您可以选择 Nginx 之类的东西,也可以将其留给 Spring Cloud,它还有许多其他方便的缩放/集群功能.扩展应该不是很困难,因为 spring boot 在它自己的服务器上运行.

For load balancing you can either choose something like an Nginx or leave it to spring cloud which also has a lot of other handy features for scaling/clustering. Scaling shouldn't be very hard because spring boot runs on it's own server.

一些有助于扩展/集群的工具:

Some tools that help with scaling/clustering:

Spring Boot 应用:

  • 如果你要扩展,你的应用必须接近无状态(例如:你不能有计划任务或类似的东西,因为当你扩展到 x 个实例时,它们被执行 x 次).

  • If you are going to scale, your app has to be near-stateless (e.g: you cannot have a scheduled task or something like that because when you scale to x instances, they are executed x times).

您可以使用 spring cloud 项目来获得额外的附加功能,例如服务发现和其他使扩展更容易的好处(例如:当您启动一个新实例时,它可以轻松地从配置服务器获取配置,注册"以减轻服务之间的负载平衡,具有类似集群的行为等...).

You can use the spring cloud project for extra added features like service discovery and other goodies that make scaling easier (e.g: When you spin up a new instance, it can get the config easily from a config server, 'register' to ease the loadbalancing between services, have cluster-like behaviour, etc...).

基础设施和容器:

  • Docker 在这里可以轻松启动您的应用程序及其副本(如果需要).如果您可以更进一步地使用资源并使用 Kubernetes,但这一切都取决于用例.

  • Docker is a no-brainer here to handle easy launching of your applications and their replicas, if needed. If you can go further with resources and go with Kubernetes but it all depends on the use case.

各种服务器(节点),以防其中一个出现故障并轻松分配负载.

Various servers (nodes), in case one of them fails and to easily distribute loads.

如果您还没有使用 Spring Cloud 完成任何工作,那么用于负载平衡的 Ngnix 非常简单.

Ngnix for load balancing is pretty straightforward if you already don't have something done with spring cloud.

数据库:

  • 您真的不想在这里使用 MySQL,因为它无法像您的 Spring 应用程序那样扩展.您可以选择 Cassandra 或 Redis 之类的东西,但这意味着重构您的数据模型.也许从 MySQL 到可扩展的 NoSQL 的最不痛苦的过渡是 MongoDB(恕我直言:Cassandra 表现更好).

日志记录:

  • 这可能是一场噩梦,但 Spring 也有解决方案.查看 zipkin 和 spring sleuth.
  • This can be a nightmare but spring also has a solution for this. Check out zipkin and spring sleuth.

此外,还有很多资源这里讨论了很多总体架构和尝试运行分布式服务时如何改变思维方式.

Also, there are a lot resources here that talk a lot about architecture in general and how it is necessary to change the mindset when trying to run distributed services.

希望这会有所帮助.

  • 今天,当我们谈论扩展时,Kubernetes 几乎是事实上的标准,并且是首选,因为它具有丰富的功能集,您将能够利用这些功能并将应用程序完全专注于业务领域逻辑,并且可以删除诸如 spring cloud 之类的东西以进行服务发现.如果可以使用 EKS 和 GKE 等一些公共云,则无需自己管理集群就更好了.它提供自动缩放和内置健康检查.从 Spring Boot 2.4 开始,在 K8s 上运行 Spring Boot 有许多额外的好处,例如用于活动和就绪探测的专用健康检查端点、正常关闭等......

  • Today, Kubernetes is pretty much a de-facto standard when we talk about scaling and is preferred because of the rich set of features that you will be able to leverage and focus your app purely on business domain logic and can remove things like spring cloud for service discovery. If you can use some public clouds like EKS and GKE, you are better off without having to manage the clusters by yourself. It provides autoscaling and built-in healthchecks. Starting from Spring Boot 2.4, you have many added benefits for running Spring Boot on K8s like dedicated healthcheck endpoints for liveness and readiness probes, graceful shutdown, etc....

在数据库方面,瞄准易于管理和扩展的东西,例如 AWS Aurora 或类似的东西.

On the database side, aim for something that is managed and scales easily such as AWS Aurora or similar.

在大规模管理 Spring Boot 服务时需要提及的一个重要事项可能是配置管理.一个非常有用的解决方案是 Consul.这将使您能够热重载配置,当您有 50 个服务需要重新启动仅更改一个布尔变量时,这一点很重要.根据您的应用程序的大小,启动可能会很昂贵,包括时间和 CPU/内存资源

An important thing to mention when managing spring boot services at scale is probably configuration management. A very useful solution that you can use out of the box is Consul. This will enable you to hot reload the configuration which is important when you have 50 services that you need to restart only to change one boolean variable. Depending on how big is your application, the startup can be costly, in terms of time as well as CPU/memory resources

这篇关于如何扩展 Spring Boot 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆