如何在 Amazon 的 EC2 上运行 Java 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4867764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a Java server on Amazon's EC2?
提问by foxy
I want to place a server program written in Java on the cloud. It would accept TCP socket connections from clients (clients are android phones using 3G), do some computations, save stuff to a MySQL database (also on EC2), and send stuff back to the clients over the TCP connections. It may even be necessary to create several instances of the server (i.e. a process group).
我想在云端放置一个用Java编写的服务器程序。它将接受来自客户端的 TCP 套接字连接(客户端是使用 3G 的安卓手机),进行一些计算,将内容保存到 MySQL 数据库(也在 EC2 上),并通过 TCP 连接将内容发送回客户端。甚至可能需要创建服务器的多个实例(即进程组)。
Is this easy to do? I think I can make a AMI, but I'm not sure how to upload Java files, compile and run them, and create a MySQL database etc
这容易做到吗?我想我可以制作 AMI,但我不确定如何上传 Java 文件、编译和运行它们以及创建 MySQL 数据库等
Any help would be much appreciated.
任何帮助将非常感激。
回答by Taylor Leese
Take a look at using Amazon Elastic Beanstalk. Beanstalk is Amazon's PaaS offering and it will alleviate a lot of the system administration burden. Here's a quick description from their docs:
看看使用Amazon Elastic Beanstalk。Beanstalk 是亚马逊的 PaaS 产品,它将减轻很多系统管理负担。这是他们文档中的快速描述:
AWS Elastic Beanstalk is an even easier way for you to quickly deploy and manage applications in the AWS cloud. You simply upload your application, and Elastic Beanstalk automatically handles the deployment details of capacity provisioning, load balancing, auto-scaling, and application health monitoring.
AWS Elastic Beanstalk 是一种更简单的方法,可让您在 AWS 云中快速部署和管理应用程序。您只需上传您的应用程序,Elastic Beanstalk 就会自动处理容量供应、负载平衡、自动扩展和应用程序健康监控的部署细节。
Also, if you're interested in using MySQL then you should look at Amazon RDS. Again, this will alleviate the system administration burden for your database tier. Here's a quick description from their docs:
此外,如果您对使用 MySQL 感兴趣,那么您应该查看Amazon RDS。同样,这将减轻数据库层的系统管理负担。这是他们文档中的快速描述:
Amazon Relational Database Service (Amazon RDS) is a web service that makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while managing time-consuming database administration tasks, freeing you up to focus on your applications and business.
Amazon Relational Database Service (Amazon RDS) 是一项 Web 服务,可让您轻松地在云中设置、操作和扩展关系数据库。它提供经济高效且可调整大小的容量,同时管理耗时的数据库管理任务,让您腾出时间专注于您的应用程序和业务。
回答by Upgradingdave
Is this easy to do? I think I can make a AMI, ...
这容易做到吗?我想我可以做一个AMI,...
I think the answer depends on how comfortable you are with system administration in general. Creating a AMI to run in EC2 is really pretty much the same as creating a physical server or a VM image. You'll need to install an operating system, and then install tools, libraries and programs you need (like mysql, the jdk, ssh, etc).
我认为答案取决于您对系统管理的总体感觉。创建 AMI 以在 EC2 中运行实际上与创建物理服务器或 VM 映像几乎相同。您需要安装操作系统,然后安装您需要的工具、库和程序(如 mysql、jdk、ssh 等)。
You can save yourself a little work by using one of Amazon's pre-built AMI's http://aws.amazon.com/amis/. But, ultimately, you'll be responsible for all system administration of the server. If you've never built a server from the ground up, you have a pretty big learning curve ahead of you. It's not insurmountable, but just be warned that the devil is in the details; there's a ton of stuff you'll need to learn ;-)
您可以使用 Amazon 的预构建 AMI 之一http://aws.amazon.com/amis/为自己节省一些工作。但是,最终,您将负责服务器的所有系统管理。如果您从未从头开始构建服务器,那么您将面临相当大的学习曲线。这并非不可逾越,但请注意细节是魔鬼;你需要学习很多东西;-)
... but I'm not sure how to upload Java files, compile and run them, ...
...但我不确定如何上传 Java 文件、编译和运行它们,...
Once the server is setup and running in EC2, compiling them and running java files is just the same as compiling and running on your local. Normally, you probably want to compile and package your java app into a jar or war and then transfer that up to your EC2 server. If you install linux os on your EC2 server, you can use scp or a FTP client to transfer your files over sftp to move the files from your local up to the server. Once the latest files are up on your server, you can ssh to the server and start your app.
在 EC2 中设置并运行服务器后,编译它们并运行 java 文件与在本地编译和运行相同。通常,您可能希望将您的 Java 应用程序编译并打包到 jar 或 war 中,然后将其传输到您的 EC2 服务器。如果您在 EC2 服务器上安装 linux os,则可以使用 scp 或 FTP 客户端通过 sftp 传输文件,以将文件从本地移动到服务器。在您的服务器上启动最新文件后,您可以通过 ssh 连接到服务器并启动您的应用程序。
... and create a MySQL database etc ...
...并创建一个 MySQL 数据库等 ...
Installing mysql is going to be specific to the OS you choose to install on your server. For example, you can install mysql easily on Ubuntu with a command like:
安装 mysql 将特定于您选择在服务器上安装的操作系统。例如,您可以使用以下命令在 Ubuntu 上轻松安装 mysql:
sudo aptitude install mysql
Again, there will be more system-admin-type stuff to learn here specific to mysql databases.
同样,这里将有更多系统管理员类型的东西要学习特定于 mysql 数据库的内容。
So, it's definitely doable. An experienced sys admin could build a AMI instance pretty easily/quickly. If this is your first experience with system administration, I'd suggest finding an old Desktop you have lying around and try installing Ubuntu and all the required libraries and tools you need (mysql, jdk, ssh, etc..). Get your java program working on the old desktop and then it should be pretty easy to create an AMI from that. Then you can run your custom AMI on EC2 and will be set up.
所以,这绝对是可行的。经验丰富的系统管理员可以非常轻松/快速地构建 AMI 实例。如果这是您第一次使用系统管理,我建议您找一个旧桌面,然后尝试安装 Ubuntu 和您需要的所有必需库和工具(mysql、jdk、ssh 等)。让您的 Java 程序在旧桌面上运行,然后从中创建 AMI 应该很容易。然后您可以在 EC2 上运行您的自定义 AMI 并进行设置。
If you don't have a spare desktop lying around, you can use one of the Virtual Machine products like VMWare Player or Sun's VirtualBox and build a server instance on one of those.
如果您身边没有备用桌面,您可以使用 VMWare Player 或 Sun 的 VirtualBox 等虚拟机产品之一,并在其中之一上构建服务器实例。
If you want to avoid the hassle of managing the entire install of the Operating system, you might want to look at services like slicehost and/or linode instead of EC2. They give you ssh access to a pre-installed server. And it's as easy as clicking a button to install programs like mysql, etc.
如果您想避免管理整个操作系统安装的麻烦,您可能需要查看 slicehost 和/或 linode 等服务,而不是 EC2。它们使您可以通过 ssh 访问预安装的服务器。它就像单击一个按钮来安装 mysql 等程序一样简单。
Hope this answer is relevant and helpful, good luck. - Dave
希望这个答案是相关的和有帮助的,祝你好运。- 戴夫
回答by Romain Hippeau
If you can use Tomcat as your server - you might want to try Amazon Web Services Elastic BeanstalkIt will greatly simplify your task by providing an easy instance of Tomcat to deploy.
如果您可以使用 Tomcat 作为您的服务器 - 您可能想尝试使用Amazon Web Services Elastic Beanstalk它通过提供一个易于部署的 Tomcat 实例来极大地简化您的任务。
EDIT: AWS has a full section on how to develop using Java here: http://aws.amazon.com/java/
编辑:AWS 有关于如何使用 Java 进行开发的完整部分:http: //aws.amazon.com/java/
回答by Speck
How comfortable are you with remote administering a server and solving problems most people have never heard of? That's what you're talking about.
您对远程管理服务器和解决大多数人从未听说过的问题感到满意吗?这就是你所说的。
You'll create your EC2 instance, log into it and configure it like you would any other server you're working with. You can download JDKs to it, dbs etc. You might consider using a tool like Chef to help you. You'll use ftp and scp to copy files to the server.
您将创建您的 EC2 实例,登录它并像您正在使用的任何其他服务器一样对其进行配置。您可以将 JDK、dbs 等下载到它。您可以考虑使用像 Chef 这样的工具来帮助您。您将使用 ftp 和 scp 将文件复制到服务器。
You're probably going to want your Java server on one box and have it talk to a separate db server since you say you may want multiple servers.
您可能希望将 Java 服务器放在一个机器上,并让它与单独的数据库服务器通信,因为您说可能需要多台服务器。
Once the server is working the way you like it you can create an image of it to use to launch multiple instances, then configure a load balancer to point at your servers.
一旦服务器按照您喜欢的方式工作,您就可以创建它的映像以用于启动多个实例,然后配置负载均衡器以指向您的服务器。
If you can create a MySql db on your local box you can create it in the cloud. If not?....
如果您可以在本地机器上创建 MySql 数据库,则可以在云中创建它。如果不?....
回答by Ravi Tej Vupasi
Running Applications needs app. s/w to run them.
Apps such as Cyber Duck helps to upload files via SFTP.
I am successful in doing the same problem addressed.. Trust me, YOU CAN DO IT. All u need is interfaces for server services such as MySQL (use WorkBench to connect using key pair), terminal access (using PUTTY/SSH via MAC) and well you are good to go
运行应用程序需要应用程序。s/w 来运行它们。
Cyber Duck 等应用程序有助于通过 SFTP 上传文件。
我成功地解决了同样的问题。相信我,你可以做到。您所需要的只是服务器服务的接口,例如 MySQL(使用 WorkBench 使用密钥对进行连接)、终端访问(通过 MAC 使用 PUTTY/SSH),一切顺利
回答by Darshan Lila
I think to get started is to use a a whole unit packed into one archive.
我认为开始是使用一个完整的单元打包成一个档案。
Think of it as you have your Java files compiled, a embedded http server say tomcat
. Now all of this packed in a jar ready to be deployed on Amazon's EC2
. Use thislink for more on embedded servers and relevant code.
把它想象成你已经编译了你的 Java 文件,一个嵌入式 http 服务器说tomcat
。现在所有这些都打包在一个 jar 中,准备部署在Amazon's EC2
. 使用此链接了解有关嵌入式服务器和相关代码的更多信息。
As far as database is concerned, you can use Amazon's RDS. You can configure mysql on RDS and obtain a link to it for connection. Amazon RDSwould help you to get started with database.
数据库方面,可以使用亚马逊的RDS。您可以在 RDS 上配置 mysql 并获取指向它的链接以进行连接。Amazon RDS将帮助您开始使用数据库。
Now that you have everything ready to start and work with application. Now is the time to deploy on Amazon AMI
现在您已准备好开始和使用应用程序。现在是在 Amazon AMI 上部署的时候了
Perform Following steps to deploy you app on Amazon EC2:
执行以下步骤以在 Amazon EC2 上部署您的应用程序:
- Create a server instance on Amazon EC2 refer EC2 getting started.
- Receive Private key file i.e.
.pem
(receive from Step 1), this file would help you to login to your server instance and perform SFTP. - Use
Putty
or similarSSH client
to login to your EC2 server instance created on Step-1 using.pem
file and server's public DNS, refer Accessing Instancefor SSH login. - Transfer the archived files packed into jar to server using SFTP client,
WinnSCP
being one such client, refer SFTPfor more on it. - Run application. Note in case of jar file you can simply fire
java -jar TomcatApp.jar
.
- 在 Amazon EC2 上创建服务器实例,请参阅EC2 入门。
- 接收私钥文件,即
.pem
(从步骤 1 中接收),该文件将帮助您登录到您的服务器实例并执行 SFTP。 - 使用
Putty
或类似SSH client
方法使用.pem
文件和服务器的公共 DNS登录到在 Step-1 上创建的 EC2 服务器实例,请参阅访问实例以进行 SSH 登录。 - 使用 SFTP 客户端将打包成 jar 的存档文件传输到服务器,
WinnSCP
作为此类客户端之一,请参阅SFTP了解更多信息。 - 运行应用程序。请注意,如果是 jar 文件,您可以简单地触发
java -jar TomcatApp.jar
.
If everything is configured properly you would be to access the app using EC2 instance's public DNS
or public IP
, which would be of the form:http://<public_dns_address>:<port number>/servlet
如果一切配置正确,您将使用 EC2 实例的public DNS
or访问应用程序public IP
,其形式如下:http://<public_dns_address>:<port number>/servlet
Hope it helps you to get started and provide you an overall view.
希望它可以帮助您入门并为您提供全局视图。