一文带你了解Spring Actuator( 二 )

  • @DeleteOperation:作用在方法上,可用来删除对应端点信息(通过 Delete 方法请求) 。
  • @Selector:作用在参数上,用来定位一个端点的具体指标路由 。
  • 自定义一个端点服务:
    @Endpoint(id = "custom")public class CustomEndpoint {/*** /actuator/custom*/@ReadOperationpublic Map custom() {return new HashMap();}/*** /actuator/custom/{name}?value=https://www.isolves.com/it/cxkf/kj/2023-11-08/{value}*/@ReadOperationpublic Map name(@Selector String name, @Nullable String value) {return new HashMap();}}Spring-AdminSpring-Actuator主要实现数据的采集,以及提供REST API以及JMX的访问渠道 , 那么数据具体如何友好地显示出来?这时我们需要对应的UI,其中spring-boot-admin就是这样一款工具 。
    http://localhost:8080/applications
    • 服务端
    <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId></dependency> @EnableAdminServerpublic class Application{} 
    • 客户端
     <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.6.2</version></dependency>客户端配置
    spring:boot:admin:client:url: http://localhost:8080Prometheus + Grafana上面说到,Actuator除了采集指标 , 提供访问API外,还提供了“应用度量数据的导出”的功能,这样就能将我们采集到的指标输出到指定的存储服务或终端以便进一步分析 。其中Prometheus就是这样一个应用 。
    • Prometheus 时序数据库,用于存储数据,提供并提供查询,它存储了计算机系统在各个时间点上的监控数据
    • Grafana 仪表盘,提供监控指标可视化界面 。
    • 依赖
    <dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency>
    • 配置
     management:endpoints:web:exposure:include: "*"metrics:export:prometheus:enabled: trueprometheus:enabled: true
    • prometheus配置
    scrape_configs:- job_name: 'spring-boot-actuator'metrics_path: '/actuator/prometheus'static_configs:- targets: ['localhost:8080'] # 使用你的Spring Boot应用程序的实际主机和端口替换
    • 启动
     prometheus.exe --config.file=prometheus.ymlgrafana-server.exe由于篇幅有限 , 关于Grafana如何集成Prometheus,网上有很多具体实践 , 这里不重复赘述...
    问题
    • 服务端点
    由于项目使用spring-boot版本为2.3.7.RELEASE,而spring-boot-admin-starter-server版本设置设置为2.7.x版本时,UI相关配置一直无法加载,通过源码可以看到
    在2.6.x版本中对应spring-boot-admin-server-ui存在META-INspring.factories文件
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration而在2.7.x版本中,spring.factories删除了且改为了 META-INFspringorg.springframework.boot.autoconfigure.AutoConfiguration.imports
    de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration因此如果需要使用2.7.x版本的spring-boot-admin,记得把spring-boot升级到2.7.x
    • 参数名称
    参数名称被解析为arg0 , 导致请求匹配失败 。通过下面的配置保证编译后的文件通过反射获取的参数名称不变
    <plugin><groupId>org.Apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><debug>false</debug><!-- 防止方法参数名解析为arg0...--><compilerArgs><arg>-parameters</arg></compilerArgs></configuration></plugin>如果使用Idea,你可以在应用启动后,Actuator功能面板的Mappings中看到服务地址的变化
    结束语服务监控是为了更好的了解服务运行状况,及时发现服务可能出现的问题 , 并在出现故障时能够有效的定位问题产生的原因 。更大层面解决系统运行过程中的维护 成本 。关于监控相关的应用还有一些,比如SkyWalking、Zipkin、Elastic APM等等 。


    推荐阅读