@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,网上有很多具体实践 , 这里不重复赘述...问题
- 服务端点
在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.importsde.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration因此如果需要使用2.7.x版本的spring-boot-admin,记得把spring-boot升级到2.7.x- 参数名称
<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等等 。
推荐阅读
- 一文搞定双链表,让你彻底弄懂线性表的链式实现
- 一文读懂Android架构演进历程
- Instagram 早期技术架构,你了解了吗?
- 太阳具有5个特点,除此之外《太阳》一文还介绍孑太阳和的特点
- 发现男友车上有口红?先不要急着提刀,了解真相再做决定!
- 眉曼颜:眉毛可以分为几种类型,了解后对你肯定很有用。
- “掉秤”快的主食排行榜,红薯倒数第一,窝头排第4,不妨了解下
- 一文搞明白Hive与数据库区别
- 24张生活照了解白珊珊,肤白貌美大长腿,身家过亿生活奢侈
- 有人说抗老的尽头是修护,喜欢修护型抗老面霜,来了解这5款面霜
