用tsuru部署Java应用
概述
本文档是在tsuru中部署一个简单的Java应用的实战指南。例子应用原型由mvn生成,通过运行下面的命令生成:
$ 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
命令创建应用:
$ tsuru app-create <app-name> <app-platform>
对于Java来说,应用平台是,你猜,Java
!让我们把应用起名为"helloweb":
$ tsuru app-create helloweb java
使用platform-list
命令列出所有的可用的平台。使用app-list
查看你所有的应用。
$ tsuru app-list
+-------------+-------------------------+------------------------------+
| Application | Units State Summary | Address |
+-------------+-------------------------+------------------------------+
| helloweb | 0 of 0 units in-service | helloweb.192.168.50.4.nip.io |
+-------------+-------------------------+------------------------------+
部署代码
Java平台的部署方式有两种:用户可以将WAR包上传到tsuru或者用常规的git push
的方式部署。下面将会介绍这两种方法:
WAR包部署
使用mvn原型,可以很容易的用mvn package
打包WAR文件,然后用户可以通过tsuru app-deploy
去部署代码:
$ mvn package
$ cd target
$ tsuru app-deploy -a helloweb helloweb.war
Uploading files.... ok
---- Building application image ----
---> Sending image to repository (0.00MB)
---> Cleaning up
---- Starting 1 new unit ----
---> Started unit 21c3b6aafa...
---- Binding and checking 1 new units ----
---> Bound and checked unit 21c3b6aafa
---- Adding routes to 1 new units ----
---> Added route to unit 21c3b6aafa
OK
完成!现在我们可以访问tsuru app-list
返回的项目地址。记得加上/helloweb/
。也可以将应用部署到 / 下面,只需要将WAR重命名为ROOT.war并且重新部署:
$ mv helloweb.war ROOT.war
$ tsuru app-deploy -a helloweb ROOT.war
Uploading files... ok
---- Building application image ----
---> Sending image to repository (0.00MB)
---> Cleaning up
---- Starting 1 new unit ----
---> Started unit 4d155e805f...
---- Adding routes to 1 new units ----
---> Added route to unit 4d155e805f
---- Removing routes from 1 old units ----
---> Removed route from unit d2811c0801
---- Removing 1 old unit ----
---> Removed old unit 1/1
OK
这样就可以从应用的根地址访问helloworld应用了。
通过Git部署
对于Git部署的方式,需要将代码发送给tsuru,然后在那里编译。我们会配合mvn使用Jetty plugin
。这么做需要创建一个Procfile并且输入以下的命令去启动应用:
$ cat Procfile
web: mvn jetty:run
为了能在部署过程中编译应用的类,我们需要添加部署钩子。tsuru解析tsuru.yaml
并且在部署阶段运行一些构建钩子。下面是helloweb
应用的tsuru.yaml
文件的内容:
$ cat tsuru.yaml
hooks:
build:
- mvn package
添加完这些文件后,就可以部署应用了。app-info
命令会显示一个Git远程地址。我们将代码推送到这个地址。
$ tsuru app-info -a helloweb
Application: helloweb
Repository: git@192.168.50.4.nip.io:helloweb.git
Platform: java
Teams: admin
Address: helloweb.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 2
Pool: theonepool
Units: 1
+------------+---------+
| Unit | State |
+------------+---------+
| 313458bb9d | started |
+------------+---------+
App Plan:
+---------------+--------+------+-----------+--------+---------+
| Name | Memory | Swap | Cpu Share | Router | Default |
+---------------+--------+------+-----------+--------+---------+
| autogenerated | 0 MB | 0 MB | 100 | | false |
+---------------+--------+------+-----------+--------+---------+
"Repository"那行包含了我们需要的远程仓库的地址。 现在我们可以用Git push去推送应用的代码:
$ git push git@192.168.50.4.nip.io:helloweb.git master
Counting objects: 25, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (25/25), 2.59 KiB | 0 bytes/s, done.
Total 25 (delta 5), reused 0 (delta 0)
remote: tar: Removing leading `/' from member names
remote: [INFO] Scanning for projects...
remote: [INFO]
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Building helloweb Maven Webapp 1.0-SNAPSHOT
remote: [INFO] ------------------------------------------------------------------------
remote: Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.3/maven-resources-plugin-2.3.pom
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)
remote: Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/12/maven-plugins-12.pom
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)
...
remote: [INFO] Packaging webapp
remote: [INFO] Assembling webapp [helloweb] in [/home/application/current/target/helloweb]
remote: [INFO] Processing war project
remote: [INFO] Copying webapp resources [/home/application/current/src/main/webapp]
remote: [INFO] Webapp assembled in [27 msecs]
remote: [INFO] Building war: /home/application/current/target/helloweb.war
remote: [INFO] WEB-INF/web.xml already added, skipping
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] BUILD SUCCESS
remote: [INFO] ------------------------------------------------------------------------
remote: [INFO] Total time: 51.729s
remote: [INFO] Finished at: Tue Nov 11 17:04:05 UTC 2014
remote: [INFO] Final Memory: 8M/19M
remote: [INFO] ------------------------------------------------------------------------
remote:
remote: ---- Building application image ----
remote: ---> Sending image to repository (2.96MB)
remote: ---> Cleaning up
remote:
remote: ---- Starting 1 new unit ----
remote: ---> Started unit e71d176232...
remote:
remote: ---- Adding routes to 1 new units ----
remote: ---> Added route to unit e71d176232
remote:
remote: ---- Removing routes from 1 old units ----
remote: ---> Removed route from unit d8a2d14948
remote:
remote: ---- Removing 1 old unit ----
remote: ---> Removed old unit 1/1
remote:
remote: OK
To git@tsuru.mycompany.com:helloweb.git
* [new branch] master -> master
如你所见,输出的最后一部分是相同的,应用已经运行在tsuru给定的地址了。
切换Java版本
在tsuru的Java平台中,用户可以使用两个版本的Java:Oracle提供的Java 7和8。通过JAVA_VERSION
环境变量可以选择你想使用的Java版本。缺省使用Java 7,但是可以通过运行下面的命令切换到Java 8:
$ tsuru env-set -a helloweb JAVA_VERSION=8
---- Setting 1 new environment variables ----
---- Starting 1 new unit ----
---> Started unit d8a2d14948...
---- Adding routes to 1 new units ----
---> Added route to unit d8a2d14948
---- Removing routes from 1 old units ----
---> Removed route from unit 4d155e805f
---- Removing 1 old unit ----
---> Removed old unit 1/1
完成!不需要再运行一次部署,应用已经运行在Java 8之上了。
设置应用的内存大小
在tsuru的Java平台,用户可以使用不同的计划的单元,每个计划可以包含内存不同的容器。定义Java堆内存最大值(以MB计算)可以通过环境变量JAVA_MAX_MEMORY
设置。变量缺省值为128(根据你的basebuilder
不同而不同)。
$ tsuru env-set -a helloweb JAVA_MAX_MEMORY=1024
---- Setting 1 new environment variables ----
---- Starting 1 new unit ----
---> Started unit o5p1k70289...
---- Adding routes to 1 new units ----
---> Added route to unit o5p1k70289
---- Removing routes from 1 old units ----
---> Removed route from unit d8a2d14948
---- Removing 1 old unit ----
---> Removed old unit 1/1
完成! 不需要再运行一次部署,应用已经被分配了更多的内存。
进一步探索
更多信息,可以查看tsuru文档
,或者阅读tsuru命令完全使用指南。