如何从chef recipe在后台运行java程序
我是厨师新手。我想创建一个配方,在后台运行一个jar如何从chef recipe在后台运行java程序,java,chef-infra,Java,Chef Infra,我是厨师新手。我想创建一个配方,在后台运行一个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 bash'run\u jar'do 代码最好将代码配置为作为服务运行。有几种包装
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
bash'run\u jar'do
代码最好将代码配置为作为服务运行。有几种包装器可用,例如:
完成此操作后,您可以配置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
您应该使用remote\u file
资源来获取文件,而不是在bashhere document块中使用wget
调试非常有用,如果您不确定,请启用它;-)(假设您使用的是chef solo)
chef solo-c solo.rb-j node.json-l debug
注意:我建议将文件下载到Chef::Config[:file\u cache\u path]
或/tmp
我可以使用远程\u文件
实现您想要的。配方如下所示:
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
我认为最好使用一个像主管这样的工具来完成它。
我用的是平衡管理器配方,你可以读一下
下面是一个示例代码:
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
马克,谢谢你的回复。它起作用了!我想创建一个微型服务。如果我必须自己编写服务,我更愿意使用现有的web容器,如tomcat、jetty。有没有其他可能以最简单的方式部署微服务?@kiwisw没有跨平台的方式。正如您所说的,将应用程序部署到Jetty或Tomcat可能是实现您的愿望的最简单的方法。谢天谢地,chef简化了设置。你知道dropwizard吗?我已经通过dropwizard提供了一项服务,这是我想由chef部署的。@kiwisw是的,我看过dropwizard,非常简洁的代码。我认为这一点仍然存在。让chef来配置运行时。尽管所有的酷孩子都在使用docker等,你的答案仍然是相关的。祝贺您。+1推荐远程_文件并将输出重定向到日志文件。shell的另一个改进可能是使用“nohup”运行java命令,以断开它与父进程的连接。我需要将dropwizard microservice运行的jar和yaml文件放在哪里,并给我一个带位置的示例recepie请根据一位维护人员的说法,主管“cookbook实际上已被弃用,等待重写。”它有十几个未决问题和PRs,一年半以来都没有更新。
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