Deploying Java applications

Overview

This document is a hands-on guide to deploying a simple Java application ontsuru. The example application is a simple mvn generated archetype, in order togenerate it, just run:

  1. $ mvn archetype:generate -DgroupId=io.tsuru.javasample -DartifactId=helloweb -DarchetypeArtifactId=maven-archetype-webapp

You can also deploy any other Java application you have on a tsuru server.Another alternative is to just download the code available at GitHub:https://github.com/tsuru/tsuru-java-sample.

Creating the app

To create an app, you use the command app-create:

  1. $ tsuru app-create <app-name> <app-platform>

For Java, the app platform is, guess what, java! Let’s call our application “helloweb”:

  1. $ tsuru app-create helloweb java

To list all available platforms, use the command platform-list.

You can see all your applications using the command app-list:

  1. $ tsuru app-list
  2. +-------------+-------------------------+------------------------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+------------------------------+
  5. | helloweb | 0 of 0 units in-service | helloweb.192.168.50.4.nip.io |
  6. +-------------+-------------------------+------------------------------+

Deploying the code

Using the Java platform, there are two deployment strategies: users can eitherupload WAR files to tsuru or send the code using the regular git pushapproach. This guide will cover both approaches:

WAR deployment

Using the mvn archetype, generating the WAR is as easy as running mvn
package
, then the user can deploy the code using tsuru app-deploy:

  1. $ mvn package
  2. $ cd target
  3. $ tsuru app-deploy -a helloweb helloweb.war
  4. Uploading files.... ok
  5.  
  6. ---- Building application image ----
  7. ---> Sending image to repository (0.00MB)
  8. ---> Cleaning up
  9.  
  10. ---- Starting 1 new unit ----
  11. ---> Started unit 21c3b6aafa...
  12.  
  13. ---- Binding and checking 1 new units ----
  14. ---> Bound and checked unit 21c3b6aafa
  15.  
  16. ---- Adding routes to 1 new units ----
  17. ---> Added route to unit 21c3b6aafa
  18.  
  19. OK

Done! Now you can access your project in the address displayed in the output oftsuru app-list. Remeber to add /helloweb/.

You can also deploy you application to the / address, renaming the WAR toROOT.war and redeploying it:

  1. $ mv helloweb.war ROOT.war
  2. $ tsuru app-deploy -a helloweb ROOT.war
  3. Uploading files... ok
  4.  
  5. ---- Building application image ----
  6. ---> Sending image to repository (0.00MB)
  7. ---> Cleaning up
  8.  
  9. ---- Starting 1 new unit ----
  10. ---> Started unit 4d155e805f...
  11.  
  12. ---- Adding routes to 1 new units ----
  13. ---> Added route to unit 4d155e805f
  14.  
  15. ---- Removing routes from 1 old units ----
  16. ---> Removed route from unit d2811c0801
  17.  
  18. ---- Removing 1 old unit ----
  19. ---> Removed old unit 1/1
  20.  
  21. OK

And now you can access your hello world in the root of the application address!

Git deployment

For Git deployment, we will send the code to tsuru, and compile the classesthere. For that, we’re going to use mvn with the Jetty plugin.For doing that, we will need to create a Procfile with the command for startingthe application:

  1. $ cat Procfile
  2. web: mvn jetty:run

In order to compile the application classes during deployment, we need also toadd a deployment hook. tsuru parses a file called tsuru.yaml and runs somebuild hooks in the deployment phase.

Here is how the file for the helloweb application looks like:

  1. $ cat tsuru.yaml
  2. hooks:
  3. build:
  4. - mvn package

After adding these files, we’re ready for deploying the application. Thecommand app-info command will display a Git remote that we can use to pushthe application code to production:

  1. $ tsuru app-info -a helloweb
  2. Application: helloweb
  3. Repository: git@192.168.50.4.nip.io:helloweb.git
  4. Platform: java
  5. Teams: admin
  6. Address: helloweb.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Deploys: 2
  10. Pool: theonepool
  11. Units: 1
  12. +------------+---------+
  13. | Unit | State |
  14. +------------+---------+
  15. | 313458bb9d | started |
  16. +------------+---------+
  17.  
  18. App Plan:
  19. +---------------+--------+------+-----------+---------+
  20. | Name | Memory | Swap | Cpu Share | Default |
  21. +---------------+--------+------+-----------+---------+
  22. | autogenerated | 0 MB | 0 MB | 100 | false |
  23. +---------------+--------+------+-----------+---------+

The “Repository” line contains what we need: the remote repository. Now we cansimply push the application code, using Git push:

  1. $ git push git@192.168.50.4.nip.io:helloweb.git master
  2. Counting objects: 25, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (19/19), done.
  5. Writing objects: 100% (25/25), 2.59 KiB | 0 bytes/s, done.
  6. Total 25 (delta 5), reused 0 (delta 0)
  7. remote: tar: Removing leading `/' from member names
  8. remote: [INFO] Scanning for projects...
  9. remote: [INFO]
  10. remote: [INFO] ------------------------------------------------------------------------
  11. remote: [INFO] Building helloweb Maven Webapp 1.0-SNAPSHOT
  12. remote: [INFO] ------------------------------------------------------------------------
  13. remote: Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.3/maven-resources-plugin-2.3.pom
  14. remote: Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.3/maven-resources-plugin-2.3.pom (5 KB at 6.0 KB/sec)
  15. remote: Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/12/maven-plugins-12.pom
  16. remote: Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/12/maven-plugins-12.pom (12 KB at 35.9 KB/sec)
  17.  
  18. ...
  19.  
  20. remote: [INFO] Packaging webapp
  21. remote: [INFO] Assembling webapp [helloweb] in [/home/application/current/target/helloweb]
  22. remote: [INFO] Processing war project
  23. remote: [INFO] Copying webapp resources [/home/application/current/src/main/webapp]
  24. remote: [INFO] Webapp assembled in [27 msecs]
  25. remote: [INFO] Building war: /home/application/current/target/helloweb.war
  26. remote: [INFO] WEB-INF/web.xml already added, skipping
  27. remote: [INFO] ------------------------------------------------------------------------
  28. remote: [INFO] BUILD SUCCESS
  29. remote: [INFO] ------------------------------------------------------------------------
  30. remote: [INFO] Total time: 51.729s
  31. remote: [INFO] Finished at: Tue Nov 11 17:04:05 UTC 2014
  32. remote: [INFO] Final Memory: 8M/19M
  33. remote: [INFO] ------------------------------------------------------------------------
  34. remote:
  35. remote: ---- Building application image ----
  36. remote: ---> Sending image to repository (2.96MB)
  37. remote: ---> Cleaning up
  38. remote:
  39. remote: ---- Starting 1 new unit ----
  40. remote: ---> Started unit e71d176232...
  41. remote:
  42. remote: ---- Adding routes to 1 new units ----
  43. remote: ---> Added route to unit e71d176232
  44. remote:
  45. remote: ---- Removing routes from 1 old units ----
  46. remote: ---> Removed route from unit d8a2d14948
  47. remote:
  48. remote: ---- Removing 1 old unit ----
  49. remote: ---> Removed old unit 1/1
  50. remote:
  51. remote: OK
  52. To git@tsuru.mycompany.com:helloweb.git
  53. * [new branch] master -> master

As you can see, the final part of the output is the same, and the applicationis running in the address given by tsuru as well.

Switching between Java versions

In the Java platform provided by tsuru, users can use two version of Java: 7and 8, both provided by Oracle. There’s an environment variable for definingthe Java version you wanna use: JAVA_VERSION. The default behavior of theplatform is to use Java 7, but you can change to Java 8 by running:

  1. $ tsuru env-set -a helloweb JAVA_VERSION=8
  2. ---- Setting 1 new environment variables ----
  3.  
  4. ---- Starting 1 new unit ----
  5. ---> Started unit d8a2d14948...
  6.  
  7. ---- Adding routes to 1 new units ----
  8. ---> Added route to unit d8a2d14948
  9.  
  10. ---- Removing routes from 1 old units ----
  11. ---> Removed route from unit 4d155e805f
  12.  
  13. ---- Removing 1 old unit ----
  14. ---> Removed old unit 1/1

And… done! No need to run another deployment, your application is now runningwith Java 8.

Setting memory for application

In the Java platform provided by tsuru, users can use units with different plans and each plan may have containers with different amounts of memory. There’s an environment variable for defining the max amount of heap memory (in megabytes) that Java should use: JAVA_MAX_MEMORY ( it’s equal -Xmx). The default value for this environment variable is 128 (it can be different according to your basebuilder).

  1. $ tsuru env-set -a helloweb JAVA_MAX_MEMORY=1024
  2. ---- Setting 1 new environment variables ----
  3.  
  4. ---- Starting 1 new unit ----
  5. ---> Started unit o5p1k70289...
  6.  
  7. ---- Adding routes to 1 new units ----
  8. ---> Added route to unit o5p1k70289
  9.  
  10. ---- Removing routes from 1 old units ----
  11. ---> Removed route from unit d8a2d14948
  12.  
  13. ---- Removing 1 old unit ----
  14. ---> Removed old unit 1/1

And… done! No need to run another deployment, your application is now runningwith more memory.

Going further

For more information, you can dig into tsuru docs, orread complete instructions of use for the tsuru command.

原文: https://docs.tsuru.io/1.6/using/java.html