Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatible with nacos 2.2.0 #21

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.idea
target
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
61 changes: 37 additions & 24 deletions src/main/java/name/jervyshi/nacos/NacosStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -95,36 +100,44 @@ public NacosProcess start() {
List<String> 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());
command.add("-Dserver.port=" + serverPort);
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);

logger.info("Starting nacos server on port: {}", serverPort);
new NacosWaiter(host, serverPort).avoidUntilNacosServerStarted();
logger.info("Nacos server started");
}
} catch (NacosEmbeddedException e) {
// Log standard output and error stream
try {
List<String> outputLines = IOUtils.readLines(innerProcess.getInputStream(), "UTF-8");
outputLines.forEach(System.err::println);
List<String> 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;
Expand All @@ -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);
Expand All @@ -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");
}
}

Expand Down