1.引入xmlrpc-client
如果是C#语言,请参考《C#对接supervisor XML-RPC API 实现进程控制》
如何安装Supervisor,请参考《Linux进程守护—Supervisor(ubuntu)》
如果是Maven项目,则在pom.xml引入jar包
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
2.封装SupervisorUtils工具包
/**
* Supervisor 方法封装
* @author Mr.Li
* @date 2023-05-30
*/
public class SupervisorUtils {
/**
* 执行supervisor控制命令
* @param url 服务地址
* @param username 用户名
* @param password 密码
* @param method 控制方法
* @param params 参数
*/
public static Object sendSupervisorCommand(String url,String username,String password,String method, Vector<Object> params){
try {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(url));
config.setBasicUserName(username);
config.setBasicPassword(password);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
return client.execute(method, params);
}catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (XmlRpcException e) {
e.printStackTrace();
return null;
}
}
}
2.1.参数详解
2.1.1.url
http://localhost:9001/RPC2
其中localhost为你的IP地址,如果是本地可以使用localhost,或者127.0.0.1.
2.1.2.username/password
这里的用户名密码是你在安装supervisor时,supervisor配置文件中[inet_http_server]下配置的远程控制界面的用户名与密码。
2.1.3.method
method是可以控制supervisor的方法,对应的清单如下(我定义了一个常量用来记录这些方法):
/**
* Supervisor XML-RPC监控方法常量
* @author Mr.Li
* @date 2023-05-30
*/
public class SupervisorConstants {
/**
* 获取监控服务运行状态
* 返回SupervisorStruct实体类
*/
public static final String getState="supervisor.getState";
/**
* 关闭监控服务
* 返回boolean
*/
public static final String shutdown="supervisor.shutdown";
/**
* 重启监控服务
* 返回boolean
*/
public static final String restart="supervisor.restart";
/**
* 获取监控的所有进程信息
* 返回ProcessStruct[]集合
*/
public static final String getAllProcessInfo="supervisor.getAllProcessInfo";
/**
* 启动进程;
* 参数1:group:name(String);
* 参数2:wait:是否等待启动完毕(Boolean)
* 返回boolean
*/
public static final String startProcess="supervisor.startProcess";
/**
* 启动所有进程
* 参数:wait:是否等待启动完毕(Boolean)
* 返回ProcessStateStruct[]集合
*/
public static final String startAllProcesses="supervisor.startAllProcesses";
/**
* 关闭进程
* 参数1:group:name(String);
* 参数2:wait:是否等待状态返回(Boolean)
* 返回boolean
*/
public static final String stopProcess="supervisor.stopProcess";
/**
* 关闭所有进程
* 参数:wait:是否等待状态返回(Boolean)
* 返回ProcessStateStruct[]集合
*/
public static final String stopAllProcesses="supervisor.stopAllProcesses";
/**
* 重新加载配置文件
* 返回一个实体object
*/
public static final String reloadConfig="supervisor.reloadConfig";
/**
* 清理单个进程的日志,这里只是清理监控服务器对此进程记录的日志,并不是程序内部打印的日志
* 参数:group:name(String);
* 返回boolean
*/
public static final String clearProcessLogs="supervisor.clearProcessLogs";
}
2.1.4.params
supervisor每个控制方法对应的参数不同,在方法常量里面有说明每个方法对应的参数项。
示例1:启动进程
/**
* 启动进程
* @param id
* @param group
* @param name
* @return
*/
public boolean startProcess(String id,String group,String name){
SupervisorConfig config=querySupervisorConfig(id);
if(StringUtils.isNotBlank(config.getUrl())&&StringUtils.isNotBlank(config.getUserName())&&StringUtils.isNotBlank(config.getPassword())) {
Vector<Object> params =new Vector<>();
params.addElement(String.format("%s:%s",group,name));
params.addElement(true);
Object result = SupervisorUtils.sendSupervisorCommand(config.getUrl(),config.getUserName(),config.getPassword(), SupervisorConstants.startProcess,params);
return Boolean.valueOf(result.toString());
}else{
return false;
}
}
其中group与name,在“supervisor.getAllProcessInfo”返回的结果集里面是包含了这些信息,示例如下:
[{"stderr_logfile":"","logfile":"/opt/gnss/analysis-server/run.log","start":1686387550,"description":"pid 24636, uptime 2 days, 17:11:15","pid":24636,"statename":"RUNNING","spawnerr":"","stop":1686387549,"now":1686622225,"name":"analysis-server","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/analysis-server/run.log","group":"analysis-server"},{"stderr_logfile":"","logfile":"/opt/gnss/apollo-api/run.log","start":1686617943,"description":"pid 28303, uptime 1:11:22","pid":28303,"statename":"RUNNING","spawnerr":"","stop":1686617942,"now":1686622225,"name":"apollo-api","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/apollo-api/run.log","group":"apollo-api"},{"stderr_logfile":"","logfile":"/opt/gnss/gps-server/run.log","start":1686320681,"description":"pid 7685, uptime 3 days, 11:45:44","pid":7685,"statename":"RUNNING","spawnerr":"","stop":1686320681,"now":1686622225,"name":"gps-server","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/gps-server/run.log","group":"gps-server"},{"stderr_logfile":"","logfile":"/opt/gnss/hc808-server/run.log","start":1686387543,"description":"pid 24569, uptime 2 days, 17:11:22","pid":24569,"statename":"RUNNING","spawnerr":"","stop":1686387542,"now":1686622225,"name":"hc808-server","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/hc808-server/run.log","group":"hc808-server"},{"stderr_logfile":"","logfile":"/opt/gnss/hciot-server/run.log","start":1686387535,"description":"pid 24460, uptime 2 days, 17:11:30","pid":24460,"statename":"RUNNING","spawnerr":"","stop":1686387535,"now":1686622225,"name":"hciot-server","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/hciot-server/run.log","group":"hciot-server"},{"stderr_logfile":"","logfile":"/opt/gnss/jt808-server/run.log","start":1686464923,"description":"pid 17339, uptime 1 day, 19:41:42","pid":17339,"statename":"RUNNING","spawnerr":"","stop":1686464923,"now":1686622225,"name":"jt808-server","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/jt808-server/run.log","group":"jt808-server"},{"stderr_logfile":"","logfile":"/opt/gnss/spartan-tracker/run.log","start":1686387554,"description":"pid 24724, uptime 2 days, 17:11:11","pid":24724,"statename":"RUNNING","spawnerr":"","stop":1686387553,"now":1686622225,"name":"spartan-tracker","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/spartan-tracker/run.log","group":"spartan-tracker"},{"stderr_logfile":"","logfile":"/opt/gnss/system-api/run.log","start":1686496975,"description":"pid 13304, uptime 1 day, 10:47:30","pid":13304,"statename":"RUNNING","spawnerr":"","stop":1686496974,"now":1686622225,"name":"system-api","exitstatus":0,"state":20,"stdout_logfile":"/opt/gnss/system-api/run.log","group":"system-api"}]
3.官方文档
上述每个接口返回有对应的应答参数,详情可以参考官方文档,我这里不做过多解释
XML-RPC API Documentation