问题描述
我想使用java buildpack在cloudfoundry上运行spring应用程序作为一项一次性任务.
I would like to run a spring application on cloudfoundry as a one-off task using the java buildpack.
请注意,我的应用程序不是Web应用程序,而是使用Spring Cloud Task的Spring Batch应用程序.
Please note that my app is not a web application but a spring batch application that also uses spring cloud task.
这是我的清单:
---
buildpack: https://github.com/cloudfoundry/java-buildpack.git
memory: 1024M
env:
APPLICATION_URL: http://bignibou-server.cfapps.io/
APPLICATION_MAIL_NO_REPLY_ADDRESS: [email protected]
SPRING_PROFILES_ACTIVE: cloud
applications:
- name: bignibou-server
path: bignibou-server/build/libs/bignibou-server.jar
env:
APPLICATION_GOOGLE_API_KEY: ******************
- name: bignibou-batch
path: bignibou-batch/build/libs/bignibou-batch.jar
no-hostname: true
no-route: true
command: null
health-check-type: none
env:
JAVA_OPTS: -Dspring.batch.job.names=messagesDigestMailingJob
每当我推送该应用程序时,都会将该应用程序检测为辅助应用程序
Whenever I push the app, it is detected as a worker app:
App bignibou-batch is a worker, skipping route creation
这就是我想要的.
但是随后它尝试将批处理作为Web应用程序启动(仍在部署应用程序的同时)...请参阅:
But then it tries to start the batch as a web application (still whilst deploying the app)... See:
2017-03-24T22:33:50.828+01:00 [CELL/0] [OUT] Destroying container
2017-03-24T22:33:50.854+01:00 [API/3] [OUT] Process has crashed with type: "web"
2017-03-24T22:33:50.873+01:00 [API/3] [OUT] App instance exited with guid befc8bf2-d338-45e1-90b9-430ff3b09a3f payload: {"instance"=>"", "index"=>0, "reason"=>"CRASHED", "exit_description"=>"2 error(s) occurred:\n\n* Codependent step exited\n* cancelled", "crash_count"=>1, "crash_timestamp"=>1490391230813627797, "version"=>"8f92e459-a6c7-4558-bc0b-09ac398eb069"}
2017-03-24T22:33:51.473+01:00 [CELL/0] [OUT] Successfully destroyed container
2017-03-24T22:33:51.634+01:00 [CELL/0] [OUT] Creating container
2017-03-24T22:33:52.478+01:00 [CELL/0] [OUT] Successfully created container
2017-03-24T22:33:56.824+01:00 [APP/PROC/WEB/0] [OUT] JVM Memory Configuration: -XX:MaxMetaspaceSize=164239K -XX:ReservedCodeCacheSize=240M -XX:CompressedClassSpaceSize=25984K -Xmx295151K -XX:MaxDirectMemorySize=10M
2017-03-24T22:33:56.827+01:00 [APP/PROC/WEB/0] [ERR] JVM Memory Configuration: -XX:ReservedCodeCacheSize=240M -XX:CompressedClassSpaceSize=25984K -Xmx295151K -XX:MaxDirectMemorySize=10M -XX:MaxMetaspaceSize=164239K
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] . ____ _ __ _ _
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] ' |____| .__|_| |_|_| |_\__, | / / / /
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] =========|_|==============|___/=/_/_/_/
2017-03-24T22:33:59.073+01:00 [APP/PROC/WEB/0] [OUT] ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2017-03-24T22:33:59.075+01:00 [APP/PROC/WEB/0] [OUT] :: Spring Boot :: (v1.5.2.RELEASE)
2017-03-24T22:33:59.266+01:00 [APP/PROC/WEB/0] [OUT] 2017-03-24 21:33:59.256 INFO 16 --- [ main] pertySourceApplicationContextInitializer : Adding 'cloud' PropertySource to ApplicationContext
2017-03-24T22:33:59.381+01:00 [APP/PROC/WEB/0] [OUT] 2017-03-24 21:33:59.359 WARN 16 --- [ main] o.c.r.o.s.cloud.AbstractCloudConnector : No suitable service info creator found for service elasticsearch Did you forget to add a ServiceInfoCreator?
2017-03-24T22:33:59.389+01:00 [APP/PROC/WEB/0] [OUT] 2017-03-24 21:33:59.388 INFO 16 --- [ main] nfigurationApplicationContextInitializer : Adding cloud service auto-reconfiguration to ApplicationContext
2017-03-24T22:33:59.465+01:00 [APP/PROC/WEB/0] [OUT] 2017-03-24 21:33:59.451 INFO 16 --- [ main] com.bignibou.batch.Batch : Starting Batch on a1e87528-637d-4dae-62ab-0538e48fb49b with PID 16 (/home/vcap/app/BOOT-INF/classes started by vcap in /home/vcap/app)
这不是我想要的...
which is not what I want...
所以我的问题是:
- 如何配置清单以便正确地批处理应用而不自动启动-我只希望将二进制文件推送到cf,以便以后可以运行任务...
- 如何在cloudfoundry上作为cf任务来启动我的spring批处理?
推荐答案
要运行任务,您需要部署和完全上演一个应用程序(因此会有一个分支).然后,您可以cf run-task <app> <task-cmd>
.
In order to run a task, you need an app to be deployed and to be completely staged (so there's a droplet). You can then cf run-task <app> <task-cmd>
.
一些想法:
第一个选择是,您可以使用manifest.yml,如下所示:
First option, you could use a manifest.yml like this:
---
applications:
- name: spring-music
memory: 32M
path: build/libs/spring-music.war
health-check-type: none
no-route: true
command: while [ 1 == 1 ]; do sleep 9999; done
buildpack: java_buildpack
这将设置一个具有很小内存限制的应用程序,并且该命令本质上永远不会做任何事情(永远的部分是关键,因此该应用程序将完全启动并启动).我们还将运行状况检查设置为无且没有路由",这样就不会绑定任何路由,也不会进行TCP运行状况检查.您可以选择设置构建包.
This will setup an app with a very small memory limit and a command that essentially does nothing forever (the forever part is key so that the app will completely stage and start). We also set the health check to none and no route, so that no route is bound and no TCP health check is done. You can optionally set your build pack.
然后运行cf push
.该应用程序应登台并开始运行.之后,您可以根据需要多次cf run-task
次.您可以选择停止应用程序.
Then run cf push
. The app should stage and start OK. After that you can cf run-task
as many times as you want. You can optionally stop the app.
第二个选项,仅在不进行任何特殊manifest.yml配置的情况下推送您的应用程序.该应用程序应正常运行,无法开始运行,然后在失败后可以cf stop
该应用程序.从那里,您应该能够cf run-task
任意多次.
Second option, just push your app without making any special manifest.yml configurations. The app should stage properly, fail to start running then after it's failed you can cf stop
the app. From there, you should be able to cf run-task
as many times as you want.
您也许还可以使用 v3直接使用API ,这会给您带来更多的灵活性,但是此时所有交互都是通过cf curl
手动进行的.
You might also be able to use the v3 API directly, which would give you some additional flexibility, but all the interaction is manual via cf curl
at this point.
如果您想查看有关如何使用cf run-task
的具体示例,请参见Selwyn的答案.
If you want to see a concrete example of how to use cf run-task
, see Selwyn's answer.
这篇关于如何使用Java buildpack作为一次性任务在Cloud Foundry上运行Spring应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!