如何从厨师食谱在后台运行java程序

Posted

技术标签:

【中文标题】如何从厨师食谱在后台运行java程序【英文标题】:how to run a java program at background from chef recipe 【发布时间】:2013-08-27 04:04:27 【问题描述】:

我是一名厨师新手。我想创建一个在后台运行 jar 的配方。

bash 'run_jar' do
    code <<-EOH
    wget https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar -O hello.jar
    java -jar hello.jar &
    EOH
end

helloworld-1.0.jar 是一个程序首先打印“Hello World”,然后执行一个while(true) 循环。

我希望在我登录到厨师客户端机器时。它应该表明有一个使用“jps”命令运行的 jar。但是没有这样的jar运行。

我可以看到 hello.jar 已下载,这表明代码块已经执行。

这个食谱有什么问题?

【问题讨论】:

【参考方案1】:

我认为最好使用像主管这样的工具。 我正在使用平衡主管配方,您可以阅读它here。

这是一个示例代码:

include_recipe "supervisor"

service "supervisor" do
  supports :status => true, :restart => true
  action [:enable, :start]
end

# configure supervisor
supervisor_service "sample-java-app" do
  command   "java -jar hello.jar"
  directory <path-to-folder>
  environment <environment-hash>
  action [:enable, :start]
  autostart true
  autorestart true
  stdout_logfile /var/log/sample-java-app.log
  redirect_stderr true
  user root
  notifies :restart, 'service[supervisor]', :delayed
end

您可以使用以下命令控制您的服务:

$> supervisorctl start/stop/restart sample-java-app

【讨论】:

根据其中一位维护者的说法,主管“cookbook 实际上已被弃用,等待重写。”它有十几个未解决的问题和 PR,并且一年半没有更新。【参考方案2】:

您应该使用 remote_file 资源来获取文件,而不是在 bash here document 块中使用 wget

Debug 很有帮助,如果不确定,请启用它;-)(假设您使用的是 chef-solo)

chef-solo -c solo.rb -j node.json -l debug

注意:我建议将文件下载到Chef::Config[:file_cache_path]/tmp

我能够使用remote_file 实现您想要的。配方如下:

 temp=Chef::Config[:file_cache_path]

 remote_file "#Chef::Config[:file_cache_path]/hello.jar" do
     source "https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar"
     mode 00644
 end

 bash 'run_jar' do
     code <<-EOF
       /opt/jdk1.6.0_37/bin/java -jar #temp/hello.jar > #temp/hello.log 2>&1 &
     EOF
 end

在 EC2 测试盒上运行的示例(性能很糟糕...):

# chef-solo -c solo.rb -j node.json -l debug
[2013-08-26T02:51:08+00:00] INFO: Forking chef instance to converge...
[2013-08-26T02:51:08+00:00] DEBUG: Fork successful. Waiting for new chef pid: 18991
[2013-08-26T02:51:08+00:00] DEBUG: Forked instance now converging
Starting Chef Client, version 11.6.0
[2013-08-26T02:51:13+00:00] INFO: *** Chef 11.6.0 ***
[2013-08-26T02:51:37+00:00] DEBUG: Building node object for cloud.terry.im
[2013-08-26T02:51:37+00:00] DEBUG: Extracting run list from JSON attributes provided on command line
[2013-08-26T02:51:37+00:00] INFO: Setting the run_list to ["recipe[main]"] from JSON
[2013-08-26T02:51:37+00:00] DEBUG: Applying attributes from json file
[2013-08-26T02:51:37+00:00] DEBUG: Platform is ubuntu version 12.04
[2013-08-26T02:51:37+00:00] INFO: Run List is [recipe[main]]
[2013-08-26T02:51:37+00:00] INFO: Run List expands to [main]
[2013-08-26T02:51:37+00:00] INFO: Starting Chef Run for cloud.terry.im
[2013-08-26T02:51:37+00:00] INFO: Running start handlers
[2013-08-26T02:51:37+00:00] INFO: Start handlers complete.
[2013-08-26T02:51:37+00:00] DEBUG: No chefignore file found at /tmp/chef/cookbooks/chefignore no files will be ignored
Compiling Cookbooks...
[2013-08-26T02:51:37+00:00] DEBUG: Cookbooks to compile: [:main]
[2013-08-26T02:51:37+00:00] DEBUG: Loading Recipe main via include_recipe
[2013-08-26T02:51:37+00:00] DEBUG: Found recipe default in cookbook main
[2013-08-26T02:51:37+00:00] DEBUG: Loading from cookbook_path: /tmp/chef/cookbooks
Converging 2 resources
[2013-08-26T02:51:37+00:00] DEBUG: Converging node cloud.terry.im
Recipe: main::default
  * remote_file[/var/chef/cache/hello.jar] action create[2013-08-26T02:51:37+00:00] INFO: Processing remote_file[/var/chef/cache/hello.jar] action create (main::default line 3)
[2013-08-26T02:51:37+00:00] DEBUG: touching /var/chef/cache/hello.jar to create it
[2013-08-26T02:51:37+00:00] INFO: remote_file[/var/chef/cache/hello.jar] created file /var/chef/cache/hello.jar

    - create new file /var/chef/cache/hello.jar[2013-08-26T02:51:37+00:00] DEBUG: remote_file[/var/chef/cache/hello.jar] checking for changes
[2013-08-26T02:51:37+00:00] DEBUG: Cache control headers: 
[2013-08-26T02:51:37+00:00] DEBUG: Sending HTTP Request via GET to github.com:443/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar
[2013-08-26T02:51:38+00:00] DEBUG: Streaming download from https://github.com/kiwiwin/jar-repo/releases/download/kiwi/helloworld-1.0.jar to tempfile /tmp/chef-rest20130826-18991-11kv5ag
[2013-08-26T02:51:38+00:00] DEBUG: Following redirect 1/10
[2013-08-26T02:51:38+00:00] DEBUG: Sending HTTP Request via GET to s3.amazonaws.com:443/github-cloud/releases/12257402/d34edcec-0ba1-11e3-9ef0-268967c7e46f.jar
[2013-08-26T02:51:39+00:00] DEBUG: Streaming download from https://s3.amazonaws.com/github-cloud/releases/12257402/d34edcec-0ba1-11e3-9ef0-268967c7e46f.jar?response-content-disposition=attachment%3B%20filename%3Dhelloworld-1.0.jar&AWSAccessKeyId=AKIAISTNZFOVBIJMK3TQ&Expires=1377485552&Signature=ahoXNIhegXTdEvUE0FoimC34x%2Bg%3D to tempfile /tmp/chef-rest20130826-18991-1ojysmn
[2013-08-26T02:51:39+00:00] DEBUG: reading modes from /var/chef/cache/hello.jar file
[2013-08-26T02:51:39+00:00] DEBUG: applying mode = 640, uid = 0, gid = 0 to /tmp/chef-rest20130826-18991-1ojysmn
[2013-08-26T02:51:39+00:00] DEBUG: moving temporary file /tmp/chef-rest20130826-18991-1ojysmn into place at /var/chef/cache/hello.jar
[2013-08-26T02:51:39+00:00] INFO: remote_file[/var/chef/cache/hello.jar] updated file contents /var/chef/cache/hello.jar

    - update content in file /var/chef/cache/hello.jar from none to 9d1706
        (new content is binary, diff output suppressed)[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode
[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode
[2013-08-26T02:51:39+00:00] DEBUG: found target_uid == nil, so no owner was specified on resource, not managing owner
[2013-08-26T02:51:39+00:00] DEBUG: found target_gid == nil, so no group was specified on resource, not managing group
[2013-08-26T02:51:39+00:00] DEBUG: found target_uid == nil, so no owner was specified on resource, not managing owner
[2013-08-26T02:51:39+00:00] DEBUG: found target_gid == nil, so no group was specified on resource, not managing group
[2013-08-26T02:51:39+00:00] DEBUG: found current_mode == nil, so we are creating a new file, updating mode
[2013-08-26T02:51:39+00:00] INFO: remote_file[/var/chef/cache/hello.jar] mode changed to 644

    - change mode from '' to '0644'[2013-08-26T02:51:39+00:00] DEBUG: selinux utilities can not be found. Skipping selinux permission fixup.


  * bash[run_jar] action run[2013-08-26T02:51:39+00:00] INFO: Processing bash[run_jar] action run (main::default line 8)
[2013-08-26T02:51:39+00:00] DEBUG: Platform ubuntu version 12.04 found
[2013-08-26T02:51:41+00:00] INFO: bash[run_jar] ran successfully

    - execute "bash"  "/tmp/chef-script20130826-18991-jvx1bi"

[2013-08-26T02:51:41+00:00] INFO: Chef Run complete in 3.688815399 seconds
[2013-08-26T02:51:41+00:00] INFO: Running report handlers
[2013-08-26T02:51:41+00:00] INFO: Report handlers complete
Chef Client finished, 2 resources updated
[2013-08-26T02:51:44+00:00] DEBUG: Forked child successfully reaped (pid: 18991)
[2013-08-26T02:51:44+00:00] DEBUG: Exiting
root@cloud:/tmp/chef# cat /var/chef/cache/hello.log
Hello World

【讨论】:

+1 用于推荐 remote_file 并将输出重定向到日志文件。另一个 shell 改进可能是使用“nohup”运行 java 命令以断开它与父进程的连接。 我需要在哪里放置 jar 和 yaml 文件以运行 dropwizard 微服务,并请给我带有位置的示例接收【参考方案3】:

最好将您的代码配置为作为服务运行。几个可用的包装器,例如:

http://wrapper.tanukisoftware.com/doc/english/app-hello-world-server.html

完成后,您可以配置 chef 来管理新服务:

service "myapp_service" do
  supports :status => true, :restart => true
  start_command "/usr/lib/myapp/bin/myapp start"
  restart_command "/usr/lib/myapp/bin/myapp restart"
  status_command "/usr/lib/myapp/bin/myapp status"
  action [ :enable, :start ]
end

【讨论】:

马克,谢谢你的回复。有用!我想创建一个微服务。如果我必须自己编写服务,我更喜欢使用现有的 web 容器,如 tomcat、jetty。有没有其他可能以最简单的方式部署微服务? @kiwisw 不是跨平台的。正如您所说,将应用程序部署到 Jetty 或 Tomcat 可能是实现您的愿望的最简单方法。值得庆幸的是,厨师使设置变得容易。 你知道dropwizard吗?我已经通过 dropwizard 提供了一项服务,这就是我想由厨师部署的服务。 @kiwisw 是的,我见过 dropwizard,非常简洁的一段代码。我认为这一点仍然成立。让厨师只配置运行时。 尽管所有酷孩子都在使用 docker 等,但您的答案仍然很重要。恭喜。

以上是关于如何从厨师食谱在后台运行java程序的主要内容,如果未能解决你的问题,请参考以下文章

饭店的运作模式(线程线程池任务)

python 并发编程

如何在系统(unix/Linux)后台运行java程序?

如何使 Java 桌面应用程序永远不会让系统休眠并在后台运行?

如何在基于servlet的Web应用程序中运行后台任务?

如何使用 Ruby 块在厨师食谱中分配变量