一、簡介
現在的服務器端程序很多都是基于Java開發,針對于Java開發的Socket程序,這樣的服務器端上線后出現問題需要手動重啟,萬一大半夜的掛了,還是特別麻煩的。
大多數的解決方法是使用其他進程來守護服務器程序,如果服務器程序掛了,通過守護進程來啟動服務器程序。
萬一守護進程掛了呢?使用雙守護來提高穩定性,守護A負責監控服務器程序與守護B,守護B負責監控守護A,任何一方出現問題,都能快速的啟動程序,提高服務器程序的穩定性。
Java的運行環境不同于C等語言開發的程序,Java程序跑在JVM上面。不同于C語言可以直接創建進程,Java創建一個進程等同于使用java -jar xxx.jar啟動一個程序。
Java啟動程序并沒有C#類似的單實例限制,你可以啟動多個,但是你不能啟動多個,不能讓多個守護A去守護服務器程序,萬一啟動了多個服務器程序怎么辦?
二、技術講解
這里的技術講解比較粗略,具體請百度一下,這里只講解作用。
1、jps命令。
JDK自帶的命令工具,使用jps -l可以列出正在運行的Java程序,顯示Java程序的pid與Name。只對Java程序有效,其實查看的是運行的JVM
2、java.nio.channels.FileLock類的使用
這個是Java new IO中的類,使用他可以維持在讀取文件的給文件加上鎖,判斷文件時候有鎖可以判斷該文件是否被其他的程序使用
3、ProcessBuilder與Process
這兩個原理差不多,都是調用系統的命令運行,然后返回信息。但是硬編碼會導致你的Java程序失去可移植性,可以將命令獨立到配置文件中。
三、設計原理
Server:服務器程序
A:守護進程A
B:守護進程B
A.lock:守護進程A的文件鎖
B.lock:守護進程B的文件鎖
----------------------------------------------------------------------------------
Step 1:首先不考慮Server,只考慮A與B之間的守護
1.A判斷B是否存活,沒有就啟動B
2.B判斷A是否存活,沒有就啟動A
3.在運行過程中A與B互相去拿對方的文件鎖,如果拿到了,證明對面掛了,則啟動對方。
4.A啟動的時候,獲取A.lock文件的鎖,如果拿到了證明沒有A啟動,則A運行;如果沒有拿到鎖,證明A已經啟動了,或者是B判斷的時候拿到了鎖,如果是A已經啟動了,不需要再次啟動A,如果是B判斷的時候拿到了鎖,沒關緊 要,反正B會再次啟動A。
5.B啟動的時候原理與A一致。
6.運行中如果A掛了,B判斷到A已經掛了,則啟動A。B同理。
Step 2:加入Server
1.A用于守護B和Server,B用于守護A。
2.原理與Step 1 一致,只是A多個一個守護Serer的任務。
3.當A運行的時候,使用進程pid檢測到Server已經掛了,就啟動Server
4.如果Server與A都掛了,B會啟動A,然后A啟動Server
5.如果Server與B掛了,A啟動Server與B
6.如果A與B都掛了,守護結束
Step 3:使用Shutdown結束守護,不然結束Server后會自動啟動
四、實現
1、GuardA的實現
public class GuardA {
// GuardA用于維持自己的鎖
private File fileGuardA;
private FileOutputStream fileOutputStreamGuardA;
private FileChannel fileChannelGuardA;
private FileLock fileLockGuardA;
// GuardB用于檢測B的鎖
private File fileGuardB;
private FileOutputStream fileOutputStreamGuardB;
private FileChannel fileChannelGuardB;
private FileLock fileLockGuardB;
public GuardA() throws Exception {
fileGuardA = new File(Configure.GUARD_A_LOCK);
if (!fileGuardA.exists()) {
fileGuardA.createNewFile();
}
//獲取文件鎖,拿不到證明GuardA已啟動則退出
fileOutputStreamGuardA = new FileOutputStream(fileGuardA);
fileChannelGuardA = fileOutputStreamGuardA.getChannel();
fileLockGuardA = fileChannelGuardA.tryLock();
if (fileLockGuardA == null) {
System.exit(0);
}
fileGuardB = new File(Configure.GUARD_B_LOCK);
if (!fileGuardB.exists()) {
fileGuardB.createNewFile();
}
fileOutputStreamGuardB = new FileOutputStream(fileGuardB);
fileChannelGuardB = fileOutputStreamGuardB.getChannel();
}
/**
* 檢測B是否存在
*
* @return true B已經存在
*/
public boolean checkGuardB() {
try {
fileLockGuardB = fileChannelGuardB.tryLock();
if (fileLockGuardB == null) {
return true;
} else {
fileLockGuardB.release();
return false;
}
} catch (IOException e) {
System.exit(0);
// never touch
return true;
}
}
}
2、GuardServer的實現
public class GuardServer {
private String servername;
public GuardServer(String servername) {
this.servername = servername;
}
public void startServer(String cmd) throws Exception {
System.out.println("Start Server : " + cmd);
//將命令分開
// String[] cmds = cmd.split(" ");
// ProcessBuilder builder = new ProcessBuilder(cmds);
//
ProcessBuilder builder=new ProcessBuilder(new String[]{"/bin/sh","-c",cmd});
//將服務器程序的輸出定位到/dev/tty
builder.redirectOutput(new File("/dev/tty"));
builder.redirectError(new File("/dev/tty"));
builder.start(); // throws IOException
Thread.sleep(10000);
}
/**
* 檢測服務是否存在
*
* @return 返回配置的java程序的pid
* @return pid >0 返回的是 pid <=0 代表指定java程序未運行
* **/
public int checkServer() throws Exception {
int pid = -1;
Process process = null;
BufferedReader reader = null;
process = Runtime.getRuntime().exec("jps -l");
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
String[] strings = line.split("\\s{1,}");
if (strings.length < 2)
continue;
if (strings[1].contains(servername)) {
pid = Integer.parseInt(strings[0]);
break;
}
}
reader.close();
process.destroy();
return pid;
}
}
3、GuardAMain實現
public class GuardAMain {
public static void main(String[] args) throws Exception {
GuardA guardA = new GuardA();
Configure configure = new Configure();
GuardServer server = new GuardServer(configure.getServername());
while (true) {
// 如果GuardB未運行 運行GuardB
if (!guardA.checkGuardB()) {
System.out.println("Start GuardB.....");
Runtime.getRuntime().exec(configure.getStartguardb());
}
// 檢測服務器存活
if (server.checkServer() <= 0) {
boolean isServerDown = true;
// trip check
for (int i = 0; i < 3; i++) {
// 如果服務是存活著
if (server.checkServer() > 0) {
isServerDown = false;
break;
}
}
if (isServerDown)
server.startServer(configure.getStartserver());
}
Thread.sleep(configure.getInterval());
}
}
}
4、Shutdown實現
public class ShutDown {
public static void main(String[] args) throws Exception {
Configure configure = new Configure();
System.out.println("Shutdown Guards..");
for (int i = 0; i < 3; i++) {
Process p = Runtime.getRuntime().exec("jps -l");
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.toLowerCase().contains("Guard".toLowerCase())) {
String[] strings = line.split("\\s{1,}");
int pid = Integer.parseInt(strings[0]);
Runtime.getRuntime().exec(configure.getKillcmd() + " " + pid);
}
}
p.waitFor();
reader.close();
p.destroy();
Thread.sleep(2000);
}
System.out.println("Guards is shutdown");
}
}
5、GuardB與GuardA類似
五、下載與使用
項目文件夾:guard_demo
下載地址:http://pan.baidu.com/s/1bn1Y6BX
如果有什么疑問或者建議,請聯系我