Java EE Cloud application deployment with Amazon EC2

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
{{Cookbook}} {{Cloud}}
 
{{Cookbook}} {{Cloud}}
  
 
+
(Updating to 4.0.27 in progress...)
 
This tutorial is going to cover the basics of using the Resin Java application server with Amazon Web Services for cloud deployment. If you are new to cloud computing and IaaS ([http:///en.wikipedia.org/wiki/Cloud_computing#Infrastructure Infrastructure as a Service]), follow along and you will soon be deploying Java web applications in the cloud. You wil create an EC2 instance. You will download and install Resin on Ubuntu on a local machine. You will install Resin on an Amazon Linux AMI instance (EC2 instance). You will use Roo to create a simple application and deploy it.
 
This tutorial is going to cover the basics of using the Resin Java application server with Amazon Web Services for cloud deployment. If you are new to cloud computing and IaaS ([http:///en.wikipedia.org/wiki/Cloud_computing#Infrastructure Infrastructure as a Service]), follow along and you will soon be deploying Java web applications in the cloud. You wil create an EC2 instance. You will download and install Resin on Ubuntu on a local machine. You will install Resin on an Amazon Linux AMI instance (EC2 instance). You will use Roo to create a simple application and deploy it.
  
Line 67: Line 67:
  
 
$ ls -l /usr/local/share/resin
 
$ ls -l /usr/local/share/resin
lrwxrwxrwx 1 root root 16 2011-11-18 09:03 /usr/local/share/resin -> resin-pro-4.0.26
+
lrwxrwxrwx 1 root root 16 2012-03-22 18:42 /usr/local/share/resin -> resin-pro-4.0.27
 +
 
  
 
</pre></code>
 
</pre></code>
Line 74: Line 75:
 
Also at this point, Resin should be running as well, and you can verify this with resinctl status:
 
Also at this point, Resin should be running as well, and you can verify this with resinctl status:
  
<code> <pre>
+
<pre>
 
$ resinctl status
 
$ resinctl status
Resin/4.0.25 status for watchdog at 127.0.0.1:6600
+
</pre>
 +
 
 +
'''Output'''
 +
 
 +
<pre>
 +
Resin/4.0.27 status for watchdog at 127.0.0.1:6600
  
 
watchdog:
 
watchdog:
   watchdog-pid: 2587
+
   watchdog-pid: 18597
  
 
server 'app-0' : ACTIVE
 
server 'app-0' : ACTIVE
   password: ok
+
   password: missing
 
   watchdog-user: root
 
   watchdog-user: root
   user: resin(resin)
+
   user: www-data(www-data)
 
   root: /var/www
 
   root: /var/www
 
   conf: /etc/resin/resin.xml
 
   conf: /etc/resin/resin.xml
   pid: 2809
+
   pid: 18692
   uptime: 4 days 02h21
+
   uptime: 0 days 00h13
  
</pre></code>
+
</pre>
  
 
== Install Maven 3 (Optional, for Roo example) ==
 
== Install Maven 3 (Optional, for Roo example) ==

Revision as of 00:00, 26 March 2012

Cookbook-48.pngCloud-48.png

(Updating to 4.0.27 in progress...) This tutorial is going to cover the basics of using the Resin Java application server with Amazon Web Services for cloud deployment. If you are new to cloud computing and IaaS (Infrastructure as a Service), follow along and you will soon be deploying Java web applications in the cloud. You wil create an EC2 instance. You will download and install Resin on Ubuntu on a local machine. You will install Resin on an Amazon Linux AMI instance (EC2 instance). You will use Roo to create a simple application and deploy it.

We use Roo because Spring is fairly widely used, and Roo is a quick way to generate a sample app. Future tutorials will use other common Java tools as well as show you how to configure and manage a complete Resin server cluster. Think of this as the first tutorial in a series of tutorials.

For this tutorial you will need Resin 4.0.26 or later. Check back periodically because as we are going to expand the tutorial and improve Resin's support of cloud deployments. The Resin engineering team plans on improving cloud support continuously.

Many of the steps in this tutorial would be similar even if you were using Eucalyptus, CloudStack with CloudBridge, RightScale myCloud or even OpenNebula this guide should help you along as they all support the Amazon EC2 REST APIs. Also any cloud computing environment (private or public, on premises or hosted) will have similar characteristics. Thus even if you are using a private cloud using OpenStack like Project Olympus, the principles will be the same. In fact even using remote servers deployed in a datacenter or virtualized servers with Xen Server, Xen Cloud or VMWare vSphere the steps will be very similar.


For this tutorial we expect you are familiar with starting, and stopping Amazon WS instances. If you are not, go through this tutorial from Amazon WS. You will need an Amazon WS account. Amazon WS allows you to have free tier so you can learn Amazon WS (EC2, S3, Elastic Load Balancer, Block Storage, SimpleDB, Simple Queue Service, Simple Notification Service).


Contents

Fire up an Amazon Instance

Fire up an Amazon Instance using Amazon Linux (basically CentOS 5.4). Then install Resin as follows using RPM:



$ sudo rpm --import http://caucho.com/download/rpm/RPM-GPG-KEY-caucho
$ sudo yum install http://caucho.com/download/rpm/4.0.24/x86_64/resin-pro-4.0.26-1.x86_64.rpm


Install Resin on a Local Box

Install Resin locally so you test locally and deploy. Feel free to install it on any supported OS.


Add Caucho's Debian repository to your system's repositories to use automated update tools like Synaptic and apt-get. To do this, add the following line to your /etc/apt/sources.list

deb http://caucho.com/download/debian unstable multiverse


Then install Resin as follows:

$ sudo apt-get update
$ sudo apt-get install resin-pro

It is a good idea to make sure the version you install on Amazon AMI matches the version you install locally.

You can see that Resin is installed as follows:

$ ls /etc/resin
app-default.xml      health.xml  resin.properties
cluster-default.xml  resin-inf   resin.xml


$ ls /var/www
doc  licenses  log  resin-data  watchdog-data  webapps

$ ls /etc/init.d/resin
/etc/init.d/resin

$ ls -l /usr/local/share/resin
lrwxrwxrwx 1 root root 16 2012-03-22 18:42 /usr/local/share/resin -> resin-pro-4.0.27



Also at this point, Resin should be running as well, and you can verify this with resinctl status:

$ resinctl status

Output

Resin/4.0.27 status for watchdog at 127.0.0.1:6600

watchdog:
  watchdog-pid: 18597

server 'app-0' : ACTIVE
  password: missing
  watchdog-user: root
  user: www-data(www-data)
  root: /var/www
  conf: /etc/resin/resin.xml
  pid: 18692
  uptime: 0 days 00h13

Install Maven 3 (Optional, for Roo example)

If you already have a test .war file, you can skip to the deploy section and deploy your own war.

Install maven. Maven is not in the public Ubuntu repository.

$ sudo add-apt-repository "deb http://build.discursive.com/apt/ lucid main"
$ sudo apt-get update
$ sudo apt-get install maven

Create a symbolic link:

$ sudo ln -s /usr/local/maven/bin/mvn /usr/local/bin/mvn


Install Spring Roo (Optional)

If you already have a test .war file, you can skip to the deploy section and deploy your own war.

Download and install Spring Roo using directions at the Spring Roo site.

This is how I install roo:

$ unzip spring-roo-1.2.0.RC1.zip
$ sudo mv spring-roo-1.2.0.RC1 /usr/local/share/
$ sudo ln -s /usr/local/share/spring-roo-1.2.0.RC1/ /usr/local/share/spring-roo
$ sudo ln -s /usr/local/share/spring-roo/bin/roo.sh /usr/bin/roo

At the roo prompt, type hint.


$ roo
    ____  ____  ____  
   / __ \/ __ \/ __ \ 
  / /_/ / / / / / / / 
 / _, _/ /_/ / /_/ /  
/_/ |_|\____/\____/    1.2.0.RC1 [rev dcaa483]


Welcome to Spring Roo. For assistance press TAB or type "hint" then hit ENTER.
...
roo> hint

..

Hint should always let you know what you might want to do next. Please feel free to read the Roo docs for details or just follow along and read the Roo docs later.

Create the project, this generates maven project files and Spring starter project.


roo> project --topLevelPackage com.example.blog

Created ROOT/pom.xml
Created SRC_MAIN_RESOURCES
Created SRC_MAIN_RESOURCES/log4j.properties
Created SPRING_CONFIG_ROOT
Created SPRING_CONFIG_ROOT/applicationContext.xml

Next setup the database and JPA, keep it simple to minimize the amount of things we need to configure later.


roo> jpa setup --database H2_IN_MEMORY --provider HIBERNATE 

Created SPRING_CONFIG_ROOT/database.properties
Updated SPRING_CONFIG_ROOT/applicationContext.xml
Created SRC_MAIN_RESOURCES/META-INF/persistence.xml
Updated ROOT/pom.xml [added dependencies com.h2database:h2:1.3.161, org.hibernate:hibernate-core:3.6.8.Final, ...]

Create a entity class called blog.


roo> entity jpa --class ~.domain.Blog
Created SRC_MAIN_JAVA/com/example/blog/domain
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog.java
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_Configurable.aj
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_Jpa_ActiveRecord.aj
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_ToString.aj
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_Jpa_Entity.aj


Add two fields to the Blog class.

~.domain.Blog roo> field string --fieldName title
Updated SRC_MAIN_JAVA/com/example/blog/domain/Blog.java
Created SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_JavaBean.aj

~.domain.Blog roo> field string --fieldName message
Updated SRC_MAIN_JAVA/com/example/blog/domain/Blog.java
Updated SRC_MAIN_JAVA/com/example/blog/domain/Blog_Roo_JavaBean.aj

Create the web tier

~.domain.Blog roo> web mvc setup
Created ROOT/src/main/webapp/WEB-INF/spring
Created ROOT/src/main/webapp/WEB-INF/spring/webmvc-config.xml
Created ROOT/src/main/webapp/WEB-INF/web.xml
...
Updated ROOT/src/main/webapp/WEB-INF/web.xml
Updated ROOT/pom.xml [added dependencies org.springframework:spring-webmvc:${spring.version}, ...]
Updated SRC_MAIN_WEBAPP/WEB-INF/views/footer.jspx


Generate the web tier.

~.domain.Blog roo> web mvc all --package ~.web

Created SRC_MAIN_JAVA/com/example/blog/web
Created SRC_MAIN_JAVA/com/example/blog/web/BlogController.java
...
Created SRC_MAIN_WEBAPP/WEB-INF/views/blogs
Created SRC_MAIN_WEBAPP/WEB-INF/views/blogs/views.xml
Updated SRC_MAIN_WEBAPP/WEB-INF/views/blogs/views.xml
...
Updated SRC_MAIN_WEBAPP/WEB-INF/views/blogs/views.xml
...
Created SRC_MAIN_WEBAPP/WEB-INF/views/blogs/update.jspx

Package our web app

roo> perform package


Exit roo.

roo> exit

Deploy locally and test

Deploy the example Roo app with Resin.


$ resinctl deploy --name blog target/blog-0.1.0.BUILD-SNAPSHOT.war

Deployed production/webapp/default/blog from target/blog-0.1.0.BUILD-SNAPSHOT.war to hmux://127.0.0.1:6800



You can see if the app was actually deployed:

$ resinctl deploy-list

production/webapp/default/blog

Now you should be able to load the Roo blog example.

http://localhost:8080/blog/


Create an Elastic IP and assign it to your Amazon WS Linux instance

Create an Elastic IP and assign it to the Amazon WS Linux instance using the Amazon web console.

I got this IP address.

23.21.195.83

In order to use it, you must assign it to our instance that has Resin.


Remember after you restart the server, you need to reassign it the Amazon Elastic IP. This means go to the console. Find the list of Elastic IPs. Select the one you want. Click the Associate Address button and pick the instance ID of the server that has Resin.


Create password

$ resinctl generate-password -user admin -password roofoo

admin_user : admin
admin_password : {SSHA}2fGGuMzKVkeR2z03J/hoVFqOHWNTw999

Create a properties file and put it into user-data of Amazon AMI

Create a file that looks like this.

[http://caucho.com/resin-4.0/changes/resin-4.0.24.xtp#resinproperties Resin properties] file format details.


admin_user : admin
admin_password : {SSHA}2fGGuMzKVkeR2z03J/hoVFqOHWNTw999 
web_admin_enable : true
remote_cli_enable : true


To put the above in the user-data of that instance, you typically need to stop the instance from the console or command line. Then from the Amazon Management Console, select the instance in the instances list, then select the action "View/Change User Data" and add the contents above. In a later lesson, we show you how to do this from the command line.

Remember after you restart the server, you need to reassign it the Amazon Elastic IP. This means go to the console. Find the list of Elastic IPs. Select the one you want. Click the Associate Address button and pick the instance ID of the server that has Resin.



Create an amazon.xml on the Amazon instance with the following content.


<resin xmlns="http://caucho.com/ns/resin"
      xmlns:resin="urn:java:com.caucho.resin">

  <resin:properties path="http://169.254.169.254/latest/user-data"
                    optional="true" recover="true"/>

</resin>

The IP address (http://169.254.169.254) is a meta address that Xen, and Amazon EC2 use to configure per instance data about the instance you are running. Later we will put the resin.properties file that we created in the Amazon instances user data.


Log into Amazon AMI instance and add the above to /etc/resin/resin-inf/amazon.xml.


$ ssh -i resin2.pem ec2-user@23.21.195.83 
$ sudo nano /etc/resin/resin-inf/amazon.xml

XML files that you add to /etc/resin/resin-inf get added to the resin configuration. This allows you add just the additional config you need without changing the rest on the resin configuration.

The Amazon secuirty group that this instance is in needs to open up the 8080, and 8443, 80, and 443.

From your local development machine execute this command:


$ resinctl deploy --name blog --address 23.21.195.83 --port 8080 --user
admin --password roofoo  target/blog-0.1.0.BUILD-SNAPSHOT.war

You can see if the deploy worked by do the following:

$ resinctl deploy-list --address 23.21.195.83 --port 8080 --user admin --password roofoo

production/webapp/default/blog


Now it works in the cloud:

http://23.21.195.83:8080/blog/


Next step learn how to spin up instances with eucatools Resin Cloud deployment with Amazon WS EC2 and Euca Tools.


Next step learn how to use MySQL setup with Resin and Amazon RDS Java EE Setup, JDBC datasource, JNDI, MySQL and MySQL setup with Amazon RDS/EC2.


Resin Cloud Deployment Reference

Personal tools
TOOLBOX
LANGUAGES