影刀RPA 微服务健康检查:多服务状态一键巡检

📅 2026/7/16 9:17:23
影刀RPA 微服务健康检查:多服务状态一键巡检
影刀RPA 微服务健康检查多服务状态一键巡检作者林焱 | 分类影刀RPA新手教程 | 难度★★什么情况用微服务架构下可能有几十上百个服务。运维日常需要检查每个服务是否存活、健康检查是否通过、依赖的服务是否正常。手动一个个curl或者打开监控面板看繁琐且容易漏。用影刀RPA定时对所有微服务执行健康检查HTTP health check 端口检测 依赖检查生成健康报告。异常服务自动告警并尝试重启。怎么做第一步维护服务清单店群矩阵自动化突破运营极限在Excel中维护需要健康检查的服务服务名环境健康检查URL端口依赖服务重启命令user-service生产/actuator/health8080mysql,redisdocker restart user-svcorder-service生产/actuator/health8081mysql,kafka,user-servicedocker restart order-svcgateway生产/health443user-service,order-servicedocker restart gatewaynotification预发布/health8083kafkadocker restart notif-svc第二步执行健康检查# 影刀Python节点HTTP健康检查importrequestsimportsocketfromdatetimeimportdatetimedefcheck_http_health(service_name,health_url,timeout5):HTTP健康检查try:resprequests.get(health_url,timeouttimeout)# Spring Boot Actuator返回的格式ifresp.status_code200:dataresp.json()statusdata.get(status,UNKNOWN)# 检查组件状态如数据库连接componentsdata.get(components,{})down_components[]forcomp_name,comp_dataincomponents.items():ifcomp_data.get(status)DOWN:down_components.append(comp_name)return{service:service_name,http_status:resp.status_code,health_status:status,response_time_ms:round(resp.elapsed.total_seconds()*1000,1),down_components:down_components,healthy:statusUPandnotdown_components,}else:return{service:service_name,http_status:resp.status_code,health_status:DOWN,healthy:False,error:fHTTP{resp.status_code},}exceptrequests.Timeout:return{service:service_name,healthy:False,error:连接超时}exceptrequests.ConnectionError:return{service:service_name,healthy:False,error:无法连接}exceptExceptionase:return{service:service_name,healthy:False,error:str(e)[:100]}defcheck_port(host,port,timeout3):TCP端口检查socksocket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.settimeout(timeout)try:resultsock.connect_ex((host,port))sock.close()returnresult0except:returnFalse第三步依赖链检查# 影刀Python节点依赖链分析defcheck_dependency_chain(services,results): 检查依赖链如果A依赖BB挂了A的DOWN_COMPONENTS里应该显示 by_name{r[service]:rforrinresults}dependency_issues[]forsvcinservices:depssvc.get(依赖服务,).split(,)deps[d.strip()fordindepsifd.strip()]fordepindeps:dep_resultby_name.get(dep)ifdep_resultandnotdep_result.get(healthy,False):dependency_issues.append({服务:svc[服务名],依赖:dep,问题:f{dep}不可用,影响:f{svc[服务名]}可能受影响,})returndependency_issues# 示例dependency_issuescheck_dependency_chain(services,health_results)forissueindependency_issues:print(f{issue[服务]}依赖{issue[依赖]}{issue[问题]}→{issue[影响]})第四步自动恢复尝试# 影刀Python节点自动重启尝试defauto_recovery(failed_service): 对失败的服务尝试自动恢复 注意自动重启有风险只对确认可以安全重启的服务执行 service_namefailed_service[service]restart_cmdfailed_service.get(restart_cmd)ifnotrestart_cmd:return{action:skipped,reason:未配置重启命令}# 只对非核心服务自动重启或者配置了auto_restart标志ifnotfailed_service.get(auto_restart,False):return{action:skipped,reason:未开启自动重启}try:resultsubprocess.run(restart_cmd.split(),capture_outputTrue,![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/95d1764d8f7d42cca1234cbfe46cde61.png#pic_center)textTrue,timeout30,)ifresult.returncode0:# 等待服务启动time.sleep(10)# 再次健康检查recheckcheck_http_health(service_name,failed_service[health_url])ifrecheck.get(healthy):return{action:recovered,msg:f重启成功健康检查通过}else:return{action:failed,msg:重启后仍然不健康}else:return{action:failed,msg:f重启命令失败{result.stderr}}exceptExceptionase:return{action:error,msg:str(e)}第五步生成健康报告defgenerate_health_report(results,timestamp):生成微服务健康日报totallen(results)healthysum(1forrinresultsifr.get(healthy))unhealthytotal-healthy reportf## 微服务健康日报 ({timestamp})\n\nreportf### 概览\nreportf- 服务总数{total}\nreportf- 健康{healthy}✅\nreportf- 异常{unhealthy}❌\nreportf- 健康率{healthy/total*100:.1f}%\n\nifunhealthy0:report### ❌ 异常服务\nforrinresults:ifnotr.get(healthy):reportf- **{r[service]}**:{r.get(error,UNKNOWN)}\nifr.get(down_components):reportf - 异常组件{, .join(r[down_components])}\n# 平均响应时间avg_rtsum(r.get(response_time_ms,0)forrinresultsifr.get(healthy))/max(healthy,1)reportf\n平均响应时间{avg_rt:.0f}ms\nreturnreport影刀【企微推送】给运维群。有什么坑坑1健康检查端点格式不统一temu店群自动化报活动案例Spring Boot是/actuator/healthNode.js项目可能用/healthGo项目可能用/healthz有的返回JSON、有的返回纯文本OK。需要为每个服务配置正确的检查URL和解析方式。坑2网络分区导致误报如果影刀所在的机器和微服务之间有网络问题防火墙规则变更、VPN断开所有健康检查都会失败但实际上微服务正常运行。需要确认影刀可以访问服务的网络或者同时在多个位置部署检查点。坑3自动重启可能引发雪崩如果因为数据库挂了导致10个服务都不健康自动重启它们没有意义——数据库还是坏的。需要先分析根因是服务本身问题还是依赖问题依赖问题不要重启服务。坑4K8s环境与服务编排如果用的是KubernetesK8s自己就有健康检查和自动重启机制livenessProbe/readinessProbe。RPA的健康检查是作为外部监控的补充不要和K8s的内置机制冲突。坑5检查频率与告警风暴如果每1分钟检查一次网络抖动导致一批服务瞬时失败会产生大量告警。建议设置告警阈值连续3次失败才告警单次失败只记录日志。总结微服务健康检查的重点是从全局视角看依赖链——不是只看单个服务是否存活而是要判断服务A挂了会不会影响服务B和C。把依赖关系理清楚才能精准定位真正需要处理的故障。