ftp讀寫遠端yml檔案

2021-10-09 12:52:15 字數 3652 閱讀 6048

遠端修改prometheus配置的規則檔案

public class prometheusutils 

ftp.storefile(filename, createfileitem(filenametemp).getinputstream());

} catch (ioexception e) {} finally catch (ioexception e) {}

}} }

/*** 通過ftp從遠端獲取規則map

* @throws socketexception

* @throws ioexception

*/public static mapftpread(string filename)

outputstream os = new fileoutputstream(new file(filenametemp));

ftp.retrievefile(ftpfile.getname(), os);

try (inputstream input = new fileinputstream(filenametemp)) catch (exception e) {}

os.close();

}} catch (ioexception e) {} finally catch (ioexception e) {}

}} return resltmap;

} /**

* 組名和規則轉化成規則map

*/public static mapgetrulesmap(string groupname, listrulelist)

map.put(rules, objs);

list.add(map);

result.put(group, list);

return result;

} /**

* 通過規則map獲取組名

* @param prometheusmap

* @return

*/public static string getgroupname(mapprometheusrulesmap)

/*** 通過規則map獲取規則

* @param prometheusmap

* @return

*/public static listgetrules(mapprometheusrulesmap) else

try catch (illegalacces***ception | invocationtargetexception e)

reslist.add(prometheusrulevo);

} return reslist;

} /**

* @return

*/public static boolean reloadrules()

} catch (exception e) {}

return false;

} private static multipartfile createfileitem(string filepath)

} catch (exception e) {}

return new commonsmultipartfile(item);

} private static string gettempfilename(string filename)

private static ftpclient setftpclient(ftpclient ftp) throws ioexception

private static string getreloaduri(string ip, string port)

}

vo實體類

public class prometheusrulevo 

public void setalert(string alert)

public string getexpr()

public void setexpr(string expr)

public string getforstr()

public void setforstr(string forstr)

public labelsvo getlabelsvo()

public void setlabelsvo(labelsvo labelsvo)

public annotationsvo getannotationsvo()

public void setannotationsvo(annotationsvo annotationsvo)

}public class annotationsvo

public void setsummary(string summary)

public string getdescrption()

public void setdescrption(string descrption)

}public class labelsvo

public void setseverity(string severity)

}

server:

port: 8080

promethues:

ftpaddr: 192.168.1.101

ftpport: 8888

username: admin

password: 123456

rulesdir: /opt/***x/***x/

常量類

@component

public class prometheuscontext implements initializingbean ")

private string ftpaddr;

@value("$")

private string username;

@value("$")

private string password;

@value("$")

private string rulesdir;

@value("$")

private string ftpport;

public static string ftp_addr;

public static string username;

public static string password;

public static string rules_dir;

public static string ftp_port;

@override

public void afterpropertiesset() throws exception

}

測試類

執行測試類,可以在遠端ftp指定目錄下寫入yml檔案,yml格式由實體類的結構決定

@runwith(springrunner.class)

@springboottest(webenvironment = springboottest.webenvironment.random_port)

public class tests extends basetest

}

FTP 操作遠端檔案

string ftpuserid 使用者名稱 string ftppassword 密碼 ftpwebrequest reqftp region 連線伺服器 連線伺服器 private void connect string path endregion region 獲得檔案列表 獲得檔案列表 p...

FTP遠端傳輸檔案操作

1.與遠端建立連線 ftp ftp open serverip 使用username password登入後即可進行ftp操作。2.目錄操作 ls cd mkdir delete 刪除乙個檔案 mdelete 刪除一批檔案 檔名稱羅列 lcd 跳轉本機目錄 3.檔案傳輸 1 從遠端到本地 ftp g...

yml檔案教程

原來三個橫線 是用來區分多個檔案的,像下面就是指定了兩個配置。spring name microservice discovery eureka ha 指定profile peer1 spring profiles peer1 server port 8761 eureka instance 當pr...