Java Development 2.0: You can also hire ec2--to create efficient, scalable, leased infrastructure in a flexible and low-cost way

Source: Internet
Author: User
Keywords nbsp can select application
Tags .mall .url access address app application applications browser

In this issue of Java Development 2.0, Andrew Glover describes how to develop and deploy for Amazon elastic Compute Cloud (EC2). Learn about the differences between EC2 and Google App Engine and how to quickly build and run simple WEB applications on EC2 using the Eclipse plug-in and the concise Groovy language.

You may have heard of the Standish Chaos report, which reports annually on successful and unsuccessful IT projects. The report often contains a lot of bad news. (If all good news, it should not be called the Chaos report, right?) The 2009 report noted that 44 of IT projects had problems for a variety of reasons. One reason is that the project is beyond budget. It is easy to think of several reasons that lead to a project exceeding budget. For example, a programmer's pay is not low. Infrastructure is also one of the factors that affect costs.

As noted in the first article in this series ("Using Google App Engine"), one of the key aspects of Java™ Development 2.0 is that software producers now have the option of not maintaining a high-cost internal infrastructure, but using another alternative. A few years ago, several sensible companies began to use the "commercialization" of hardware (and related software), which built a system of large, inexpensive machines in which the entire infrastructure could continue to function even if some machines failed. These companies, such as Google and Amazon, have huge infrastructure to lend to people like me (while keeping their core business running smoothly). So the concept of cloud computing has now become a reality. If you are budgeting for your IT projects, consider whether it is cheaper to rent your infrastructure from Google or Amazon than to buy your own hardware for your own (and your company's benefit).

EC2 differences

Google and Amazon have a very different lease infrastructure. As you can see in the previous article, Google App Engine is more like a platform for developing Java WEB applications. You cannot use any of the tools and frameworks available in the Java domain, such as Hibernate. For example, you can only use Google's big Table. But the advantage is that you can extend the resident solution to any size you need. (Remember, Google will charge you when your Google App Engine site reaches a bandwidth or space limit.)

Amazon EC2 does not offer a development platform, more like a general-purpose infrastructure service, that provides virtual machines (which can be based on linux®, OpenSolaris, or Windows®) and can run any program on those virtual machines. EC2 is not free, but it is much more flexible than Google App Engine. Whenever you can create or lease a virtual machine, you can run any Java application (including Java applications that use Hibernate). (Amazon and its community provide a complete preconfigured virtual machine directory, which is called an image). Like Google app Engine, it can be very efficient to extend applications to global users, but the location and scale of deployment applications is largely Amazon-controlled. (Amazon also allows you to select several geographic areas to deploy your application).

Amazon provides relevant Eclipse plug-ins to help create applications that can use this platform. In contrast to the use of the Google App Engine plug-in, EC2 flexibility can easily lead to complications. For example, by using Amazon's EC2 Eclipse plug-in, it is easy to deploy Apache Tomcat Web applications (excluding databases) to a configurable cluster using EC2. However, if you want to implement a solution other than this basic use case, such as using a MySQL instance already deployed on EC2 or using Amazon's SimpleDB, you will need to expend more effort.

In this article, I'll explain how to quickly and easily create a simple application that uses Groovlet on Amazon infrastructure via Eclipse. In addition to developing machines, you do not need any hardware, and you do not need to pay license fees for any software that will be used. (A very small portion of the cost must be paid for hosting the code on Amazon's infrastructure).

Getting Started with EC2

EC2 is a cheap
.

Use "small instances"-images used to run simple applications that do not use a lot of space or memory-charge 10 cents per hour. The bandwidth cost is about 30 cents per 1GB of data sent and received. You can also "rent" an IP address and charge 1 cents per hour (but keep it for about two days).

To use EC2, you must create an Amazon Web Services (AWS) account. If you are not a registered Amazon customer, you will need to provide a credit card number-EC2 is not free (but the registration is free). After registering, you receive an access key ID and Secret access key, which is required for safe use of EC2.

Amazon recommends using Eclipse 3.5 for the AWS for Eclipse plug-in, otherwise you will need to install some components that are default in this latest Eclipse release. Because you want to create and deploy a Web application, I recommend using the Eclipse IDE for Java EE developers, which contains a nice Java EE perspective that supports building Web applications.

Installing the AWS Plug-in Kit

As with other eclipse Plug-ins, to install Amazon's AWS plug-in, you must find the latest version of the plug-in in Eclipse with Help > Install new Software. After the Install dialog box opens, enter Http://aws.amazon.com/eclipse in the Work with text box. The list of available plug-ins appears below the text box, as shown in Figure 1. Select the entire AWS Toolkit for Eclipse option, and then click OK. Eclipse installs this plug-in after accepting the license agreement. It is likely that Eclipse will need to be restarted.


Figure 1. Download AWS Plugin

This Eclipse plug-in allows you to specify the target runtime environment for a WEB application. For this article, you will use Tomcat 6, so you need to download and install Tomcat 6 first. Although the WEB application will be deployed to EC2, and EC2 runs a Tomcat server cluster, it still wants to test and run the application locally because it is free.

Creating a Groovy Web application

Before you begin to demonstrate how to configure EC2, create a simple Web application with Groovy, my favorite fast-developing language. As I mentioned in "Using Google App Engine," Groovy is essentially the Java language, but the syntax isn't that complicated. For example, the typical Hello world servlet has about 6 to 10 lines of Java code (depending on whether to compute the import statement, and so on). Regardless of the number of lines of code, I can guarantee that the Hello world servlet written in groovy is shorter, in fact, the simplest servlet that can be written in groovy has only one line of code.

In the Eclipse Java EE perspective, right-click in Project Explorer and select New > Dynamic Web Project. If you haven't created a project before, you don't have to worry that-eclipse can differentiate between static WEB projects (using HTML pages) and dynamic projects (with server-side resources such as servlet).

After you select the Dynamic Web Project option, a dialog box appears where you can configure various aspects of the project, such as the target runtime environment and the Servlet version. Be sure to select Tomcat 6. (You may also need to tell Eclipse where to install Tomcat 6). The configuration options remain the default settings.

You can choose to save the location of the source code file. I usually keep it as src directory unchanged. Finally, you must tell Eclipse up and down root-the name that will be used in the Web application's URI. Be sure to select a simple name. You also need to select a name for the Web content folder, where static files and Groovlet are saved.

Click Finish to install the plugin.

Set Groovy dependencies

To use Groovy, you have to make two changes. First, add the Groovy-all JAR to the project's Classpath (the version is 1.6.4 when you write this article). You also need to update the Web.xml files in the INF directory. This file maps Web requests to groovlet-if you follow Java Development 2.0: Use Google App Engine practice, you should have done this step. The Web.xml file should look like Listing 1:


Listing 1. Support for Groovlet web.xml files

<servlet> <servlet-name>GroovyServlet</servlet-name> <servlet-class> Groovy.servlet.groovyservlet</servlet-class></servlet><servlet-mapping> <servlet-name> Groovyservlet</servlet-name> <url-pattern>*.groovy</url-pattern></servlet-mapping>

Now, you can write something in Groovy.

Write Groovlet

The simplest groovlet has only one line, and it uses println to output "Hello world". We'll add some spice with some HTML code. Groovy supports the concept of Builder, Builder is a logical mechanism for defining hierarchical data structures. You can use Groovy's Builder to easily mimic markup languages such as XML and HTML, or even a GUI that mimics frameworks such as Swing. You can quickly create HTML tags without having to process HTML (no angle brackets are required!). )。 The simplest use of HTML for groovlet uses Groovy's Markupbuilder, in which case Groovlet exists as an HTML variable. For example, listing 2 gives a simple Hello world Groovlet using HTML:


Listing 2. Very concise Groovlet

html.html {head {title (' Hello from EC2 ')} body {p (' Hello from Ec2! '}}}

So much for the code. Just a few lines of code generates well-formed HTML with a <head> element, followed by <title>, then a <body> element and a paragraph, too simple. Create the file Ec2groovlet.groovy, where you enter the code in Listing 2. Save this file in the Web content directory.

Want to create XML? It's easy. Don't want the view to mix with the controller? No problem, you can use Groovyserver page (GSP) or JavaServer page (JSP). Whichever way you prefer, it's easy to write a servlet in Groovy. Now look at how easy it is to use a servlet on EC2.

As a last step, create a simple index.html file in the context root of your project. This file will be handy later, because eclipse accesses URLs that are up and down root when accessing EC2 in Eclipse, and if index.html files are not found, you receive an error page from Tomcat. Create a Groovlet relative link in index.html.

For example, my index.html is like Listing 3:


Listing 3. Simple index.html pointing to Groovlet

&lt;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" &gt;&lt;html&gt; &lt;head&gt; &lt;meta http-equiv= "Content-type" content= html; Charset=utf-8 "&gt; &lt;title&gt;insert any old title here!&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt;&lt;a href = "Ec2groovlet.groovy" &gt;groovy is slick!&lt;/a&gt;&lt;/p&gt; &lt;/body&gt;&lt;/html&gt;

The easy work is done. Now look at EC2.

Configure EC2

It is easy to configure EC2 through the Eclipse plug-in. First, open the Amazon EC2 perspective and enter your credentials. Next, open Window > View > Servers. You need to define at least one server, but we'll define two servers: one for free testing locally and the other for Amazon's infrastructure. Right-click and select New > Server.

Enter Local in the Select the server type text box. You will see a few choices (see Figure 2). Select the Tomcat v6.0 Server option (assuming you have already downloaded Tomcat and configured the runtime environment correctly in Eclipse). Click Next to see options for adding available resources (that is, items) to this instance. Select your item and click the Add button. Click Finish to complete. You can now automatically publish your Web project to a local instance of Tomcat.


Figure 2. Define local server

On the Servers console, select the local Tomcat instance, right-click and select Start. After Tomcat starts, Eclipse deploys your code (that is, your groovlet). When you start your Web browser and access the Localhost:8080/your context Name/ec2groovlet.groovy, you should see the HTML page generated by groovy Builder. If everything is okay, try it on Amazon's infrastructure.

Deploy and test with AWS

You may now be afraid to deploy this application on EC2. In fact, this is almost as easy as a local deployment. First, go back to the Servers console, right-click and select New. Then, enter EC2 and see the Amazon EC2 Tomcat v6.0 Cluster option shown in Figure 3. Select it and click Next.


Figure 3. Defining EC2 Servers

In the next dialog box (see Figure 4), you can select the number of instances you want the cluster to use. At the moment, keep 1-you can expand at any time. You can choose a custom Amazon Machine image (AMI) or accept the default image. The default image is currently used; another AMI can be added to additional packages, such as databases. For instance types, select Small-if you prefer, you can choose a larger instance (but remember that you must pay).


Figure 4. EC2 Preferences

Amazon has shown that it attaches great importance to security, and the next two options (see Figure 4) are related to the security of the Tomcat cluster. For now, simply right-click in the key Pair area and select New Key Pair to create the default security settings. You must define a name and select the key pair that was installed when registering EC2.

Next, you can create groups in the security group box. (This is just creating groups; in another view, you can actually use them). Right-click and add a new group, specifying its name. The default setting allows all people to access port 8080, which can now be set by default.

Finally, you can configure the IP address. This is handy if you finally decide to use EC2 for a long time because the default URI for the WEB application is quite long and hard to remember. Currently accept the address provided by EC2. But don't worry-you can always map your domain name to the IP address provided by Amazon.

Click the Next button to associate your project (and Groovlet) with the EC2 server. Now it's ready to deploy!

As before, right click on the Amazon EC2 server that you just defined and start it, as shown in Figure 5:


Figure 5. Starting and stopping servers

This time, startup may take a little more time because startup is performed remotely. When the server is running, select your Web project and select the Run as > run on server option. Next, select the Amazon Tomcat cluster that has been started and click Finish. Doing so opens an embedded browser in Eclipse, which accesses your context root by default-there are index.html files, and the files contain groovlet links.

Note the URL in the Browser location window. You can see my URL in Figure 6. URLs can be mapped to more logical addresses at any time. Click the Groovlet link, and the Groovy program will show again-this time, the program is running on a server somewhere you don't know!


Figure 6. EC2 is groovy!.

Click the link several times to realize that running programs on Amazon's infrastructure is as easy as running on your local machine. Then, be sure to stop the server-if you don't, you'll have to pay for it. Stopping the server is the same as starting. Right-click your instance in the Servers console and select Stop.


EC2 is Java Development 2.0

Java Development 2.0 is mainly influenced by two factors:

leverages open source tools and frameworks to build the application infrastructure that the entire application leases (or borrows) to manage the lifecycle of the software (including running the application itself)

As you can see in this article, the two aspects of Java Development 2.0 are implemented by using EC2. You can make the most of any software you like on your borrowed infrastructure (remember that Amazon's virtual machine can run anything), and it costs far less than you buy your hardware resources. Not only that, you can set up the EC2 very efficiently, extending to very large scale, far more than you can achieve at the same cost.

The next issue of Java Development 2.0 will delve into the EC2 and introduce a virtual machine (or AMI) that supports running Tomcat and MySQL. You'll learn how to deploy a Java WEB application that supports Spring and Hibernate (currently the two most popular Java open source frameworks), and you'll see the application of the Java Development 2.0 rationale again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.