我有一个 Spring boot 应用程序,用于监视和重新部署另一个 Spring boot 应用程序。两个应用程序都使用 application.properties
文件进行配置,但是当主应用程序在发生故障时重新部署另一个应用程序时,辅助应用程序不会获取其自己的应用程序的配置。属性。
辅助应用程序配置了一个 Spring boot 执行器端点,当主应用程序重新部署时,该端点不会激活(我想是因为它自己的 application.properties
没有被拾取)。为了启用执行器端点,应该选取这些行:
endpoints.metrics.enabled=true
endpoints.metrics.id=metrics
endpoints.metrics.sensitive=false
从主应用程序的角度来看,这是我通过java代码执行的命令:
bash -c 'java -jar full_path_to_jar_file' &
并尝试将 -Dspring.config.location=file:full_path_to_appliction.properties_file
添加到此命令,但没有产生任何影响。
这是用于执行重新部署的java类是这样的:
package com.my.package;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class ProcessExecutor {
private static final Logger LOGGER =
LoggerFactory.getLogger(ProcessExecutor.class);
private final String command;
public ProcessExecutor(String command) {
Validate.notBlank(command);
this.command = command;
}
public void execute() {
LOGGER.debug("Command that will be executed: {}",
this.command);
CommandLine commandLine = CommandLine.parse(this.command);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(commandLine);
} catch (IOException e) {
LOGGER.error("Error restarting the process: {}",
e.getMessage());
}
}
}
当同一命令单独运行时,它可以正常工作并加载 application.properties
文件中的所有值。我可以做什么来从主应用程序正确地重新部署 jar?
最佳答案
我有类似的情况(重新启动 docker 容器或重新启动 python 脚本)。
我能够使用 ProcessBuilder
运行任何 shell 脚本。我所做的是编写一个 shell 脚本,其中包含 cd/path/to/Correct/environment/
和代码 java -jar my-client.jar
的实际运行。根据性质,我选择是否在后台运行它( refer to this )。另外,我假设您不希望执行程序线程等到客户端应用程序结束,因此我在示例中生成了一个新线程。
你试过这个吗?
public static class Slave implements Runnable {
ProcessExecutor pe;
public void run () {
try {
pe._execute();
} catch (Exception e) { pe.problemCallback(); }
}
}
public class ProcessExecutor {
private static final Logger LOGGER =
LoggerFactory.getLogger(ProcessExecutor.class);
private final String command;
public ProcessExecutor(String command) {
Validate.notBlank(command);
this.command = command;
}
public void execute() {
LOGGER.debug("Command that will be executed: {}",
this.command);
try {
Slave s = new Slave();
s.pe = this;
Thread t = new Thread(s);
t.start();
} catch (IOException e) {
LOGGER.error("Error restarting the process: {}",
e.getMessage());
}
}
public void _execute() {
ProcessBuilder pb = new ProcessBuilder ("/full/path/to/shell/script.sh");
try {
Process p = pb.start();
p.waitFor();
} catch (Exception e) {}
}
public void problemCallback () {
// do something with problem.
}
}
在 shell 脚本中,我使用 change-dir 命令生成一个 java 进程:
#!/bin/bash
# correct application.properties and jar file should be in
# /path/to/correct/environment/
cd /path/to/correct/environment/
java -jar my-client.jar # put & here if you want a background
# put disown if you don't want this to die with parent
关于java - 当一个应用程序部署另一个应用程序时,Spring Boot application.properties 发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51151874/