Resin as a PaaS: Amazon RDS/EC2 Web App Deployment with MySQL JDBC database

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
 
Line 1: Line 1:
 +
[IN PROGRESS BEING UPGRADED to 4.0.28]
 +
 
This is a continuance of our earlier tutorial on Resin cloud deploy on
 
This is a continuance of our earlier tutorial on Resin cloud deploy on
 
Amazon EC2. The first part of this tutorial could be used to see how
 
Amazon EC2. The first part of this tutorial could be used to see how
to configure Resin for production deployments. We continue to use
+
to configure Resin for production deployments.  
Spring Roo to generate an application.  
+
We create a simple web application that uses MySQL.
  
 
The first part of this tutorial just shows how to use a database from
 
The first part of this tutorial just shows how to use a database from
Line 21: Line 23:
  
 
==Tutorial Status==
 
==Tutorial Status==
This tutorial is in progress. It is being updated from 4.0.24 to 4.0.26.
+
This tutorial is in progress. It is being updated from 4.0.24/4.0.26 to 4.0.27 and 4.0.28.
 
Improvements were made to Resin to support remote cloud deployment and configuration.
 
Improvements were made to Resin to support remote cloud deployment and configuration.
  
 +
We assume your development box is Ubuntu or Debian and your Amazon AMI is Amazon Linux (CentOS 4.5 more or less/Red Hat Enterprise).
  
 
==Install MySQL on local box==
 
==Install MySQL on local box==
Line 59: Line 62:
 
</code>
 
</code>
  
Exit mysql.
+
Use blogdb database and create a table called blog.
  
<code>
 
 
<pre>
 
<pre>
mysql> exit;
+
 
 +
mysql> use blogdb;
  
 
Output:
 
Output:
Bye
+
Database changed
 
</pre>
 
</pre>
</code>
 
  
==Use Roo to switch to MySQL==
+
Create a table called blog.
 +
<pre>
 +
mysql> CREATE TABLE blog (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
 +
                                            title VARCHAR(80), message VARCHAR(2000));
  
In the examples folder start up roo and switch to using MySQL.
+
Output:
 +
Query OK, 0 rows affected (0.01 sec)
  
 +
</pre>
 +
 +
Exit the MySQL client.
  
<code>
 
 
<pre>
 
<pre>
$ roo
+
mysql> exit;
 
+
roo> jpa setup --database MYSQL --provider HIBERNATE --databaseName blogdb
+
  
 
Output:
 
Output:
Updated SPRING_CONFIG_ROOT/database.properties
+
Bye
Please update your database details in src/main/resources/META-INF/spring/database.properties.
+
Updated SPRING_CONFIG_ROOT/applicationContext.xml
+
Updated ROOT/src/main/resources/META-INF/persistence.xml
+
Updated ROOT/pom.xml [added dependency mysql:mysql-connector-java:5.1.18; removed dependency com.h2database:h2:1.3.161]
+
 
</pre>
 
</pre>
</code>
 
  
Find and modify the database properties for this application. (Later we will set this up in JNDI of Resin, but for now just modify the properties file).
+
== Local Dev Box: Make sure Resin is started ==
  
 +
Check the status, if status cannot connect then Resin is not started:
  
<code>
 
 
<pre>
 
<pre>
 
+
$ resinctl status
$ find . -name "database.properties"
+
Resin/4.0.27 can't retrieve status of -server 'app-0' for watchdog at 127.0.0.1:6600.
 
+
com.caucho.bam.RemoteConnectionFailedException: BamError[type=cancel,group=remote-connection-failed,text=Cannot connect to http://127.0.0.1:6600/hmtp
Output
+
  java.net.ConnectException: Connection refused]
./src/main/resources/META-INF/spring/database.properties
+
 
+
$ emacs ./src/main/resources/META-INF/spring/database.properties
+
 
+
 
</pre>
 
</pre>
</code>
 
  
Add the user name and password that we setup earlier.
+
Start Resin if not connected:
 
+
<code>
+
 
<pre>
 
<pre>
#Updated at Thu Dec 01 12:57:30 PST 2011
+
$ sudo resinctl start
#Thu Dec 01 12:57:30 PST 2011
+
 
database.password=roofoo
+
Output:
database.url=jdbc\:mysql\://localhost\:3306/blogdb
+
Resin/4.0.27 launching watchdog at 127.0.0.1:6600
database.username=bloguser
+
Resin/4.0.27 started -server 'app-0' with watchdog at 127.0.0.1:6600
database.driverClassName=com.mysql.jdbc.Driver
+
 
</pre>
 
</pre>
</code>
 
  
== Deploy and test what we have done so far ==
+
The status should look something like this when you are connected:
Now let's test out what we have so far. Build the project and deploy it to resin.
+
  
<code>
 
 
<pre>
 
<pre>
$ mvn package
+
$ resinctl status
 
+
  
 
Output:
 
Output:
...
 
[INFO] BUILD SUCCESS
 
  
$ resinctl deploy --name blogm ./target/blog-0.1.0.BUILD-SNAPSHOT.war
+
Resin/4.0.27 status for watchdog at 127.0.0.1:6600
  
Output
+
watchdog:
Deployed production/webapp/default/blogm from ./target/blog-0.1.0.BUILD-SNAPSHOT.war to hmux://127.0.0.1:6800
+
  watchdog-pid: 1829
  
 +
server 'app-0' : ACTIVE
 +
  password: ok
 +
  watchdog-user: root
 +
  user: www-data(www-data)
 +
  root: /var/www
 +
  conf: /etc/resin/resin.xml
 +
  pid: 2021
 +
  uptime: 0 days 00h00
 
</pre>
 
</pre>
</code>
 
  
Since this is a local development box, you could just copy the war
 
file over.
 
  
<code>
+
== Download MySQL Jar file ==
<pre>
+
$ sudo cp ./target/blog-0.1.0.BUILD-SNAPSHOT.war /var/www/webapps/
+
</pre>
+
</code>
+
  
Now startup the blogm app and add a blog entry.
+
Download and unpack the MySQL Jar file.
  
$ firefox http://localhost:8080/blogm/
+
http://dev.mysql.com/downloads/connector/j/5.1.html
  
If you like, you can fire up the mysql client and see if your new entry and blog table is in MySQL.
+
Unpack it:
  
<code>
 
 
<pre>
 
<pre>
$ mysql -u root -p
+
$ cd ~
Enter password:
+
$ cd Downloads/
 
+
$ tar xzvf mysql-connector-java-5.1.19.tar.gz
mysql> use blogdb;
+
 
+
mysql> show tables;
+
+------------------+
+
| Tables_in_blogdb |
+
+------------------+
+
| blog            |
+
+------------------+
+
1 row in set (0.00 sec)
+
 
+
mysql> select * from blog;
+
+----+---------+-------+---------+
+
| id | message | title | version |
+
+----+---------+-------+---------+
+
|  1 | sdf    | sadf  |      0 |
+
+----+---------+-------+---------+
+
1 row in set (0.00 sec)
+
 
+
mysql> exit
+
 
+
 
</pre>
 
</pre>
</code>
 
  
== Now change Resin and Roo to use JNDI instead of Spring configured datasource ==
+
== Create driver deploy directory and install MySQL jar file with config file ==
  
Resin can monitor and manage database connection better than Spring alone.  
+
Resin can monitor and manage database connection.  
In order to do this you need to JNDI. It also good to enable JNDI for production so that
+
In order to do this you need to use JNDI. It also good to enable JNDI for production so that
 
a select few in operations can control the user name and password of the production system databases.
 
a select few in operations can control the user name and password of the production system databases.
 
Having JNDI allows your dev box and your prod box to have different usernames and passwords as well without rebuilding a special war file for each.
 
Having JNDI allows your dev box and your prod box to have different usernames and passwords as well without rebuilding a special war file for each.
Line 191: Line 157:
 
In this tutorial our dev box is Ubuntu and our prod box is Amazon AMI in the sky.
 
In this tutorial our dev box is Ubuntu and our prod box is Amazon AMI in the sky.
  
First tell Roo to use JNDI as follows:
+
Now we need to add a JNDI entry for our MySQL database to Resin and deploy the
 +
JDBC driver to Resin. You could do this manually. If you use the command line deployment,
 +
then it is easy to deploy the driver and the conf xml file to the cloud.
  
<code>
+
Create a config directory and populate it:
 
<pre>
 
<pre>
$ roo
+
$ cd ~
    ____  ____  ____ 
+
$ mkdir blogDBDeploy
  / __ \/ __ \/ __ \
+
$ cd blogDBDeploy/
  / /_/ / / / / / / /
+
$ mkdir resin-inf
/ _, _/ /_/ / /_/ / 
+
$ mkdir resin-inf/blog-db
/_/ |_|\____/\____/    1.2.0.RC1 [rev dcaa483]
+
$ cd resin-inf/blog-db
 
+
...
+
 
+
roo> jpa setup --database MYSQL --provider HIBERNATE --databaseName blogdb --jndiDataSource jdbc/blogdb
+
Output:
+
Updated SPRING_CONFIG_ROOT/applicationContext.xml
+
 
+
roo> exit
+
 
+
 
</pre>
 
</pre>
</code>
 
  
Notice the addition of --jndiDataSource jdbc/blogdb.
+
A config directory can have both jar files and Resin XML config files in it.
 +
In our case we are going to deploy the MySQL driver (jar file) and the XML file that configures our blogdb database
 +
in JNDI.
  
Let's see the change it made.
+
Copy the MySQL jar file into <code>~/blogDBDeploy/resin-inf/blog-db</code>.
  
<code>
 
 
<pre>
 
<pre>
 +
$ pwd
  
$ find . -name applicationContext.xml
+
Output:
./src/main/resources/META-INF/spring/applicationContext.xml
+
~/blogDBDeploy/resin-inf/blog-db
  
$ grep -A 5 -B 5 jdbc ./src/main/resources/META-INF/spring/applicationContext.xml
+
$ mkdir lib
    </context:component-scan>
+
$ cp ~/Downloads/mysql-connector-java-5.1.19/*.jar lib
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
+
$ ls lib
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
+
    </bean>
+
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
+
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/blogdb"/>
+
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
+
        <property name="persistenceUnitName" value="persistenceUnit"/>
+
        <property name="dataSource" ref="dataSource"/>
+
    </bean>
+
</beans>
+
  
</pre>
+
Output:
</code>
+
mysql-connector-java-5.1.19-bin.jar
  
Notice the line that has < jee:jndi-lookup id="dataSource" jndi-name="jdbc/blogdb".
 
  
Now we need to add a JNDI entry for our MySQL database to Resin and deploy the
+
</pre>
JDBC driver to Resin.
+
  
$ mkdir database-conf
 
$ cd database-conf
 
  
 +
Create the file <code>~/blogDBDeploy/resin-inf/blog-db/blog-database-conf.xml</code>
  
Create the file database-conf.xml
+
<pre>
 +
$ cd ..
 +
$ nano blog-database-conf.xml
 +
</pre>
  
  
 
And add the contents as follows:
 
And add the contents as follows:
  
<code>
 
 
<pre>
 
<pre>
  
Line 270: Line 220:
  
 
</pre>
 
</pre>
</code>
 
  
  
Find and copy the mysql jar file from the maven local repo to Resin as follows:
 
  
Find it:
+
Now we can deploy this whole directory, jar file and configuration
 +
file to Resin in one swope using resinctl config-deploy. First let's
 +
use resinctl help system to give us some more information.
  
<code>
+
Run config deploy from the root of blogDBDeploy:
<pre>
+
$ find ~/.m2/repository/ -name "*mysql*.jar"
+
/home/rick/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar
+
</pre>
+
</code>
+
  
 
Now copy it to database-conf dir.
 
 
<code>
 
 
<pre>
 
<pre>
  $ sudo cp ~/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar .
 
 
</pre>
 
</code>
 
 
When you are done, you should have something like this:
 
 
 
<code>
 
<pre>
 
 
 
$ pwd
 
$ pwd
/home/rick/examples/blog/database-conf
+
~/blogDBDeploy
 
+
$ ls
+
database-conf.xml  mysql-connector-java-5.1.18.jar
+
 
+
$ cat database-conf.xml
+
<resin xmlns="http://caucho.com/ns/resin"
+
      xmlns:resin="urn:java:com.caucho.resin">
+
 
+
<database jndi-name="jdbc/blogdb">
+
        <driver type="com.mysql.jdbc.Driver">
+
                <url>jdbc:mysql://localhost:3306/blogdb</url>
+
                <user>bloguser</user>
+
                <password>roofoo</password>
+
        </driver>
+
</database>
+
 
+
</resin>
+
 
+
 
</pre>
 
</pre>
</code>
 
  
Now we can deploy this whole directory, jar file and configuration
+
Let's look at the options for <code>config-deploy</code>:
file to Resin in one swope using resinctl config-deploy. First let's
+
use resinctl help system to give us some more information.
+
  
 
<code>
 
 
<pre>
 
<pre>
 
$ resinctl help config-deploy
 
$ resinctl help config-deploy
Line 338: Line 245:
 
   --address <ip>        : IP address or host name of (triad) server
 
   --address <ip>        : IP address or host name of (triad) server
 
   --conf <file>          : alternate resin.xml file
 
   --conf <file>          : alternate resin.xml file
   --log-directory <file> : alternate log directory
+
  --license-directory <dir> : alternate license directory
 +
   --log-directory <dir> : alternate log directory
 +
  --m <message>          : commit message
 
   --mode <string>        : select .resin properties mode
 
   --mode <string>        : select .resin properties mode
 
   --password <password>  : admin password for authentication
 
   --password <password>  : admin password for authentication
 
   --port <port>          : IP port of (triad) server
 
   --port <port>          : IP port of (triad) server
 
   --resin-home <dir>    : alternate resin home
 
   --resin-home <dir>    : alternate resin home
   --root-directory <file> : alternate root directory
+
   --root-directory <dir> : alternate root directory
 
   --server <id>          : id of a server in the config file
 
   --server <id>          : id of a server in the config file
 +
  --stage <stage>        : stage to deploy application to, defaults to production
 
   --user <user>          : admin user name for authentication
 
   --user <user>          : admin user name for authentication
 
   --user-properties <file> : select an alternate $HOME/.resin file
 
   --user-properties <file> : select an alternate $HOME/.resin file
 
   --verbose              : produce verbose output
 
   --verbose              : produce verbose output
 
+
  --version <version>    : version of application formatted as <major.minor.micro.qualifier>
 
</pre>
 
</pre>
</code>
 
  
  
If we don't specify the address and the port, then we should deploy
+
If we don't specify the address and the port, then it will deploy
 
locally to our dev box. Later when we want to deploy to Amazon WS, we
 
locally to our dev box. Later when we want to deploy to Amazon WS, we
 
just need to specify its address and port of our remote server in the
 
just need to specify its address and port of our remote server in the
Line 360: Line 269:
 
First let's deploy our config to our dev box.
 
First let's deploy our config to our dev box.
  
 +
Let's briefly reivew what we are deploying (the directory structure and the files):
  
<code>
 
 
<pre>
 
<pre>
 +
$ ls -R
 +
.:
 +
resin-inf
  
$ cd ..
+
./resin-inf:
 +
blog-db
  
$ ls
+
./resin-inf/blog-db:
 +
blog-database-conf.xml  lib
  
database-conf  log.roo  pom.xml  src  target
+
./resin-inf/blog-db/lib:
 +
mysql-connector-java-5.1.19-bin.jar
  
$ resinctl config-deploy database-conf/
+
</pre>
  
Deployed production/config/resin from database-conf/ to hmux://127.0.0.1:6800
+
Now let's deploy it:
 +
<pre>
 +
$ cd ..
 +
$ pwd
  
 +
Output:
 +
~
  
 +
 +
$ resinctl config-deploy blogDBDeploy/
 +
 +
Output:
 +
Deployed production/config/resin from blogDBDeploy to hmux://127.0.0.1:6800
 
</pre>
 
</pre>
</code>
 
  
To verify that our deploy worked, lets run resinctl config-ls, and
+
 
resinctl config-cat:
+
To verify that our deploy worked, lets run <code>resinctl config-ls</code>, and
 +
<code>resinctl config-cat</code>:
  
  
<code>
 
 
<pre>
 
<pre>
  
 
$ resinctl config-ls
 
$ resinctl config-ls
  
database-conf.xml
+
Output:
mysql-connector-java-5.1.18.jar
+
resin-inf/blog-db/blog-database-conf.xml
 +
resin-inf/blog-db/lib/mysql-connector-java-5.1.19-bin.jar
  
$ resinctl config-cat database-conf.xml
+
$ resinctl config-cat resin-inf/blog-db/blog-database-conf.xml
  
 +
Output:
 
<resin xmlns="http://caucho.com/ns/resin"
 
<resin xmlns="http://caucho.com/ns/resin"
 
       xmlns:resin="urn:java:com.caucho.resin">
 
       xmlns:resin="urn:java:com.caucho.resin">
Line 404: Line 330:
  
 
</resin>
 
</resin>
 
 
</pre>
 
</pre>
</code>
 
  
 +
==Blog Project with Eclipse==
  
== Deploy and test what we have done so far ==
 
Now let's test out what we have so far. Build the project and deploy it to resin.
 
  
<code>
+
If you already have a war file that you want to deploy, then you can skip this section.
<pre>
+
$ mvn package
+
  
 +
[http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr2 Use Eclipse for Java EE Indigo or higher].
  
Output:
+
If you know how to create a new project using Eclipse and Resin Java EE environment, just go ahead and do it now.
...
+
[INFO] BUILD SUCCESS
+
  
$ resinctl deploy --name blogjndi  ./target/blog-0.1.0.BUILD-SNAPSHOT.war
+
Otherwise follow these instructions:
  
Output
+
Using Eclipse Indigo
Deployed production/webapp/default/blogjndi from ./target/blog-0.1.0.BUILD-SNAPSHOT.war to hmux://127.0.0.1:6800
+
  
</pre>
+
# Go to File menu -> New Project -> Dynamic Web Project
</code>
+
# In New Dynamic Web Project Dialog-> New Runtime...
 +
# In New Runtime Dialog -> Download Additional Server Adapters -> Select Resin (Java EE Web Profile) 4.0.24
 +
# (Click Next and Ok until it installs Resin runtime)
 +
# (Eclipse needs to restart)
 +
# File -> New Project -> Dynamic Web Project
 +
# In New Dynamic Web Project Dialog-> New Runtime...->Select Resin->Check create local server checkbox
 +
# Step 2 of New Runtime...->Click Download and Install
 +
# Fill out project name etc. (blog).
 +
# (Click Next and Ok until you are done)
  
Now startup the blogm app and add a blog entry.
 
  
$ firefox http://localhost:8080/blogjndi/
+
After the project is setup, do the following:
  
Poke around with mysql client tool and see if your new blog entry is there.
+
File->New->Servlet (Name the new Servlet example.BlogServlet)
 
+
LEFT OFF HERE.
+
 
+
== Setting up variables ==
+
 
+
I don't like needing to modify XML all of the time. You can setup EL variables in database.xml that allow you to set key name value pairs in the resin.properties file as follows:
+
 
+
'''/etc/resin/resin.properties''' Add three properties to resin.properties
+
<code>
+
<pre>
+
blogdb.url : localhost
+
blogdb.user : bloguser
+
blogdb.password : roofoo
+
</pre>
+
</code>
+
 
+
 
+
'''/etc/resin/local.d/database.xml''' Change database.xml to use new properties.
+
<code>
+
<pre>
+
<resin xmlns="http://caucho.com/ns/resin"
+
      xmlns:resin="urn:java:com.caucho.resin">
+
 
+
<database jndi-name="jdbc/blogdb">
+
        <driver type="com.mysql.jdbc.Driver">
+
                <url>jdbc:mysql://${blogdb.url}:3306/blogdb</url>
+
                <user>${blogdb.user}</user>
+
                <password>${blogdb.password}</password>
+
        </driver>
+
</database>
+
</resin>
+
</pre>
+
</code>
+
  
This will be nice because later we can set the user-data for the VM and point to the Amazon RDS database.
+
LEFT OFF HERE 4/6/2012...
  
 
==Setup RDS database==
 
==Setup RDS database==

Latest revision as of 00:00, 6 April 2012

[IN PROGRESS BEING UPGRADED to 4.0.28]

This is a continuance of our earlier tutorial on Resin cloud deploy on Amazon EC2. The first part of this tutorial could be used to see how to configure Resin for production deployments. We create a simple web application that uses MySQL.

The first part of this tutorial just shows how to use a database from JNDI. It shows how to install the JDBC jar files and how to configure Resin datasources. There is nothing cloud centric per se about the first part. It also shows how to do remote configuration of Resin servers.

Then we show you how to setup Amazon RDS for MySQL. Then configure Resin to talk to the RDS MySQL instance.

These tutorials are related:

Part 1 Resin_Cloud_deployment_with_Amazon

Part 2 Resin_Cloud_deployment_with_Amazon_WS_EC2_and_Euca_Tools


Contents

Tutorial Status

This tutorial is in progress. It is being updated from 4.0.24/4.0.26 to 4.0.27 and 4.0.28. Improvements were made to Resin to support remote cloud deployment and configuration.

We assume your development box is Ubuntu or Debian and your Amazon AMI is Amazon Linux (CentOS 4.5 more or less/Red Hat Enterprise).

Install MySQL on local box

First let's install MySQL create a user called bloguser with a password. Then let's create a database called blogdb.

$ sudo apt-get install mysql-server

Startup MySQL client tool to create a new user and database:

$ mysql -u root -p

Next create a user called bloguser with the pass roofoo.

mysql> GRANT ALL PRIVILEGES ON *.* TO 'bloguser'@'%' IDENTIFIED BY 'roofoo' WITH GRANT OPTION;

Output:
Query OK, 0 rows affected (0.00 sec)

Next create a database called blogdb.

mysql> create database blogdb;

Output:
Query OK, 1 row affected (0.00 sec)

Use blogdb database and create a table called blog.


mysql> use blogdb;

Output:
Database changed

Create a table called blog.

mysql> CREATE TABLE blog (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
                                            title VARCHAR(80), message VARCHAR(2000));

Output:
Query OK, 0 rows affected (0.01 sec)

Exit the MySQL client.

mysql> exit;

Output:
Bye

Local Dev Box: Make sure Resin is started

Check the status, if status cannot connect then Resin is not started:

$ resinctl status
Resin/4.0.27 can't retrieve status of -server 'app-0' for watchdog at 127.0.0.1:6600.
com.caucho.bam.RemoteConnectionFailedException: BamError[type=cancel,group=remote-connection-failed,text=Cannot connect to http://127.0.0.1:6600/hmtp
  java.net.ConnectException: Connection refused]

Start Resin if not connected:

$ sudo resinctl start

Output:
Resin/4.0.27 launching watchdog at 127.0.0.1:6600
Resin/4.0.27 started -server 'app-0' with watchdog at 127.0.0.1:6600

The status should look something like this when you are connected:

$ resinctl status

Output:

Resin/4.0.27 status for watchdog at 127.0.0.1:6600

watchdog:
  watchdog-pid: 1829

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


Download MySQL Jar file

Download and unpack the MySQL Jar file.

http://dev.mysql.com/downloads/connector/j/5.1.html

Unpack it:

$ cd ~
$ cd Downloads/
$ tar xzvf mysql-connector-java-5.1.19.tar.gz 

Create driver deploy directory and install MySQL jar file with config file

Resin can monitor and manage database connection. In order to do this you need to use JNDI. It also good to enable JNDI for production so that a select few in operations can control the user name and password of the production system databases. Having JNDI allows your dev box and your prod box to have different usernames and passwords as well without rebuilding a special war file for each.

In this tutorial our dev box is Ubuntu and our prod box is Amazon AMI in the sky.

Now we need to add a JNDI entry for our MySQL database to Resin and deploy the JDBC driver to Resin. You could do this manually. If you use the command line deployment, then it is easy to deploy the driver and the conf xml file to the cloud.

Create a config directory and populate it:

$ cd ~
$ mkdir blogDBDeploy
$ cd blogDBDeploy/
$ mkdir resin-inf
$ mkdir resin-inf/blog-db
$ cd resin-inf/blog-db

A config directory can have both jar files and Resin XML config files in it. In our case we are going to deploy the MySQL driver (jar file) and the XML file that configures our blogdb database in JNDI.

Copy the MySQL jar file into ~/blogDBDeploy/resin-inf/blog-db.

$ pwd

Output:
~/blogDBDeploy/resin-inf/blog-db

$ mkdir lib
$ cp ~/Downloads/mysql-connector-java-5.1.19/*.jar lib
$ ls lib

Output:
mysql-connector-java-5.1.19-bin.jar



Create the file ~/blogDBDeploy/resin-inf/blog-db/blog-database-conf.xml

$ cd ..
$ nano blog-database-conf.xml


And add the contents as follows:


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

<database jndi-name="jdbc/blogdb">
        <driver type="com.mysql.jdbc.Driver">
                 <url>jdbc:mysql://localhost:3306/blogdb</url>
                 <user>bloguser</user>
                 <password>roofoo</password>
         </driver>
</database>

</resin>


Now we can deploy this whole directory, jar file and configuration file to Resin in one swope using resinctl config-deploy. First let's use resinctl help system to give us some more information.

Run config deploy from the root of blogDBDeploy:

$ pwd
~/blogDBDeploy

Let's look at the options for config-deploy:

$ resinctl help config-deploy
usage: resinctl config-deploy [--options]

  deploys a configuration directory or jar file

where options include:
  --address <ip>         : IP address or host name of (triad) server
  --conf <file>          : alternate resin.xml file
  --license-directory <dir> : alternate license directory
  --log-directory <dir>  : alternate log directory
  --m <message>          : commit message
  --mode <string>        : select .resin properties mode
  --password <password>  : admin password for authentication
  --port <port>          : IP port of (triad) server
  --resin-home <dir>     : alternate resin home
  --root-directory <dir> : alternate root directory
  --server <id>          : id of a server in the config file
  --stage <stage>        : stage to deploy application to, defaults to production
  --user <user>          : admin user name for authentication
  --user-properties <file> : select an alternate $HOME/.resin file
  --verbose              : produce verbose output
  --version <version>    : version of application formatted as <major.minor.micro.qualifier>


If we don't specify the address and the port, then it will deploy locally to our dev box. Later when we want to deploy to Amazon WS, we just need to specify its address and port of our remote server in the cloud.

First let's deploy our config to our dev box.

Let's briefly reivew what we are deploying (the directory structure and the files):

$ ls -R
.:
resin-inf

./resin-inf:
blog-db

./resin-inf/blog-db:
blog-database-conf.xml  lib

./resin-inf/blog-db/lib:
mysql-connector-java-5.1.19-bin.jar

Now let's deploy it:

$ cd ..
$ pwd

Output:
~


$ resinctl config-deploy blogDBDeploy/

Output:
Deployed production/config/resin from blogDBDeploy to hmux://127.0.0.1:6800


To verify that our deploy worked, lets run resinctl config-ls, and resinctl config-cat:



$ resinctl config-ls

Output:
resin-inf/blog-db/blog-database-conf.xml
resin-inf/blog-db/lib/mysql-connector-java-5.1.19-bin.jar

$ resinctl config-cat resin-inf/blog-db/blog-database-conf.xml

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

<database jndi-name="jdbc/blogdb">
        <driver type="com.mysql.jdbc.Driver">
                 <url>jdbc:mysql://localhost:3306/blogdb</url>
                 <user>bloguser</user>
                 <password>roofoo</password>
         </driver>
</database>

</resin>

Blog Project with Eclipse

If you already have a war file that you want to deploy, then you can skip this section.

Use Eclipse for Java EE Indigo or higher.

If you know how to create a new project using Eclipse and Resin Java EE environment, just go ahead and do it now.

Otherwise follow these instructions:

Using Eclipse Indigo

  1. Go to File menu -> New Project -> Dynamic Web Project
  2. In New Dynamic Web Project Dialog-> New Runtime...
  3. In New Runtime Dialog -> Download Additional Server Adapters -> Select Resin (Java EE Web Profile) 4.0.24
  4. (Click Next and Ok until it installs Resin runtime)
  5. (Eclipse needs to restart)
  6. File -> New Project -> Dynamic Web Project
  7. In New Dynamic Web Project Dialog-> New Runtime...->Select Resin->Check create local server checkbox
  8. Step 2 of New Runtime...->Click Download and Install
  9. Fill out project name etc. (blog).
  10. (Click Next and Ok until you are done)


After the project is setup, do the following:

File->New->Servlet (Name the new Servlet example.BlogServlet)

LEFT OFF HERE 4/6/2012...

Setup RDS database

Setup an RDS database using MySQL 5.1.7 (or as close to your MySQL as possible).

I use the same username password to make it simple. The only part that is going to change for sure is the URL.

Using the AWS web console modify the user-data of the resinserver we deployed earlier and add three properies.

user-data on resin server


blogdb.url : blogdb.cvolnlau763z.us-east-1.rds.amazonaws.com
blogdb.user : bloguser
blogdb.password : roofoo

Note that the URL should be the url of the Amazon RDS database that you just setup. Also you need to setup a database security group that allows the group of your vm instance access the database.


Start up the VM instance that we installed Resin on earlier and create a database.xml file in the /etc/resin/local.d/ as follows:


/etc/resin/local.d/database.xml Create database.properties on remote Amazon instance.

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

<database jndi-name="jdbc/blogdb">
        <driver type="com.mysql.jdbc.Driver">
                 <url>jdbc:mysql://${blogdb.url}:3306/blogdb</url>
                 <user>${blogdb.user}</user>
                 <password>${blogdb.password}</password>
         </driver>
</database>
</resin>


Next install the mysql driver on the Amazon instance.

$ sudo yum install mysql-connector-java
$ sudo cp /usr/share/java/mysql-connector-java.jar /usr/local/share/resin/lib/

Now deploy the war file to the Amazon AMI

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

Now test it out: http://ec2-107-22-13-146.compute-1.amazonaws.com:8080/blogdb/blogs/1

It worked for me. :)

Personal tools
TOOLBOX
LANGUAGES