java實現(xiàn)Linux(centos) 中docker容器下命令交互的代碼(配置向?qū)В?/h1>
瀏覽:2日期:2022-08-12 17:11:22
開發(fā)需求: 因系統(tǒng)程序部署時,經(jīng)常是拆分部署(多臺機器) ,手工部署費時費力,且每次都要手工配置系統(tǒng)參數(shù)(系統(tǒng)提供配置向?qū)?。
如下圖所示:
1)進行main容器 -> 2)執(zhí)行系統(tǒng)配置向?qū)?-> 3)選擇服務器模式 -> 4) 選擇web控制臺....然后進行具體的服務器IP設置。
![java實現(xiàn)Linux(centos) 中docker容器下命令交互的代碼(配置向?qū)В? src=]()
為了解放雙手,用java實現(xiàn)了Linux(centos) 下docker 應用程序的命令交互。
具體代碼如下:
import java.io.*; /** * @author by dujiajun * @date 2021/4/29. */public class TestMain extends Thread{ //進入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 {//執(zhí)行xx系統(tǒng)配置向?qū)畈⑸蛇M程//pro = Runtime.getRuntime().exec(cmds);ProcessBuilder pb = new ProcessBuilder(cmds); /* pb.inheritIO();pb.redirectErrorStream(true);*/pro = pb.start();System.out.println('執(zhí)行前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服務器IP case 'webIp':webIp = content;break; //redis服務器IP case 'redisIp':redisIp = content;break; //redis服務器IP case 'beanstalkdIp':beanstalkdIp = content;break; //beanstalkd服務器IP case 'pgIp':pgIp = content;break; //beanstalkd服務器IP default:break;} }} new Thread() { public void run() {try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream()))) { if (role != null && role.equals('web-server')) {//系統(tǒng)web控制臺System.out.println('*********進入web控制臺配置向?qū)?********');//系統(tǒng)配置向?qū)罴?webString[] strCmdWeb = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//系統(tǒng)配置向?qū)?server-type', //服務器模式'web-server', //管理服務器:提供管理功能及應用功能'allow-deploy-api-server 0', //設置是否允許部署應用服務器 1允許 0不允許'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'report-server-db-info ' + pgIp + ':5432', //設置報表數(shù)據(jù)庫'sfmg-db-info ' + pgIp + ':5432', //設置軟件管家數(shù)據(jù)庫'web-server-port ' + webIp + ':8080',//設置管理服務器端口'commit',//提交'exit'}; //查看進程是否還存活System.out.println('執(zhí)行前pro2:' + pro.isAlive());for (String strWeb : strCmdWeb) { synchronized (bw) {bw.write(strWeb.trim());bw.newLine();System.out.println('step: ' + strWeb);bw.wait(1000); }} //查看進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');bw.close();System.out.println(' bw.close();');System.out.println('web配置向?qū)гO置成功!'); } else if (role != null && role.equals('api-server')) {//系統(tǒng)app應用//系統(tǒng)配置向?qū)罴?APPSystem.out.println('*********進入APP服務器控制臺配置向?qū)?********');String[] strCmdApp = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//系統(tǒng)配置向?qū)?server-type', //服務器模式'api-server', //APP服務器'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'web-server-info ' + webIp + ':8080', //設置管理服務器端口'commit',//提交配置'exit'}; for (String str : strCmdApp) { synchronized (bw) {bw.write(str.trim());bw.newLine();System.out.println('step: ' + str);bw.wait(1000); }}//查看進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');System.out.println('app配置向?qū)гO置成功!');bw.close(); } else if (role != null && role.equals('log-analyze-server')) {//系統(tǒng)日志分析服務器//系統(tǒng)配置向?qū)罴?log-analyze-serverSystem.out.println('*********進入日志分析服務器控制臺配置向?qū)?********');String[] strCmdLog = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//天擎配置向?qū)?server-type', //服務器模式'log-analyze-server', //管理服務器:提供管理功能及應用功能'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'web-server-info ' + webIp + ':8080', //設置管理服務器端口'sfmg-db-info ' + pgIp + ':5432', //設置軟件管家數(shù)據(jù)庫'commit',//提交配置'exit' }; //順序執(zhí)行配置向?qū)頵or (String str : strCmdLog) { synchronized (bw) {bw.write(str.trim());bw.newLine();System.out.println('step: ' + str);bw.wait(1000); }}//測試進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');System.out.println('日志分析服務器配置向?qū)гO置成功!');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);//重啟mainSystem.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();//進程等待 } 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 ,否則會報錯 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包,執(zhí)行jar包:
![java實現(xiàn)Linux(centos) 中docker容器下命令交互的代碼(配置向?qū)В? src=]()
終于看到久違的部署成功界面~
![java實現(xiàn)Linux(centos) 中docker容器下命令交互的代碼(配置向?qū)В? src=]()
到此這篇關于java實現(xiàn)Linux(centos) 中docker容器下命令交互(配置向?qū)В┑奈恼戮徒榻B到這了,更多相關docker容器命令交互內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
標簽:
Java
相關文章:
1. .NET SkiaSharp 生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)2. 如何在jsp界面中插入圖片3. JSP數(shù)據(jù)交互實現(xiàn)過程解析4. jsp實現(xiàn)登錄界面5. XML基本概念XPath、XSLT與XQuery函數(shù)介紹6. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案7. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)8. ASP.NET泛型三之使用協(xié)變和逆變實現(xiàn)類型轉(zhuǎn)換9. PHP循環(huán)與分支知識點梳理10. AspNetCore&MassTransit Courier實現(xiàn)分布式事務的詳細過程
開發(fā)需求: 因系統(tǒng)程序部署時,經(jīng)常是拆分部署(多臺機器) ,手工部署費時費力,且每次都要手工配置系統(tǒng)參數(shù)(系統(tǒng)提供配置向?qū)?。
如下圖所示:
1)進行main容器 -> 2)執(zhí)行系統(tǒng)配置向?qū)?-> 3)選擇服務器模式 -> 4) 選擇web控制臺....然后進行具體的服務器IP設置。
為了解放雙手,用java實現(xiàn)了Linux(centos) 下docker 應用程序的命令交互。
具體代碼如下:
import java.io.*; /** * @author by dujiajun * @date 2021/4/29. */public class TestMain extends Thread{ //進入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 {//執(zhí)行xx系統(tǒng)配置向?qū)畈⑸蛇M程//pro = Runtime.getRuntime().exec(cmds);ProcessBuilder pb = new ProcessBuilder(cmds); /* pb.inheritIO();pb.redirectErrorStream(true);*/pro = pb.start();System.out.println('執(zhí)行前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服務器IP case 'webIp':webIp = content;break; //redis服務器IP case 'redisIp':redisIp = content;break; //redis服務器IP case 'beanstalkdIp':beanstalkdIp = content;break; //beanstalkd服務器IP case 'pgIp':pgIp = content;break; //beanstalkd服務器IP default:break;} }} new Thread() { public void run() {try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(pro.getOutputStream()))) { if (role != null && role.equals('web-server')) {//系統(tǒng)web控制臺System.out.println('*********進入web控制臺配置向?qū)?********');//系統(tǒng)配置向?qū)罴?webString[] strCmdWeb = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//系統(tǒng)配置向?qū)?server-type', //服務器模式'web-server', //管理服務器:提供管理功能及應用功能'allow-deploy-api-server 0', //設置是否允許部署應用服務器 1允許 0不允許'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'report-server-db-info ' + pgIp + ':5432', //設置報表數(shù)據(jù)庫'sfmg-db-info ' + pgIp + ':5432', //設置軟件管家數(shù)據(jù)庫'web-server-port ' + webIp + ':8080',//設置管理服務器端口'commit',//提交'exit'}; //查看進程是否還存活System.out.println('執(zhí)行前pro2:' + pro.isAlive());for (String strWeb : strCmdWeb) { synchronized (bw) {bw.write(strWeb.trim());bw.newLine();System.out.println('step: ' + strWeb);bw.wait(1000); }} //查看進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');bw.close();System.out.println(' bw.close();');System.out.println('web配置向?qū)гO置成功!'); } else if (role != null && role.equals('api-server')) {//系統(tǒng)app應用//系統(tǒng)配置向?qū)罴?APPSystem.out.println('*********進入APP服務器控制臺配置向?qū)?********');String[] strCmdApp = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//系統(tǒng)配置向?qū)?server-type', //服務器模式'api-server', //APP服務器'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'web-server-info ' + webIp + ':8080', //設置管理服務器端口'commit',//提交配置'exit'}; for (String str : strCmdApp) { synchronized (bw) {bw.write(str.trim());bw.newLine();System.out.println('step: ' + str);bw.wait(1000); }}//查看進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');System.out.println('app配置向?qū)гO置成功!');bw.close(); } else if (role != null && role.equals('log-analyze-server')) {//系統(tǒng)日志分析服務器//系統(tǒng)配置向?qū)罴?log-analyze-serverSystem.out.println('*********進入日志分析服務器控制臺配置向?qū)?********');String[] strCmdLog = {'python /opt/tools/skylar_cli/bin/skylar_cli.pyc',//天擎配置向?qū)?server-type', //服務器模式'log-analyze-server', //管理服務器:提供管理功能及應用功能'cache-info ' + redisIp + ':6379', //緩存服務器'db-info ' + pgIp + ':5432', //設置數(shù)據(jù)庫信息'queue-info ' + beanstalkdIp + ':11300', //設置隊列信息'web-server-info ' + webIp + ':8080', //設置管理服務器端口'sfmg-db-info ' + pgIp + ':5432', //設置軟件管家數(shù)據(jù)庫'commit',//提交配置'exit' }; //順序執(zhí)行配置向?qū)頵or (String str : strCmdLog) { synchronized (bw) {bw.write(str.trim());bw.newLine();System.out.println('step: ' + str);bw.wait(1000); }}//測試進程是否還存活System.out.println('pro3:' + pro.isAlive());bw.flush();//緩存提交System.out.println('緩存提交!');System.out.println('日志分析服務器配置向?qū)гO置成功!');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);//重啟mainSystem.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();//進程等待 } 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 ,否則會報錯 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包,執(zhí)行jar包:
終于看到久違的部署成功界面~
到此這篇關于java實現(xiàn)Linux(centos) 中docker容器下命令交互(配置向?qū)В┑奈恼戮徒榻B到這了,更多相關docker容器命令交互內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
