用tsuru部署Java应用

概述

本文档是在tsuru中部署一个简单的Java应用的实战指南。例子应用原型由mvn生成,通过运行下面的命令生成:

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

也可以在tsuru服务器上部署任意其它的Java应用。另一个选择是直接下载在Github上的代码:https://github.com/tsuru/tsuru-java-sample。

在tsuru中创建应用

使用app-create命令创建应用:

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

对于Java来说,应用平台是,你猜,Java!让我们把应用起名为"helloweb":

  1. $ tsuru app-create helloweb java

使用platform-list命令列出所有的可用的平台。使用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. +-------------+-------------------------+------------------------------+

部署代码

Java平台的部署方式有两种:用户可以将WAR包上传到tsuru或者用常规的git push的方式部署。下面将会介绍这两种方法:

WAR包部署

使用mvn原型,可以很容易的用mvn package打包WAR文件,然后用户可以通过tsuru app-deploy去部署代码:

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

完成!现在我们可以访问tsuru app-list返回的项目地址。记得加上/helloweb/。也可以将应用部署到 / 下面,只需要将WAR重命名为ROOT.war并且重新部署:

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

这样就可以从应用的根地址访问helloworld应用了。

通过Git部署

对于Git部署的方式,需要将代码发送给tsuru,然后在那里编译。我们会配合mvn使用Jetty plugin。这么做需要创建一个Procfile并且输入以下的命令去启动应用:

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

为了能在部署过程中编译应用的类,我们需要添加部署钩子。tsuru解析tsuru.yaml并且在部署阶段运行一些构建钩子。下面是helloweb应用的tsuru.yaml文件的内容:

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

添加完这些文件后,就可以部署应用了。app-info命令会显示一个Git远程地址。我们将代码推送到这个地址。

  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. App Plan:
  18. +---------------+--------+------+-----------+--------+---------+
  19. | Name | Memory | Swap | Cpu Share | Router | Default |
  20. +---------------+--------+------+-----------+--------+---------+
  21. | autogenerated | 0 MB | 0 MB | 100 | | false |
  22. +---------------+--------+------+-----------+--------+---------+

"Repository"那行包含了我们需要的远程仓库的地址。 现在我们可以用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. remote: [INFO] Packaging webapp
  19. remote: [INFO] Assembling webapp [helloweb] in [/home/application/current/target/helloweb]
  20. remote: [INFO] Processing war project
  21. remote: [INFO] Copying webapp resources [/home/application/current/src/main/webapp]
  22. remote: [INFO] Webapp assembled in [27 msecs]
  23. remote: [INFO] Building war: /home/application/current/target/helloweb.war
  24. remote: [INFO] WEB-INF/web.xml already added, skipping
  25. remote: [INFO] ------------------------------------------------------------------------
  26. remote: [INFO] BUILD SUCCESS
  27. remote: [INFO] ------------------------------------------------------------------------
  28. remote: [INFO] Total time: 51.729s
  29. remote: [INFO] Finished at: Tue Nov 11 17:04:05 UTC 2014
  30. remote: [INFO] Final Memory: 8M/19M
  31. remote: [INFO] ------------------------------------------------------------------------
  32. remote:
  33. remote: ---- Building application image ----
  34. remote: ---> Sending image to repository (2.96MB)
  35. remote: ---> Cleaning up
  36. remote:
  37. remote: ---- Starting 1 new unit ----
  38. remote: ---> Started unit e71d176232...
  39. remote:
  40. remote: ---- Adding routes to 1 new units ----
  41. remote: ---> Added route to unit e71d176232
  42. remote:
  43. remote: ---- Removing routes from 1 old units ----
  44. remote: ---> Removed route from unit d8a2d14948
  45. remote:
  46. remote: ---- Removing 1 old unit ----
  47. remote: ---> Removed old unit 1/1
  48. remote:
  49. remote: OK
  50. To git@tsuru.mycompany.com:helloweb.git
  51. * [new branch] master -> master

如你所见,输出的最后一部分是相同的,应用已经运行在tsuru给定的地址了。

切换Java版本

在tsuru的Java平台中,用户可以使用两个版本的Java:Oracle提供的Java 7和8。通过JAVA_VERSION环境变量可以选择你想使用的Java版本。缺省使用Java 7,但是可以通过运行下面的命令切换到Java 8:

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

完成!不需要再运行一次部署,应用已经运行在Java 8之上了。

设置应用的内存大小

在tsuru的Java平台,用户可以使用不同的计划的单元,每个计划可以包含内存不同的容器。定义Java堆内存最大值(以MB计算)可以通过环境变量JAVA_MAX_MEMORY设置。变量缺省值为128(根据你的basebuilder不同而不同)。

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

完成! 不需要再运行一次部署,应用已经被分配了更多的内存。

进一步探索

更多信息,可以查看tsuru文档,或者阅读tsuru命令完全使用指南

原文: http://doc.oschina.net/tsuru-paas?t=52823