在CloudFoundry上部署独立Java应用

昨天知道了一个Scala Web框架,感觉相当不错。

由于它是基于Netty的,不能通过Web容器部署,有点犹豫要不要用它,不知道能不能在CloudFoundry上发布。

之后搜到一篇关于在CloudFoundry上部署独立应用的文章,于是开始尝试。

首先,开发了一个简单的Socko示例应用,打完包以后目录结构大概是这样:

dist/
    main.jar
    lib/
        a.jar
        b.jar

其中IP和端口绑定代码为:

hostname = Option(System.getenv("VCAP_APP_HOST")).getOrElse("localhost"),
port = Option(System.getenv("VCAP_APP_PORT")).getOrElse("8080").toInt
理论上,我只需要把dist目录下的所有内容上传(vmc)到cloudfoundry上即可。

上传过程:

dist>vmc push msockotest
DL is deprecated, please use Fiddle
Instances> 1

1: grails
2: java_web
3: lift
4: node
5: play
6: rack
7: rails3
8: sinatra
9: spring
10: standalone
Framework> 10 // 选10,独立部署

1: java
2: java7
3: node
4: node06
5: node08
6: ruby18
7: ruby19
Runtime> 2  // 是Java7开发的

Custom startup command> java -jar study_socko_2.10-0.1.jar

1: 64M
2: 128M
3: 256M
Memory Limit> 64M // 示例应用,不改了

Creating mistsockotest... OK

1: msckotest.cloudfoundry.com
2: none
Domain> msockotest.cloudfoundry.com

Updating msockotest... OK

Create services for application?> n

Save configuration?> n

Uploading mistsockotest... FAILED
Upload failed. Try again with 'vmc push'.
TypeError: no implicit conversion of nil into String
For more information, see ~/.vmc/crash
结果却上传失败了,没有找到原因。替代方法是将启动命令写到脚本里:

startup.sh

CLASSPATH="./*:./lib/*"
export CLASSPATH

java $JAVA_OPTS helloexample.HelloApp

脚本放在dist下。然后把Custom startup command写成

./startup.sh

上传成功。再试,失败,然后无论怎么上传都是失败,就成功了一次。Google一下,发现很多人遇到这个问题,有人提出是新版本vmc的问题,于是卸载vmc0.5,安装0.4版:

gem uninstall vmc
uninstall tunnel-vmc-plugin

# gem install -v 0.3.21 vmc
gem install -v 0.4.0 vmc
重新上传
dist>vmc push msockotest
Instances> 1

10: standalone
Framework> 10

2: java7
Runtime> 2

Startup command> ./startup.sh

1: 64M
Memory Limit> 64M

Creating msockotest... OK

1: msockotest.cloudfoundry.com
2: none
URL> msockotest.cloudfoundry.com

Updating msockotest... OK

Create services for application?> n

Bind other services to application?> n

Save configuration?> n

Uploading msockotest... OK
Starting msockotest... OK
Checking msockotest... GAVE UP
Application failed to start.
上传成功,但启动失败了,于是查看日志:
dist>vmc files msockotest --path logs/err.log
DL is deprecated, please use Fiddle
Getting file contents... OK

# Logfile created on 2013-04-23 05:57:24 +0000 by logger.rb/25413
F, [2013-04-23T05:57:24.510280 #25207] FATAL -- : Memory limit of 64M exceeded.
F, [2013-04-23T05:57:24.510350 #25207] FATAL -- : Actual usage was 65M, process terminated.
好象是内存不够,于是把Memory Limit改成128M
dist>vmc delete msockotest
...
Deleting msockotest... OK

dist>vmc push msockotest
...
1: 64M
2: 128M
3: 256M
4: 512M
Memory Limit> 2

Creating msockotest... OK

...

Uploading msockotest... OK
Starting msockotest... OK
Checking msockotest... OK

成功,就是Checking的过程稍微有点漫长,吓得我以为又失败了呢。

dist>vmc files msockotest --path logs/stdout.log
DL is deprecated, please use Fiddle
Getting file contents... OK

./*:./lib/*
06:01:14.118 [HelloExampleActorSystem-akka.actor.default-dispatcher-4] [] INFO  akka.event.slf4j.Slf4jEventHandler  - Sl
f4jEventHandler started
06:01:14.228 [main] [] INFO  o.m.socko.webserver.WebServer  - Socko server '[WebServer, 172.30.49.146, 46824]' started o
n {}:{}
日志中可以看到这个应用实际绑定IP和端口。

用浏览器访问http://msockotest.cloudfoundry.com/,得到

Hello from Socko (Tue Apr 23 06:07:28 UTC 2013)
终于完成了,真曲折啊。

你可能感兴趣的:(CloudFoundry)