星尘代理StarAgent属于星尘服务平台的一部分,它自身注册为系统服务,支持Windows服务和Linux的systemd。StarAgent支持配置守护应用,在它启动时拉起目标应用,运行过程中定时检测目标应用进程,如果目标进程异常退出,它会再次拉起应用进程,保障业务的持续性。守护应用的功能,即使在没有配置服务端的情况下,也可以单机工作。
因此,StarAgent能够把普通应用(非桌面应用)“变成”后台服务,而不需要应用本身做任何修改!
星尘的安装部署可以参考 《星尘代理安装与设置》和《星尘代理企业级部署》。
StarAgent把需要守护的应用信息配置到文件 Config/StarAgent.config 中。
先看例子:
<?xml version="1.0" encoding="utf-8"?> <StarAgent> <!--调试开关。默认true--> <Debug>true</Debug> <!--证书--> <Code></Code> <!--密钥--> <Secret></Secret> <!--本地端口。默认5500--> <LocalPort>5500</LocalPort> <!--更新通道。默认Release--> <Channel>Release</Channel> <!--延迟时间。重启进程或服务的延迟时间,默认3000ms--> <Delay>3000</Delay> <!--应用服务集合--> <Services> <ServiceInfo Name="cube" FileName="dotnet" Arguments="cube.dll urls=http://*/" WorkingDirectory="../cube/" AutoStart="true" AutoStop="false" ReloadOnChange="true" /> <ServiceInfo Name="iotserver" FileName="dotnet" Arguments="IoTServer.dll" WorkingDirectory="../iot/server/" AutoStart="true" AutoStop="false" ReloadOnChange="true" /> </Services> </StarAgent>
需要守护的应用配置在Services段,每个应用就是一个ServiceInfo。
/// <summary>应用服务信息</summary> public class ServiceInfo { #region 属性 /// <summary>名称。全局唯一,默认应用名,根据场景可以加dev等后缀</summary> [XmlAttribute] public String Name { get; set; } /// <summary>文件</summary> [XmlAttribute] public String FileName { get; set; } /// <summary>参数</summary> [XmlAttribute] public String Arguments { get; set; } /// <summary>工作目录</summary> [XmlAttribute] public String WorkingDirectory { get; set; } /// <summary>是否自动启动</summary> [XmlAttribute] public Boolean AutoStart { get; set; } /// <summary>是否自动停止。随着宿主的退出,同时停止该应用进程</summary> [XmlAttribute] public Boolean AutoStop { get; set; } /// <summary>检测文件变动。当文件发生改变时,自动重启应用</summary> [XmlAttribute] public Boolean ReloadOnChange { get; set; } #endregion }
各个配置如下:
- Name。应用名称,本地唯一
- FileName。文件名,可以使用全路径,也可以只要文件名,此时去WorkingDirectory目录。NETCore应用这里填写 dotnet
- Arguments。进程参数,NETCore应用这里写 cube.dll urls=http://*/,就是平时dotnet命令后面那一部分。
- WorkingDirectory。工作目录,一般用于查找FileName。
- AutoStart。在StarAgent启动时,是否需要拉起该应用,一般为true。
- AutoStop。在StarAgent正常退出(停止服务)时,是否停止该应用,一般为false。即使AutoStop为true,如果StarAgent异常退出,该应用也不会退出,因为StarAgent没有机会杀进程。
- ReloadOnChange。在目标目录改变时杀掉应用进程并重新拉起,一般为true,监控目标目录的DLL文件修改时间。该功能特别适用于Linux上跑NETCore应用。
最后再来看一个Linux工控机上的配置,供参考:
<?xml version="1.0" encoding="utf-8"?> <StarAgent> <!--调试开关。默认true--> <Debug>true</Debug> <!--证书--> <Code></Code> <!--密钥--> <Secret></Secret> <!--本地端口。默认5500--> <LocalPort>5500</LocalPort> <!--更新通道。默认Release--> <Channel>Release</Channel> <!--延迟时间。重启进程或服务的延迟时间,默认3000ms--> <Delay>3000</Delay> <!--应用服务集合--> <Services> <ServiceInfo Name="flower" FileName="GirlFlower" Arguments="" WorkingDirectory="/home/feifan/flower/" AutoStart="false" AutoStop="false" ReloadOnChange="false" /> <ServiceInfo Name="iotclient" FileName="dotnet" Arguments="IoTClient.dll" WorkingDirectory="/home/feifan/iotclient/" AutoStart="true" AutoStop="false" ReloadOnChange="false" /> </Services> </StarAgent>
这里的flower,它有一个Linux可执行文件 GirelFlower,因此没有参数。