springboot啟動(dòng)bat/bash腳本實(shí)現(xiàn)過程
更新時(shí)間:2025年08月28日 16:45:17 作者:誠(chéng)誠(chéng)程程成
請(qǐng)?zhí)峁┚唧w錯(cuò)誤信息或腳本內(nèi)容,以便分析Spring Boot項(xiàng)目中Controller、Entity、Service層的依賴問題,跨平臺(tái)腳本需注意路徑兼容性和環(huán)境變量配置,確保依賴項(xiàng)正確引入
windows有兩個(gè)腳本,linux有一個(gè)腳本,需要用springboot啟動(dòng)著兩個(gè)腳本
contoller層
import com.dwc.putdatasystem.service.impl.PutServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.File;
@RestController
@RequestMapping("/putbat")
public class PutController {
@Autowired
private PutServiceImpl putBatService;
@GetMapping ("bat")
public void put() throws Exception {
//第一部分 執(zhí)行bat腳本
String batPath = "D:/******/package.bat"; // bat腳本路徑
String batPath2 ="D:/*****/put.bat";
File batFile = new File(batPath);
boolean batFileExist = batFile.exists();
System.out.println("??????????????????????????判斷bat腳本文件是否存在(true/false)??????????????????????????");
System.out.println("batFileExist:" + batFileExist);
if (batFileExist) {
System.out.println("??????????????????????????正在執(zhí)行編譯ing??????????????????????????");
putBatService.callCmd(batPath);
System.out.println("??????????????????????????編譯完成??????????????????????????");
System.out.println("??????????????????????????正在上傳至linux系統(tǒng)ing??????????????????????????");
putBatService.callCmd(batPath2);
}
System.out.println("??????????????????????????jar包上傳服務(wù)器完成??????????????????????????");
//第二部分 執(zhí)行bash腳本
String linuxIP = "ip地址";
String usrName = "用戶名";
String passwd = "密碼";
String DEFAULTCHART = "UTF-8";
//PutEntity rec = new PutEntity(linuxIP, usrName, passwd,);
//PutBatServiceImpl putBatService = new PutBatServiceImpl(linuxIP, usrName, passwd);
System.out.println("??????????????????????????正在上傳job至集群ing??????????????????????????");
// 執(zhí)行腳本
System.out.println(putBatService.execute("/home/dwc/bin/flinkrunjar1.sh start", linuxIP, usrName, passwd, DEFAULTCHART));
// 執(zhí)行jps命令,查看節(jié)點(diǎn)是否完整
System.out.println(putBatService.execute("/home/dwc/bin/jpsall start", linuxIP, usrName, passwd, DEFAULTCHART));
System.out.println("??????????????????????????job上傳集群完成??????????????????????????");
}
}
entity層
import ch.ethz.ssh2.Connection;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Value;
@Data
public class PutEntity {
public String DEFAULTCHART = "UTF-8";
public Connection conn;
public String ip;
public String userName;
public String userPwd;
public PutEntity(String ip, String userName, String userPwd, String DEFAULTCHART) {
this.ip = ip;
this.userName = userName;
this.userPwd = userPwd;
this.DEFAULTCHART = DEFAULTCHART;
}
public PutEntity() {
}
}
service層
import java.io.InputStream;
public interface PutService {
//win腳本用
void callCmd(String locationCmd);
//linux腳本用
//Boolean login(String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception;
String execute(String cmd, String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception;
String processStdout(InputStream in, String charset) throws Exception;
}
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.dwc.putdatasystem.entity.PutEntity;
import com.dwc.putdatasystem.service.PutService;
import ch.ethz.ssh2.Connection;
import org.apache.flink.calcite.shaded.org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.io.*;
@Service("PutBatService")
public class PutServiceImpl implements PutService {
@Override
public void callCmd(String locationCmd){
StringBuilder sb = new StringBuilder();
try {
Process child = Runtime.getRuntime().exec(locationCmd);
InputStream in = child.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(in));
String line;
while((line=bufferedReader.readLine())!=null)
{
sb.append(line + "\n");
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("sb:" + sb.toString());
System.out.println("callCmd execute finished");
} catch (IOException e) {
System.out.println(e);
}
}
/**
* 遠(yuǎn)程登錄linux主機(jī)
* @return 登錄成功返回true,否則返回false
*/
// @Override
// public Boolean login(String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception {
// PutEntity putEntity = new PutEntity(ip, userName, userPwd, DEFAULTCHART);
// boolean flg = false;
// try {
// putEntity.conn = new Connection(ip);
// // 連接
// putEntity.conn.connect();
// // 認(rèn)證
// flg = putEntity.conn.authenticateWithPassword(userName, userPwd);
// } catch (IOException e) {
// throw new Exception("遠(yuǎn)程連接服務(wù)器失敗", e);
// }
// return flg;
// }
/**
* 遠(yuǎn)程執(zhí)行shll腳本或者命令
* @param cmd 即將執(zhí)行的命令
* @return 命令執(zhí)行完后返回的結(jié)果值
*/
@Override
public String execute(String cmd, String ip, String userName, String userPwd, String DEFAULTCHART) throws Exception {
//根據(jù)傳來的ip、userName、userPwd、DEFAULTCHART創(chuàng)建putEntity
PutEntity putEntity = new PutEntity(ip,userName,userPwd,DEFAULTCHART);
String result = ""; //result得到命令執(zhí)行完后返回的結(jié)果值
Session session = null;
//login方法寫道execute方法中 否則conn值會(huì)丟失
//log方法:驗(yàn)證是否能夠遠(yuǎn)程登錄linux主機(jī)
boolean flg = false;
try {
putEntity.conn = new Connection(ip); //創(chuàng)建conn
// 連接
putEntity.conn.connect();
// 認(rèn)證
flg = putEntity.conn.authenticateWithPassword(userName, userPwd); //用戶密碼進(jìn)行驗(yàn)證
} catch (IOException e) {
throw new Exception("遠(yuǎn)程連接服務(wù)器失敗", e);
}
//驗(yàn)證成功 flg=true
try {
if (flg) {
// 打開一個(gè)會(huì)話
session = putEntity.conn.openSession(); //根據(jù)conn得到session值 ***之前l(fā)ogin方法寫道execute方法外部 報(bào)空指針錯(cuò)誤 ****原因:程序進(jìn)入execute時(shí)conn已經(jīng)丟失了 所以session為空 執(zhí)行失敗
// 執(zhí)行命令
session.execCommand(cmd); //執(zhí)行cmd命令
result = processStdout(session.getStdout(), putEntity.DEFAULTCHART); //命令執(zhí)行完后返回的結(jié)果值
// 如果result為空,說明腳本執(zhí)行出錯(cuò)了
if (StringUtils.isBlank(result)) {
result = processStdout(session.getStderr(), putEntity.DEFAULTCHART);
}
putEntity.conn.close();
session.close();
}
} catch (IOException e) {
throw new Exception("命令執(zhí)行失敗", e);
} finally {
if (putEntity.conn != null) {
putEntity.conn.close();
}
if (session != null) {
session.close();
}
}
return result;
}
/**
* 解析腳本執(zhí)行返回的結(jié)果集
* @param in 輸入流對(duì)象
* @param charset 編碼
* @return 以純文本的格式返回
*/
@Override
public String processStdout(InputStream in, String charset) throws Exception {
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(stdout, charset);
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
}
} catch (UnsupportedEncodingException e) {
throw new Exception("不支持的編碼字符集異常", e);
} catch (IOException e) {
throw new Exception("讀取指紋失敗", e);
} finally {
IOUtils.close(br);
IOUtils.close(isr);
IOUtils.close(stdout);
}
return buffer.toString();
}
}
依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-scala_2.11</artifactId>
<version>1.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-blink_2.12</artifactId>
<version>1.13.3</version>
</dependency>
</dependencies>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot+mybatis-plus實(shí)現(xiàn)多數(shù)據(jù)源配置的詳細(xì)步驟
- SpringBoot基于MyBatisPlus實(shí)現(xiàn)公共字段自動(dòng)填充
- SpringBoot3匹配Mybatis3的錯(cuò)誤與解決方案
- SpringBoot+MyBatis Plus實(shí)現(xiàn)update_time字段自動(dòng)更新詳解
- SpringBoot集成MyBatis實(shí)現(xiàn)SQL攔截器的實(shí)戰(zhàn)指南
- SpringBoot集成MyBatis中SQL攔截器的實(shí)戰(zhàn)指南
相關(guān)文章
java調(diào)用shell腳本及注意事項(xiàng)說明
這篇文章主要介紹了java調(diào)用shell腳本及注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
java Swing基礎(chǔ)教程之圖形化實(shí)例代碼
這篇文章主要介紹了java Swing基礎(chǔ)教程之圖形化實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringBoot如何優(yōu)雅地使用Swagger2
這篇文章主要介紹了SpringBoot如何優(yōu)雅地使用Swagger2,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
java根據(jù)負(fù)載自動(dòng)抓取jstack?dump詳情
這篇文章主要介紹了java根據(jù)負(fù)載自動(dòng)抓取jstack?dump詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Springboot項(xiàng)目中定時(shí)任務(wù)的四種實(shí)現(xiàn)方式詳解
Spring的@Scheduled注解是一種非常簡(jiǎn)單和便捷的實(shí)現(xiàn)定時(shí)任務(wù)的方式,通過在方法上添加@Scheduled注解,我們可以指定方法在特定的時(shí)間間隔或固定的時(shí)間點(diǎn)執(zhí)行,本文給大家介紹Springboot項(xiàng)目中定時(shí)任務(wù)的四種實(shí)現(xiàn)方式,感興趣的的朋友一起看看b2024-02-02

