springboot啟動bat/bash腳本實現(xiàn)過程
更新時間:2025年08月28日 16:45:17 作者:誠誠程程成
請?zhí)峁┚唧w錯誤信息或腳本內(nèi)容,以便分析Spring Boot項目中Controller、Entity、Service層的依賴問題,跨平臺腳本需注意路徑兼容性和環(huán)境變量配置,確保依賴項正確引入
windows有兩個腳本,linux有一個腳本,需要用springboot啟動著兩個腳本
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é)點是否完整
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);
}
}
/**
* 遠程登錄linux主機
* @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();
// // 認證
// flg = putEntity.conn.authenticateWithPassword(userName, userPwd);
// } catch (IOException e) {
// throw new Exception("遠程連接服務(wù)器失敗", e);
// }
// return flg;
// }
/**
* 遠程執(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值會丟失
//log方法:驗證是否能夠遠程登錄linux主機
boolean flg = false;
try {
putEntity.conn = new Connection(ip); //創(chuàng)建conn
// 連接
putEntity.conn.connect();
// 認證
flg = putEntity.conn.authenticateWithPassword(userName, userPwd); //用戶密碼進行驗證
} catch (IOException e) {
throw new Exception("遠程連接服務(wù)器失敗", e);
}
//驗證成功 flg=true
try {
if (flg) {
// 打開一個會話
session = putEntity.conn.openSession(); //根據(jù)conn得到session值 ***之前l(fā)ogin方法寫道execute方法外部 報空指針錯誤 ****原因:程序進入execute時conn已經(jīng)丟失了 所以session為空 執(zhí)行失敗
// 執(zhí)行命令
session.execCommand(cmd); //執(zhí)行cmd命令
result = processStdout(session.getStdout(), putEntity.DEFAULTCHART); //命令執(zhí)行完后返回的結(jié)果值
// 如果result為空,說明腳本執(zhí)行出錯了
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 輸入流對象
* @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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何優(yōu)雅地使用Swagger2
這篇文章主要介紹了SpringBoot如何優(yōu)雅地使用Swagger2,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
Springboot項目中定時任務(wù)的四種實現(xiàn)方式詳解
Spring的@Scheduled注解是一種非常簡單和便捷的實現(xiàn)定時任務(wù)的方式,通過在方法上添加@Scheduled注解,我們可以指定方法在特定的時間間隔或固定的時間點執(zhí)行,本文給大家介紹Springboot項目中定時任務(wù)的四種實現(xiàn)方式,感興趣的的朋友一起看看b2024-02-02

