diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/README.md" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..8e4480a8f0eed9d008b1f4ab19de7c07f5fc29b3 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/README.md" @@ -0,0 +1,53 @@ +# 一、需求概况 +现有某云主机服务器,用来做项目演示用,上面运行了docker应用,现希望有一总览页面,用来展示部署的应用。 +# 二、业务流程 + +```mermaid +graph LR +A(获取docker信息)--> B(模板生成页面)--> C(挂载到nginx) + +``` +/usr/share/nginx/html/index.html + +任务调度采用`crontab` + +演示地址:[http://124.71.129.204](http://124.71.129.204/) + + + + + + +# 三、Java工程打包 + +## 1. 一体化可执行包 + +配置文件:pom-deps.xml + +## 2. 带外部依赖lib的可执行包 + +配置文件:pom-lib.xml + + +## 3. 执行命令 +```shell +#完整打包 +mvn clean package + +#一体化可执行包 +mvn clean package -f pom-deps.xml + +#带外部依赖lib的可执行包 +mvn clean package -f pom-lib.xml +``` + +# 四、部署步骤 + +将`docker-show-jar-with-dependencies.jar`拷贝到服务器位置 `/work/gitcode/docker-run` + +输入`crontab -e`添加如下内容,实现每30分钟执行一次生成页面,并复制到nginx首页位置`/usr/share/nginx/html/index.html` + +```bash +*/10 * * * * java -jar /work/gitcode/docker-run/docker-show-jar-with-dependencies.jar --server.net.ip=124.71.129.204 +``` +这边的124.71.129.204为服务器ip。 \ No newline at end of file diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-deps.xml" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-deps.xml" new file mode 100644 index 0000000000000000000000000000000000000000..dcb862c252131bc6deecb486de4c60c8940d590d --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-deps.xml" @@ -0,0 +1,82 @@ + + + 4.0.0 + com.fly + docker-show + 0.0.1 + java-depend + http://maven.apache.org + jar + + + UTF-8 + UTF-8 + 1.8 + + + + org.freemarker + freemarker + 2.3.32 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.12.1 + + + org.apache.commons + commons-lang3 + 3.5 + + + org.projectlombok + lombok + 1.18.12 + provided + + + + ${project.artifactId} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + + maven-assembly-plugin + 3.5.0 + + false + + + com.fly.simple.TemplateRun + + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-lib.xml" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-lib.xml" new file mode 100644 index 0000000000000000000000000000000000000000..4feb584c26eff66da86f22b66031b2007a78e3f5 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom-lib.xml" @@ -0,0 +1,96 @@ + + + 4.0.0 + com.fly + docker-show + 0.0.1 + java-depend + http://maven.apache.org + jar + + + UTF-8 + UTF-8 + 1.8 + + + + org.freemarker + freemarker + 2.3.32 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.12.1 + + + org.apache.commons + commons-lang3 + 3.5 + + + org.projectlombok + lombok + 1.18.12 + provided + + + + ${project.artifactId} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.2 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/lib + lombok + runtime + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + + true + lib + com.fly.simple.MainRun + + + ./ + + + + + + + diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom.xml" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom.xml" new file mode 100644 index 0000000000000000000000000000000000000000..65da68604a382413c3ec8c1f346206f99e7be262 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/pom.xml" @@ -0,0 +1,124 @@ + + + 4.0.0 + com.fly + docker-show + 0.0.1 + java-depend + http://maven.apache.org + jar + + + UTF-8 + UTF-8 + 1.8 + + + + org.freemarker + freemarker + 2.3.32 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.12.1 + + + org.apache.commons + commons-lang3 + 3.5 + + + org.projectlombok + lombok + 1.18.12 + provided + + + + ${project.artifactId} + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + + maven-assembly-plugin + 3.5.0 + + true + + + com.fly.simple.TemplateRun + + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.2 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/lib + lombok + runtime + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.0 + + + + true + lib + com.fly.simple.MainRun + + + ./ + + + + + + + diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/MainRun.java" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/MainRun.java" new file mode 100644 index 0000000000000000000000000000000000000000..f22cf2755506ad563aba8cf5eb8c23ee5180e791 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/MainRun.java" @@ -0,0 +1,35 @@ +package com.fly.simple; + +import java.io.IOException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import com.fly.simple.utils.ShellExecutor; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class MainRun +{ + static ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1); + + /** + * 线程池保证程序一直运行 + * + * @param args + */ + public static void main(String[] args) + { + service.scheduleAtFixedRate(() -> { + try + { + log.info("######## {}", ShellExecutor.getDockerInfo()); + } + catch (IOException e) + { + log.error(e.getMessage(), e); + } + }, 2, 10, TimeUnit.SECONDS); + } +} diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/TemplateRun.java" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/TemplateRun.java" new file mode 100644 index 0000000000000000000000000000000000000000..34839fc0afce552c9930b685ba9409015ca2945c --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/TemplateRun.java" @@ -0,0 +1,83 @@ +package com.fly.simple; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; + +import com.fly.simple.utils.FreeMarkerUtil; +import com.fly.simple.utils.ShellExecutor; + +import freemarker.template.TemplateException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class TemplateRun +{ + public static void main(String[] args) + throws IOException, TemplateException, InterruptedException + { + // Jar运行,必须提供参数server.net.ip + URL url = TemplateRun.class.getProtectionDomain().getCodeSource().getLocation(); + String ip = null; + if (url.getPath().endsWith(".jar")) + { + if (args.length > 0) + { + ip = Stream.of(args).filter(arg -> arg.contains("--server.net.ip")).map(arg -> StringUtils.substringAfter(arg, "=")).collect(Collectors.joining()); + log.info("JarPath: {}, ServerIp: {}", url.getPath(), ip); + } + if (StringUtils.isBlank(ip)) + { + log.error("please start jar like:\n java -jar docker-show-jar-with-dependencies.jar --server.net.ip=124.71.129.204"); + return; + } + } + + // 写入文件 + if (SystemUtils.IS_OS_WINDOWS) + { + File file = new File("index.html"); + creatPage(ip, file); + + // 打开页面10秒后删除文件 + Runtime.getRuntime().exec("cmd /c start /min " + file.getCanonicalPath()); + TimeUnit.SECONDS.sleep(10); + file.deleteOnExit(); + return; + } + if (SystemUtils.IS_OS_LINUX) + { + // crontab -e + // */30 * * * * java -jar /work/gitcode/docker-run/docker-show-jar-with-dependencies.jar --server.net.ip=124.71.129.204 + creatPage(ip, new File("/usr/share/nginx/html/index.html")); + } + } + + private static void creatPage(String ip, File file) + throws IOException, TemplateException + { + // 收集docker信息 + Map model = new HashMap<>(3); + model.put("date", new Date()); + model.put("map", ShellExecutor.getDockerInfo()); + model.put("ip", StringUtils.defaultIfBlank(ip, "127.0.0.1")); + + // {mysql5=[3306, 13306], mysql8=[23306], redis-server=[6379]} + String content = FreeMarkerUtil.renderTemplate("/templates/index.html.ftl", model); + try (FileWriter writer = new FileWriter(file)) + { + writer.write(content); + writer.flush(); + } + } +} diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/FreeMarkerUtil.java" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/FreeMarkerUtil.java" new file mode 100644 index 0000000000000000000000000000000000000000..540f36fd5364682e487df3e2dea8adc9f3384cc4 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/FreeMarkerUtil.java" @@ -0,0 +1,66 @@ +package com.fly.simple.utils; + +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +/** + * + * FreeMarkers + * + * @author 00fly + * @version [版本号, 2017-4-4] + * @see [相关类/方法] + * @since [产品/模块版本] + */ +public class FreeMarkerUtil +{ + private static Configuration config; + + static + { + config = new Configuration(Configuration.VERSION_2_3_32); + config.setDefaultEncoding(StandardCharsets.UTF_8.name()); + } + + /** + * 获取模板填充model解析后的内容 + * + * @param template + * @param model + * @return + * @throws IOException + * @throws TemplateException + * @see [类、类#方法、类#成员] + */ + private static String renderTemplate(Template template, Map model) + throws TemplateException, IOException + { + StringWriter result = new StringWriter(); + template.process(model, result); + return result.toString(); + } + + /** + * 获取模板填充model后的内容 + * + * @param templatePath + * @param model + * @return + * @throws IOException + * @throws TemplateException + * @see [类、类#方法、类#成员] + */ + public static String renderTemplate(String templatePath, Map model) + throws TemplateException, IOException + { + config.setClassForTemplateLoading(FreeMarkerUtil.class, "/"); + Template template = config.getTemplate(templatePath); + return renderTemplate(template, model); + } +} \ No newline at end of file diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/ShellExecutor.java" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/ShellExecutor.java" new file mode 100644 index 0000000000000000000000000000000000000000..47742661f39285cc842c8d0514e1616525d76353 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/java/com/fly/simple/utils/ShellExecutor.java" @@ -0,0 +1,60 @@ +package com.fly.simple.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; + +public class ShellExecutor +{ + /** + * execute命令 + * + * @param command + * @throws IOException + * @see [类、类#方法、类#成员] + */ + public static List execute(String command) + throws IOException + { + List resultList = new ArrayList<>(); + String[] cmd = SystemUtils.IS_OS_WINDOWS ? new String[] {"cmd", "/c", command} : new String[] {"/bin/sh", "-c", command}; + Process ps = Runtime.getRuntime().exec(cmd); + try (InputStream in = ps.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in))) + { + String line; + while ((line = br.readLine()) != null) + { + resultList.add(line); + } + } + return resultList; + } + + public static Map> getDockerInfo() + throws IOException + { + // ribbon-user-1 8081/tcp + // ribbon-movie 8082/tcp + // ribbon-gateway 0.0.0.0:8085->8080/tcp, :::8085->8080/tcp + String dockerCmd = "docker ps --format \"{{.Names}} {{.Ports}}\""; + Map> map = new TreeMap<>(); + execute(dockerCmd).stream() + .filter(line -> StringUtils.contains(line, "->"))// 保留带端口映射的应用 + .map(line -> Collections.singletonMap(StringUtils.substringBefore(line, " "), + Stream.of(StringUtils.substringAfter(line, " ").split(",")).map(p -> StringUtils.substringBetween(p, ":", "->")).filter(StringUtils::isNotBlank).map(p -> p.replace(":", "")).sorted().collect(Collectors.toSet()))) + .forEach(it -> map.putAll(it)); + return map; + } +} diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/log4j2.xml" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/log4j2.xml" new file mode 100644 index 0000000000000000000000000000000000000000..0bb64bdebd9b53baff26563c2e2d6b5610ac701c --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/log4j2.xml" @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git "a/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/templates/index.html.ftl" "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/templates/index.html.ftl" new file mode 100644 index 0000000000000000000000000000000000000000..9beeabd2f1976e75db7aedde82368f61fcdedec6 --- /dev/null +++ "b/\346\234\215\345\212\241\345\231\250docker Web\345\272\224\347\224\250\344\270\200\350\247\210/src/main/resources/templates/index.html.ftl" @@ -0,0 +1,73 @@ + + + + + + +Welcome to nginx! + + + + + No. + Application + Ports + + <#list map?keys as key> + + ${key_index+1} + ${key} + + <#list map[key] as port> + ${port} + #list> + + + #list> + + 更新时间:${date?string('yyyy-MM-dd HH:mm:ss')} + + + + \ No newline at end of file