|
@@ -0,0 +1,143 @@
|
|
|
|
+package cn.com.qmth.examcloud.config.center;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.PrintStream;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
+import org.apache.logging.log4j.ThreadContext;
|
|
|
|
+import org.jline.reader.LineReader;
|
|
|
|
+import org.jline.reader.LineReaderBuilder;
|
|
|
|
+import org.jline.terminal.Terminal;
|
|
|
|
+import org.jline.terminal.TerminalBuilder;
|
|
|
|
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
+import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
+import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
|
|
|
+import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.helpers.BlackHolePrintStreamBuilder;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.config.center.core.BootstrapSecurityManager;
|
|
|
|
+import cn.com.qmth.examcloud.config.center.core.CommandInterpreter;
|
|
|
|
+import cn.com.qmth.examcloud.config.center.core.ConfigCenterBootstrap;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 配置中心启动类
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @date 2019年3月29日
|
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
|
+ */
|
|
|
|
+@SpringBootApplication
|
|
|
|
+@Configuration
|
|
|
|
+@ComponentScan(basePackages = {"cn.com.qmth"})
|
|
|
|
+@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class,
|
|
|
|
+ DataSourceAutoConfiguration.class})
|
|
|
|
+public class ConfigCenterStarter {
|
|
|
|
+
|
|
|
|
+ private static PrintStream stdOut = null;
|
|
|
|
+
|
|
|
|
+ private static PrintStream stdErrOut = null;
|
|
|
|
+
|
|
|
|
+ private static PrintStream blackHoleOut = null;
|
|
|
|
+
|
|
|
|
+ private static Long action = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 启动方法
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param args
|
|
|
|
+ */
|
|
|
|
+ public static void run(String[] args) {
|
|
|
|
+ ExamCloudLog log = ExamCloudLogFactory.getLog(ConfigCenterStarter.class);
|
|
|
|
+ ThreadContext.put("TRACE_ID", Thread.currentThread().getName());
|
|
|
|
+ log.info("Starting...");
|
|
|
|
+
|
|
|
|
+ blackHoleOut = BlackHolePrintStreamBuilder.build();
|
|
|
|
+ stdOut = System.out;
|
|
|
|
+ stdErrOut = System.err;
|
|
|
|
+
|
|
|
|
+ Terminal terminal = null;
|
|
|
|
+ LineReader reader = null;
|
|
|
|
+ try {
|
|
|
|
+ System.setOut(blackHoleOut);
|
|
|
|
+ System.setErr(blackHoleOut);
|
|
|
|
+ terminal = TerminalBuilder.terminal();
|
|
|
|
+ reader = LineReaderBuilder.builder().terminal(terminal).build();
|
|
|
|
+ System.setOut(stdOut);
|
|
|
|
+ System.setErr(stdErrOut);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ System.setOut(stdOut);
|
|
|
|
+ System.setErr(stdErrOut);
|
|
|
|
+ IOUtils.closeQuietly(terminal);
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ System.exit(-1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String secretKey = null;
|
|
|
|
+ while (true) {
|
|
|
|
+ try {
|
|
|
|
+ secretKey = reader.readLine("Enter secret key>", (char) 0);
|
|
|
|
+ System.setOut(blackHoleOut);
|
|
|
|
+ System.setErr(blackHoleOut);
|
|
|
|
+ args = (String[]) ArrayUtils.add(args,
|
|
|
|
+ "--examcloud.startup.secretKey=" + secretKey);
|
|
|
|
+
|
|
|
|
+ ConfigCenterBootstrap.run(ConfigCenterStarter.class, args);
|
|
|
|
+ System.setOut(stdOut);
|
|
|
|
+ System.setErr(stdErrOut);
|
|
|
|
+ break;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.setOut(stdOut);
|
|
|
|
+ System.setErr(stdErrOut);
|
|
|
|
+ System.out.println(e.getMessage());
|
|
|
|
+ System.out.println("Try again... ...");
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ System.out.println("I am running... ...");
|
|
|
|
+ String active = BootstrapSecurityManager.getInstance().getActive();
|
|
|
|
+ System.out.println("active=" + active);
|
|
|
|
+
|
|
|
|
+ new Thread() {
|
|
|
|
+ @Override
|
|
|
|
+ public void run() {
|
|
|
|
+ while (true) {
|
|
|
|
+ if (System.currentTimeMillis() - action > CommandInterpreter.getInstance()
|
|
|
|
+ .getSessionTimeout()) {
|
|
|
|
+ System.exit(0);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ TimeUnit.SECONDS.sleep(5);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ };
|
|
|
|
+ }.start();
|
|
|
|
+
|
|
|
|
+ IOUtils.closeQuietly(blackHoleOut);
|
|
|
|
+ action = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ while (true) {
|
|
|
|
+ action = System.currentTimeMillis();
|
|
|
|
+ String cmd = reader.readLine("$>");
|
|
|
|
+ if (cmd.equals("q")) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ CommandInterpreter.getInstance().interpret(cmd);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ IOUtils.closeQuietly(terminal);
|
|
|
|
+ System.exit(0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|