diff --git a/.gitignore b/.gitignore index a1c2a23..ece5808 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +.idea +target \ No newline at end of file diff --git a/pom.xml b/pom.xml index 29b6d70..5f76a89 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,11 @@ fastjson 1.2.51 + + commons-io + commons-io + 2.7 + junit junit diff --git a/src/main/java/name/jervyshi/nacos/NacosStarter.java b/src/main/java/name/jervyshi/nacos/NacosStarter.java index d78e2ca..a10982d 100644 --- a/src/main/java/name/jervyshi/nacos/NacosStarter.java +++ b/src/main/java/name/jervyshi/nacos/NacosStarter.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,32 +38,35 @@ /** * The type Nacos starter. + * * @author JervyShi * @version $Id : NacosStarter.java, v 0.1 2019-02-08 17:18 JervyShi Exp $$ */ public class NacosStarter { - /** logger */ + /** + * logger + */ private static final Logger logger = LoggerFactory.getLogger(NacosStarter.class); - private String nacosVersion; + private String nacosVersion; - private Path downloadPath; + private Path downloadPath; - private Path nacosHomePath; + private Path nacosHomePath; - private AtomicBoolean started; + private AtomicBoolean started; - private NacosProcess nacosProcess; + private NacosProcess nacosProcess; - private String host; + private String host; - private NacosPorts nacosPorts; + private NacosPorts nacosPorts; /** * Instantiates a new Nacos starter. * - * @param nacosVersion the nacos version + * @param nacosVersion the nacos version * @param downloadPath the download path */ NacosStarter(String nacosVersion, Path downloadPath, String host, NacosPorts nacosPorts) { @@ -79,13 +83,14 @@ public class NacosStarter { * @return the nacos process */ public NacosProcess start() { + Process innerProcess = null; try { logger.info("Start new nacos process."); checkInitialState(); if (started.compareAndSet(false, true)) { nacosHomePath = Paths.get(downloadPath.toAbsolutePath().toString(), - String.valueOf(nacosPorts.getServerPort()), "nacos"); + String.valueOf(nacosPorts.getServerPort()), "nacos"); if (!isBinaryDownloaded()) { downloadAndUnpackBinary(); } @@ -95,10 +100,10 @@ public NacosProcess start() { List command = new ArrayList<>(); if (isWindows()) { command.add(Paths.get(System.getenv("JAVA_HOME"), "bin", "java.exe") - .toAbsolutePath().toString()); + .toAbsolutePath().toString()); } else { command.add(Paths.get(System.getenv("JAVA_HOME"), "bin", "java") - .toAbsolutePath().toString()); + .toAbsolutePath().toString()); } command.add("-Dnacos.standalone=true"); command.add("-Dnacos.home=" + nacosHomePath.toAbsolutePath().toString()); @@ -106,15 +111,12 @@ public NacosProcess start() { command.add("-jar"); command.add(getAbsolutePath("target", "nacos-server.jar")); command.add( - "--spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:" - + getAbsolutePath("conf") + File.separator); + "--spring.config.location=classpath:/,optional:classpath:/config/,optional:file:./,optional:file:./config/,optional:file:" + + getAbsolutePath("conf") + File.separator); command.add("--logging.config=" + getAbsolutePath("conf", "nacos-logback.xml")); - Process innerProcess = new ProcessBuilder().directory(nacosHomePath.toFile()) - .command(command).inheritIO().redirectOutput(Redirect.PIPE).start(); - - // TODO log process result - innerProcess.getInputStream(); + innerProcess = new ProcessBuilder().directory(nacosHomePath.toFile()) + .command(command).inheritIO().redirectOutput(Redirect.PIPE).start(); nacosProcess = new NacosProcess(host, nacosPorts, innerProcess); @@ -122,9 +124,20 @@ public NacosProcess start() { new NacosWaiter(host, serverPort).avoidUntilNacosServerStarted(); logger.info("Nacos server started"); } + } catch (NacosEmbeddedException e) { + // Log standard output and error stream + try { + List outputLines = IOUtils.readLines(innerProcess.getInputStream(), "UTF-8"); + outputLines.forEach(System.err::println); + List errorLines = IOUtils.readLines(innerProcess.getErrorStream(), "UTF-8"); + errorLines.forEach(System.err::println); + } catch (IOException ex) { + throw new RuntimeException(ex); + } } catch (IOException e) { logger.error("fail to start nacos process, nacosVersion: {}, downloadPath:{}", - nacosVersion, downloadPath, e); + nacosVersion, downloadPath, e); + // Print error stream if there is any. throw new NacosEmbeddedException("fail to start nacos process"); } return nacosProcess; @@ -139,7 +152,7 @@ private void downloadAndUnpackBinary() throws IOException { } String unzipLocation = Paths.get(downloadPath.toAbsolutePath().toString(), - String.valueOf(nacosPorts.getServerPort())).toAbsolutePath().toString(); + String.valueOf(nacosPorts.getServerPort())).toAbsolutePath().toString(); logger.info("Unzip nacos archive files into: {}", unzipLocation); ZipUtil.unzip(filePath.toAbsolutePath().toString(), unzipLocation); @@ -155,19 +168,19 @@ private boolean isBinaryDownloaded() { private Path getDownloadFilePath() { return Paths.get(downloadPath.toAbsolutePath().toString(), - String.format("nacos-server-%s.zip", nacosVersion)); + String.format("nacos-server-%s.zip", nacosVersion)); } private File getNacosServerJar() { Path path = Paths.get(nacosHomePath.toAbsolutePath().toString(), "target", - "nacos-server.jar"); + "nacos-server.jar"); return path.toFile(); } private void checkInitialState() { if (started.get()) { throw new NacosEmbeddedException( - "This Nacos Starter already started nacos server. Create new Nacos Server Instance"); + "This Nacos Starter already started nacos server. Create new Nacos Server Instance"); } }