如何使用REST API和数据库扩展Java应用程序? [英] How do I scale a Java app with a REST API and a Database?

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

问题描述

我有一个典型的无状态Java应用程序,它提供一个REST API并在Postgresql数据库中执行更新(CRUD)。



然而,客户端数量正在增长,感觉需要




  • 增加冗余,如果一个失败,另一个失败


  • 不会淹没网络和只有一个服务器的CPU,从而提高响应速度(但是,负载均衡器如何不被淹没?)

  • 也许我需要分发数据库?

  • 我想能够无缝更新我的应用程序(我看到一个叫做kubernetes的东西做这个):Kill每个冗余节点一个一个,并立即将其替换为更新的版本

  • 我的应用程序还存储一些图像文件,磁盘大小增长快,我需要能够分发他们

  • 所有这些都必须是可备份的



这是我现在的图Java应用程序和数据库都在同一个服务器上):





缩放的最好/正确方法是什么?



谢谢!

解决方案

网络服务器:



在多台服务器上运行应用程序,使用AWS Elastic Beanstalk或使用EC2 + Autoscaling Groups + ELB卷自己的解决方案。



您提到了对负载均衡器洪水的担忧,但如果您使用Amazon Elastic Load Balancer服务,它会自动扩展以处理您获得的任何流量,因此您不必担心此问题。



数据库服务器:



将数据库移动到RDS并启用多个az故障转移。这将创建一个热备用服务器,如果您的主服务器有问题,您的数据库将自动故障切换到。 (可选)添加只读副本以横向扩展数据库容量。



如果您尚未开始在Redis中缓存数据库查询。有些插件可以很容易地使用Hibernate。如果您的应用程序定期执行相同的查询,这将占用数据库服务器的巨大负载。对您的Redis服务器使用AWS ElastiCache或RedisLabs。



图片



停止在您的Web服务器上存储图像文件!这产生了许多可扩展性问题。将它们移动到S3并直接从S3提供服务​​。 S3提供了无限的存储空间,自动备份,以及直接从S3服务图像的能力,这减少了您的Web服务器的负载。



部署: / strong>



这里有这么多的解决方案,它只是成为一个问题,人们喜欢哪种方法。如果您使用Elastic Beanstalk,那么它为部署提供了一个解决方案。如果你不使用EB,那么有数百个解决方案从中挑选。我建议您首先设计您的环境,然后选择一个可与您设计的环境配合使用的自动部署解决方案。



备份: / p>

如果你这样做,你不应该在你的网络服务器上备份。使用Elastic Beanstalk,您重建Web服务器所需要的是您检入Git的代码和配置文件。如果您最终必须备份EC2服务器,您将需要查看EBS快照。



对于数据库备份,RDS将自动执行每日备份。如果您想要在RDS之外备份,您可以使用pg_dump和cron作业自行安排这些备份。



对于映像,您可以启用S3版本控制和多区域复制。 >

CDN:



您没有提及,但您应该查看CDN 。这将允许您的应用程序更快地提供服务,同时减少您的服务器上的负载。 AWS提供了CloudFront CDN,我还建议您查看CloudFlare。


I have a typical stateless Java application which provides a REST API and performs updates (CRUD) in a Postgresql Database.

However the number of clients is growing and I feel the need to

  • Increase redundancy, so that if one fails another takes place
  • For this I will probably need a load balancer?
  • Increase response speed by not flooding the network and the CPU of just one server (however how will the load balancer not get flooded?)
  • Maybe I will need to distribute the Database?
  • I want to be able to update my app seamlessly (I have seen a thingy called kubernetes doing this): Kill each redundant node one by one and immediately replace it with an updated version
  • My app also stores some image files, which grow fast in disk size, I need to be able to distribute them
  • All of this must be backup-able

This is the diagram of what I have now (both Java app and DB are on the same server):

What is the best/correct way of scaling this?

Thanks!

解决方案

Web Servers:

Run your app on multiple servers, behind a load balancer. Use AWS Elastic Beanstalk or roll your own solution with EC2 + Autoscaling Groups + ELB.

You mentioned a concern about "flooding" of the load balancer, but if you use Amazon's Elastic Load Balancer service it will scale automatically to handle whatever traffic you get so that you don't need to worry about this concern.

Database Servers:

Move your database to RDS and enable multi-az fail-over. This will create a hot-standby server that your database will automatically fail-over to if there are issues with your primary server. Optionally add read replicas to scale-out your database capacity.

Start caching your database queries in Redis if you aren't already. There are plugins out there to do this with Hibernate fairly easily. This will take a huge load off your database servers if your app performs the same queries regularly. Use AWS ElastiCache or RedisLabs for your Redis server(s).

Images:

Stop storing your image files on your web servers! That creates lots of scalability issues. Move those to S3 and serve them directly from S3. S3 gives you unlimited storage space, automated backups, and the ability to serve the images directly from S3 which reduces the load on your web servers.

Deployments:

There are so many solutions here that it just becomes a question about which method someone prefers. If you use Elastic Beanstalk then it provides a solution for deployments. If you don't use EB, then there are hundreds of solutions to pick from. I'd recommend designing your environment first, then choosing an automated deployment solution that will work with the environment you have designed.

Backups:

If you do this right you shouldn't have much on your web servers to backup. With Elastic Beanstalk all you will need in order to rebuild your web servers is the code and configuration files you have checked into Git. If you end up having to backup EC2 servers you will want to look into EBS snapshots.

For database backups, RDS will perform a daily backup automatically. If you want backups outside RDS you can schedule those yourself using pg_dump with a cron job.

For images, you can enable S3 versioning and multi-region replication.

CDN:

You didn't mention this, but you should look into a CDN. This will allow your application to be served faster while reducing the load on your servers. AWS provides the CloudFront CDN, and I would also recommend looking at CloudFlare.

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

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