Java Supervisor RPC2 接口对接

news/2024/11/15 14:26:55/

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


http://www.ppmy.cn/news/360726.html

相关文章

【持续更新】ubuntu中的git命令

【持续更新】ubuntu中的git命令 git init 初始化本地目录作为本地仓库 git remote add origin xxxxxxx xxxxxxxx是指git地址。本条命令是将本地仓库和远程仓库连接起来。 git pull origin master 意思是将远程origin主机的master分支拉取过来和本地的当前分支进行合并。加上…

c++20的新增的小的特性

c20的新增的小的特性 c20新增的特性非常多&#xff0c;其中concept&#xff0c;coroutine&#xff0c;module和range为四大特性&#xff0c;在之前的章节中已有涉及&#xff0c;本文则对其他的一些小改动进行讲解。 c20的新增的小的特性 std::formatCalendartimezonestd::sou…

python代码热更原理及实现

一、简介&#xff1a; 热更&#xff0c;指程序具有在运行过程中修改代码&#xff0c;不重启&#xff0c;而直接生效的能力。 热更对代码的微调&#xff0c;debug非常友好&#xff0c;不再需要重新启动程序&#xff0c;能直接在已有的上下文环境中直接测试。 二、接入流程&am…

宁愿给新员工开高薪,也不愿意给老员工涨薪,那些大厂老板都是怎么想的?

事情是这样的&#xff0c;作为公司2年工龄的测试老员工&#xff0c;技术过关&#xff0c;独当一面&#xff0c;领导对我也很放心。 原本只想涨个30%的薪水&#xff0c;就留下来勤勤恳恳和公司共进退&#xff0c;但HR死活不肯&#xff0c;各种规章制度卡得死死的&#xff0c;顶…

老板该关心什么?

今天到我们的合作伙伴处看看&#xff0c;听说他们公司实施的ERP已上线&#xff0c;也看看使用效果&#xff0c;随即问一问员工&#xff0c;又问一问老板&#xff0c;沟通一会&#xff0c;突然有个想法&#xff0c;象他们这样高速成长的公司岂止他们一家。从树木看森林&#xff…

中国新厨房,老板电器的未来有多大?

中国企业&#xff0c;一度很躁动。在“不要把鸡蛋放在同一个篮子里”这句话的驱使下&#xff0c;做地产的布局汽车、快消&#xff0c;做航空的进军地产、健康&#xff0c;做视频的踏足硬件、消费。而品类扩张成为家电行业的通用技法。 上世纪80年代&#xff0c;老板电器开始进…

一个小公司老板的日常管理

1. 小公司如何留住骨干&#xff1a; 2.关于授权 3.有的钱不能省 5.关于招聘 6.老板尽量唱红脸 7.公司里的亲戚 8.当老板和开车 9.按时发工资 10.学会说“不” 11.不要在公司内部奢望交朋友 12.避免当场做决定 13.政策的制定 14.矬子里拔将军 15.有些事情越透明越好 16.发劳保用…

卓老板之今日金句1

从未有过一个真正有用并且深邃的理论是真的依靠纯粹思辨而发现的。 ⏤⏤ 阿尔伯特爱因斯坦 文章&#xff1a; 《极限&#xff1a;为什么电价夜间减半&#xff1f;》&#xff0c;请在微信客户端打开链接。