Java 当一个应用程序部署另一个应用程序时,Spring boot application.properties发生冲突
我有一个springboot应用程序,它监视并重新部署另一个springboot应用程序。两个应用程序都使用Java 当一个应用程序部署另一个应用程序时,Spring boot application.properties发生冲突,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个springboot应用程序,它监视并重新部署另一个springboot应用程序。两个应用程序都使用application.properties文件进行配置,但当主应用程序在出现故障时重新部署另一个应用程序时,辅助应用程序不会选择其自己的application.properties的配置 辅助应用程序配置了一个Spring boot actuator端点,当主应用程序重新部署时,该端点不会被激活(我想是因为它自己的应用程序.properties没有被拾取)。以下是启用致动器端点时应拾取的
application.properties
文件进行配置,但当主应用程序在出现故障时重新部署另一个应用程序时,辅助应用程序不会选择其自己的application.properties
的配置
辅助应用程序配置了一个Spring boot actuator端点,当主应用程序重新部署时,该端点不会被激活(我想是因为它自己的应用程序.properties
没有被拾取)。以下是启用致动器端点时应拾取的管线:
endpoints.metrics.enabled=true
endpoints.metrics.id=metrics
endpoints.metrics.sensitive=false
从主要应用程序的角度来看,这是我通过java代码执行的命令:
bash-c'java-jar文件的完整路径'&
并尝试将-Dspring.config.location=file:full\u path\u添加到\u appliction.properties\u 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脚本。我所做的是用cd/path/to/correct/environment/
编写一个shell脚本,并实际运行代码java-jar my client.jar
。根据性质,我选择是否在后台运行()。另外,我假设您不希望执行器线程等待客户端应用程序结束,所以我在示例中生成了一个新线程
你试过这个吗
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
是的,
ProcessExecutor
类正被另一个拥有ExecutorService
的类调用,该类通过从池中创建/重用线程来包装并调用它,因此我使用了与您非常类似的解决方案。关于脚本中的cd
,我记得最近使用过类似的解决方案,这似乎是问题的根本原因。我将再次尝试您的解决方案,但我通过调用DefaultExecutor上的setWorkingDirectory(pathofworkdirectory)
找到了一个解决方案。我将对此进行详细说明。使用cd
对解决方案进行了测试,到目前为止,该解决方案没有任何问题。我将使用这个解决方案,而不是setWorkingDirectory
one,因为后者需要对路径等进行一些解析。谢谢你的帮助!