- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java實(shí)現Linux(centos) 中docker容器下命令交互的代碼
開(kāi)發(fā)需求: 因系統程序部署時(shí),經(jīng)常是拆分部署(多臺機器) ,手工部署費時(shí)費力,且每次都要手工配置系統參數(系統提供配置向導)。
如下圖所示:
1)進(jìn)行main容器 -> 2)執行系統配置向導 -> 3)選擇服務(wù)器模式 -> 4) 選擇web控制臺....然后進(jìn)行具體的服務(wù)器IP設置。
為了解放雙手,用java實(shí)現了Linux(centos) 下docker 應用程序的命令交互。
具體代碼如下:
import java.io.*; /** * @author by dujiajun * @date 2021/4/29. */ public class TestMain extends Thread{ //進(jìn)入docker main private static String [] cmds = {"docker","exec","-i","main","bash"}; private Process pro; //初始化各機器IP信息 private String role = ""; private String webIp = ""; private String redisIp = ""; private String beanstalkdIp = ""; private String pgIp = ""; //main應用重啟 public static String [] cmdRestart = {"/bin/sh","-c","docker restart main"}; public void cmdRun(String[] machines) throws Exception { //執行xx系統配置向導命令并生成進(jìn)程 //pro = Runtime.getRuntime().exec(cmds); ProcessBuilder pb = new ProcessBuilder(cmds); /* pb.inheritIO(); pb.redirectErrorStream(true);*/ pro = pb.start(); System.out.println("執行前pro1:" + pro.isAlive()); //解析machines信息 if (machines.length > 0) { for (int i = 0; i < machines.length; i++) { int strEndIndex = machines[i].indexOf(":"); String str = machines[i].substring(0, strEndIndex); String content = machines[i].substring(strEndIndex + 1); switch (str.trim()) { case "role": role = content; break; //web服務(wù)器IP case "webIp": webIp = content; break; //redis服務(wù)器IP case "redisIp": redisIp = content; break; //redis服務(wù)器IP case "beanstalkdIp": beanstalkdIp = content; break; //beanstalkd服務(wù)器IP case "pgIp": pgIp = content; break; //beanstalkd服務(wù)器IP default: break; } } } new Thread() { public void run() { try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream()))) { if (role != null && role.equals("web-server")) {//系統web控制臺 System.out.println("*********進(jìn)入web控制臺配置向導*********"); //系統配置向導命令集-web String[] strCmdWeb = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//系統配置向導 "server-type", //服務(wù)器模式 "web-server", //管理服務(wù)器:提供管理功能及應用功能 "allow-deploy-api-server 0", //設置是否允許部署應用服務(wù)器 1允許 0不允許 "cache-info " + redisIp + ":6379", //緩存服務(wù)器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "report-server-db-info " + pgIp + ":5432", //設置報表數據庫 "sfmg-db-info " + pgIp + ":5432", //設置軟件管家數據庫 "web-server-port " + webIp + ":8080",//設置管理服務(wù)器端口 "commit",//提交 "exit" }; //查看進(jìn)程是否還存活 System.out.println("執行前pro2:" + pro.isAlive()); for (String strWeb : strCmdWeb) { synchronized (bw) { bw.write(strWeb.trim()); bw.newLine(); System.out.println("step: " + strWeb); bw.wait(1000); } } //查看進(jìn)程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); bw.close(); System.out.println(" bw.close();"); System.out.println("web配置向導設置成功!"); } else if (role != null && role.equals("api-server")) {//系統app應用 //系統配置向導命令集-APP System.out.println("*********進(jìn)入APP服務(wù)器控制臺配置向導*********"); String[] strCmdApp = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//系統配置向導 "server-type", //服務(wù)器模式 "api-server", //APP服務(wù)器 "cache-info " + redisIp + ":6379", //緩存服務(wù)器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "web-server-info " + webIp + ":8080", //設置管理服務(wù)器端口 "commit",//提交配置 "exit" }; for (String str : strCmdApp) { synchronized (bw) { bw.write(str.trim()); bw.newLine(); System.out.println("step: " + str); bw.wait(1000); } } //查看進(jìn)程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); System.out.println("app配置向導設置成功!"); bw.close(); } else if (role != null && role.equals("log-analyze-server")) {//系統日志分析服務(wù)器 //系統配置向導命令集-log-analyze-server System.out.println("*********進(jìn)入日志分析服務(wù)器控制臺配置向導*********"); String[] strCmdLog = { "python /opt/tools/skylar_cli/bin/skylar_cli.pyc",//天擎配置向導 "server-type", //服務(wù)器模式 "log-analyze-server", //管理服務(wù)器:提供管理功能及應用功能 "cache-info " + redisIp + ":6379", //緩存服務(wù)器 "db-info " + pgIp + ":5432", //設置數據庫信息 "queue-info " + beanstalkdIp + ":11300", //設置隊列信息 "web-server-info " + webIp + ":8080", //設置管理服務(wù)器端口 "sfmg-db-info " + pgIp + ":5432", //設置軟件管家數據庫 "commit",//提交配置 "exit" }; //順序執行配置向導命令 for (String str : strCmdLog) { synchronized (bw) { bw.write(str.trim()); bw.newLine(); System.out.println("step: " + str); bw.wait(1000); } } //測試進(jìn)程是否還存活 System.out.println("pro3:" + pro.isAlive()); bw.flush();//緩存提交 System.out.println("緩存提交!"); System.out.println("日志分析服務(wù)器配置向導設置成功!"); bw.close(); } } catch (IOException | InterruptedException e) { //pro.destroyForcibly(); e.printStackTrace(); //pro.destroy(); System.out.println("e.getMessage(): " + e.getMessage()); } finally { try { Process process = Runtime.getRuntime().exec(cmdRestart);//重啟main System.out.println("process.isAlive:" + process.isAlive()); System.out.println("重啟main成功!"); } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread(){ public void run(){ BufferedReader br = new BufferedReader(new InputStreamReader(pro.getErrorStream())); String cmdout = ""; try { cmdout = br.readLine(); while(cmdout!= null&&(!cmdout.equals("exit"))){ System.out.println(cmdout); } } catch (IOException e) { //System.out.println("br:pro.destroy();"); //pro.destroy(); e.printStackTrace(); System.out.println("e.printStackTrace();"); } try { System.out.println(" br.close();"); br.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); pro.waitFor();//進(jìn)程等待 } public static void main(String[] args) throws Exception { TestMain testMain = new TestMain(); /* //測試用 String[] machines ={ "role:web-server", "webIp:xx.xx.xx.110", "redisIp:xx.xx.xx.211", "beanstalkdIp:xx.xx.xx.211", "pgIp:xx.xx.xx.212" };*/ testMain.cmdRun(args); /* System.exit(0); System.out.println("System.exit(0);");*/ } }
特別注意:
private static String [] cmds = {"docker","exec","-i","main","bash"};
一定要使用 docker exec -i main bash ,不要使用 -it ,否則會(huì )報錯 cannot enable tty mode on non tty input。
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Run a command in a running container -i, --interactive=false Keep STDIN open even if not attached -t, --tty=false Allocate a pseudo-TTY
打成jar包,執行jar包:
終于看到久違的部署成功界面~
到此這篇關(guān)于java實(shí)現Linux(centos) 中docker容器下命令交互(配置向導)的文章就介紹到這了,更多相關(guān)docker容器命令交互內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站