從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程
項(xiàng)目背景
在java項(xiàng)目部署過程中,由于內(nèi)外部各種因素,可能會(huì)遇到一些感覺操作不便捷的場景,例如
- jar包未隨系統(tǒng)自動(dòng)啟動(dòng)需要每次手動(dòng)重啟
- 系統(tǒng)vpn堡壘機(jī)多重防御更新繁瑣
- 系統(tǒng)無圖形化界面命令行操作復(fù)雜
- 等等......
在工作中之前也總結(jié)了windows的Jar包部署工具與linux下的jar包自動(dòng)化部署腳本,這次就想著否能將二者統(tǒng)一結(jié)合,本著簡單/高效/功能專一的原則,做出一
個(gè)可視化jar包部署平臺(tái),JarManage應(yīng)運(yùn)而生
功能介紹
項(xiàng)目地址:https://gitee.com/code2roc/jar-manage
支持在線創(chuàng)建項(xiàng)目,上傳Jar包,自動(dòng)備份,配置啟動(dòng)參數(shù),注冊系統(tǒng)服務(wù),查看啟動(dòng)日志等功能,具有以下優(yōu)點(diǎn)
- 基于servlet開發(fā),依賴簡潔,部署包10MB左右
- 結(jié)合嵌入式tomcat一鍵部署,無外部容器依賴
- 使用h2db存儲(chǔ)數(shù)據(jù),無外部數(shù)據(jù)庫依賴
- 適配windows/linux平臺(tái),滿足多種環(huán)境
- 具體項(xiàng)目經(jīng)平臺(tái)部署后自動(dòng)注冊系統(tǒng)服務(wù),無需擔(dān)心服務(wù)器重啟
系統(tǒng)架構(gòu)圖如下
系統(tǒng)截圖展示
技術(shù)分析
平臺(tái)識(shí)別
首先通過系統(tǒng)os識(shí)別是windows平臺(tái)還是linux平臺(tái)
String os = System.getProperty("os.name").toLowerCase(); if (os.startsWith("win")) { platform = DepolyPlatform.Windows; }
通過system-release文件識(shí)別部分基于CentOS開發(fā)的Linux系統(tǒng)
String command = "cat /etc/system-release"; String result = CMDUtil.executeLinuxCommand(command); if (result.startsWith("Red Hat")) { platform = DepolyPlatform.LinuxRedHat; } else if (result.startsWith("CentOS")) { platform = DepolyPlatform.LinuxCentOS; } else if (result.startsWith("openEuler")) { platform = DepolyPlatform.LinuxOpenEuler; }
通過issue文件識(shí)別部分基于Ubuntu/Debian開發(fā)的Linux系統(tǒng)
command = "cat /etc/issue"; result = CMDUtil.executeLinuxCommand(command); if (!StringUtil.isEmpty(result)) { if (result.startsWith("Ubuntu")) { platform = DepolyPlatform.LinuxUbuntu; } else if (result.startsWith("Debian")) { platform = DepolyPlatform.LinuxDebian; } }
windows注冊服務(wù)
通過sc query命令判斷服務(wù)狀態(tài)
public String getStatus(String serviceName) { String status = DepolyStatus.UnInstall; try { String command = "sc query " + serviceName; String commandResultFilePath = CMDUtil.executeWindowCommandStoreFile(command); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath))); String line = reader.readLine(); while (line != null) { if (line.trim().startsWith("STATE")) { if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("1")) status = DepolyStatus.Stopped; else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("2")) status = DepolyStatus.Startting; else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("3")) status = DepolyStatus.Stopping; else if (line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim().equals("4")) status = DepolyStatus.Running; } line = reader.readLine(); } } catch (IOException e) { LogUtil.error(e); } return status; }
通過winsw這個(gè)開源項(xiàng)目配置exe和xml文件將jar包注冊為windows服務(wù),項(xiàng)目地址:https://github.com/winsw/winsw/
linux注冊服務(wù)
通過systemctl status命令判斷服務(wù)狀態(tài)
public String getStatus(String serviceName) { String status = DepolyStatus.UnInstall; try { String command = "systemctl status " + serviceName; String commandResultFilePath = CMDUtil.executeLinuxCommandWithStore(command); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(commandResultFilePath))); String line = reader.readLine(); while (line != null) { if (line.trim().startsWith("Active")) { if (line.trim().indexOf("inactive (dead)") > 0) status = DepolyStatus.Stopped; else if (line.trim().indexOf("active (running)") > 0) status = DepolyStatus.Running; else if (line.trim().indexOf("failed") > 0) status = DepolyStatus.Stopped; } line = reader.readLine(); } } catch (IOException e) { LogUtil.error(e); } return status; }
通過拷貝service文件到systemd/system目錄下注冊linux服務(wù)
yml配置文件識(shí)別
- maven配置
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.26</version> </dependency>
- 配置文件
jarmanage: port: 8555 username: admin password: abcd@1234 backupcount: 5
- 工具類
public static String getConfigValue(String configName){ String configValue = ""; try{ Yaml yaml = new Yaml(); InputStream resourceAsStream = new FileInputStream(new File("resources"+File.separator+"application.yml")); Map obj = yaml.load(resourceAsStream); Map<String,Object> param = (Map) obj.get("jarmanage"); configValue = ConvertUtil.convert2String(param.get(configName)); }catch (Exception e){ LogUtil.error(e); } return configValue; }
h2database使用
- maven引用
<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.1.214</version> </dependency>
- 工具類
public static Connection getConnection() throws Exception { File file = new File("database"); Connection conn = DriverManager.getConnection("jdbc:h2:file:" + file.getAbsolutePath() + File.separator + "manage", "root", "abcd@1234"); return conn; } public static void executeSQL(String sql) { try { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.execute(sql); stmt.close(); conn.close(); } catch (Exception e) { LogUtil.error(e); } }
servelt內(nèi)置tomcat打包
- maven引用
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>9.0.35</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-el</artifactId> <version>9.0.35</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.35</version> </dependency>
- 手動(dòng)啟動(dòng)
//啟動(dòng)tomcat服務(wù) // 1.創(chuàng)建一個(gè)內(nèi)嵌的Tomcat Tomcat tomcat = new Tomcat(); // 2.設(shè)置Tomcat端口 tomcat.setPort(8555); // 3.設(shè)置工作目錄,tomcat需要使用這個(gè)目錄進(jìn)行寫一些東西 final String baseDir = "workspace" + File.separator; tomcat.setBaseDir(baseDir); tomcat.getHost().setAutoDeploy(false); // 4. 設(shè)置webapp資源路徑 String webappDirLocation = "webapp" + File.separator; StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); // 5. 設(shè)置上下文路每徑 String contextPath = ""; ctx.setPath(contextPath); ctx.addLifecycleListener(new Tomcat.FixContextListener()); ctx.setName("jar-manage"); tomcat.getHost().addChild(ctx); //6.啟動(dòng) tomcat.getConnector(); tomcat.start(); tomcat.getServer().await();
- 打包包含引用類庫,自定義配置xml,指定運(yùn)行class
<plugins> <plugin> <!-- 打包包含引用 --> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptors> <!-- 自定義配置 --> <descriptor>package.xml</descriptor> </descriptors> <archive> <manifest> <!-- 運(yùn)行類 --> <mainClass>com.code2roc.jarmanage.Application</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd"> <!-- TODO: a jarjar format would be better --> <id>depoly</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>src/main/webapp/</directory> <outputDirectory>/webapp</outputDirectory> <includes> <include>**/**</include> </includes> </fileSet>
以上就是從零構(gòu)建可視化jar包部署平臺(tái)JarManage的詳細(xì)內(nèi)容,更多關(guān)于從零構(gòu)建可視化jar包部署平臺(tái)JarManage的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于jackson序列化和feign返回值的問題
這篇文章主要介紹了關(guān)于jackson序列化和feign返回值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03如何使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署
這篇文章主要介紹了使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解
這篇文章主要介紹了Java 實(shí)現(xiàn)FTP服務(wù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04