Springboot应用执行器Actuator源码分析

文章目录

  • 一、认识Actuator
    • 1、回顾Actuator
    • 2、Actuator重要端点
  • 二、源码分析
    • 1、Endpoint自动装配
      • (1)自动配置入口
      • (2)普通Endpoint自动装配
      • (3)配置Web - Endpoint
      • (4)注册Endpoint为Mvc映射
    • 2、BeansEndpoint自动装配原理
      • (1)自动装配类
      • (2)BeansEndpoint
      • (3)执行结果
    • 3、MappingsEndpoint自动装配原理
      • (1)自动装配类
      • (2)MappingsEndpoint
      • (3)执行结果
    • 4、ShutdownEndpoint自动装配原理
      • (1)自动装配类
      • (2)ShutdownEndpoint

一、认识Actuator

1、回顾Actuator

Actuator是Springboot提供运行时数据交互的规范。
它覆盖应用内心戏、环境配置、度量指标、敏感操作。
交互方式为Http Web或者JMX。

Spring Boot Actuator ——健康检查神器

2、Actuator重要端点

在这里插入图片描述

二、源码分析

1、Endpoint自动装配

(1)自动配置入口

在这里插入图片描述

(2)普通Endpoint自动装配

@AutoConfiguration
@ConditionalOnAvailableEndpoint(endpoint = EnvironmentEndpoint.class) // EnvironmentEndpoint可用
@EnableConfigurationProperties(EnvironmentEndpointProperties.class)// 构建属性配置文件
public class EnvironmentEndpointAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic EnvironmentEndpoint environmentEndpoint(Environment environment, EnvironmentEndpointProperties properties,ObjectProvider<SanitizingFunction> sanitizingFunctions) {EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment,sanitizingFunctions.orderedStream().collect(Collectors.toList()));String[] keysToSanitize = properties.getKeysToSanitize();if (keysToSanitize != null) {endpoint.setKeysToSanitize(keysToSanitize);}String[] additionalKeysToSanitize = properties.getAdditionalKeysToSanitize();if (additionalKeysToSanitize != null) {endpoint.keysToSanitize(additionalKeysToSanitize);}return endpoint;}@Bean@ConditionalOnMissingBean@ConditionalOnBean(EnvironmentEndpoint.class)@ConditionalOnAvailableEndpoint(exposure = { EndpointExposure.WEB, EndpointExposure.CLOUD_FOUNDRY })public EnvironmentEndpointWebExtension environmentEndpointWebExtension(EnvironmentEndpoint environmentEndpoint) {return new EnvironmentEndpointWebExtension(environmentEndpoint);}}

(3)配置Web - Endpoint

通过自动配置类WebEndpointAutoConfiguration进行自动装配。


// org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration#webEndpointDiscoverer
@Bean
@ConditionalOnMissingBean(WebEndpointsSupplier.class)
public WebEndpointDiscoverer webEndpointDiscoverer(ParameterValueMapper parameterValueMapper,EndpointMediaTypes endpointMediaTypes, ObjectProvider<PathMapper> endpointPathMappers,ObjectProvider<OperationInvokerAdvisor> invokerAdvisors,ObjectProvider<EndpointFilter<ExposableWebEndpoint>> filters) {// 把所有的Web - Endpoint都搜集到return new WebEndpointDiscoverer(this.applicationContext, parameterValueMapper, endpointMediaTypes,endpointPathMappers.orderedStream().collect(Collectors.toList()),invokerAdvisors.orderedStream().collect(Collectors.toList()),filters.orderedStream().collect(Collectors.toList()));
}

(4)注册Endpoint为Mvc映射

通过自动配置类WebMvcEndpointManagementContextConfiguration注册Endpoint为Mvc映射。

通过各种方式定义的Endpoint,创建为WebMvcEndpointHandlerMapping,进行Web映射。

// org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration#webEndpointServletHandlerMapping
@Bean
@ConditionalOnMissingBean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties, Environment environment) {List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping = new EndpointMapping(basePath);boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, WebMvcAutoConfiguration.pathPatternParser);
}

2、BeansEndpoint自动装配原理

(1)自动装配类

@AutoConfiguration
@ConditionalOnAvailableEndpoint(endpoint = BeansEndpoint.class)
public class BeansEndpointAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic BeansEndpoint beansEndpoint(ConfigurableApplicationContext applicationContext) {// 创建一个BeansEndpointreturn new BeansEndpoint(applicationContext);}}

(2)BeansEndpoint

// id就是可以访问的web路径
@Endpoint(id = "beans")
public class BeansEndpoint {
// 读操作
@ReadOperation
public ApplicationBeans beans() {Map<String, ContextBeans> contexts = new HashMap<>();// 将所有容器都拿出来ConfigurableApplicationContext context = this.context;while (context != null) {// 将Bean都拿出来,封装成一个ContextBeanscontexts.put(context.getId(), ContextBeans.describing(context));context = getConfigurableParent(context);}// 创建一个ApplicationBeans// 返回的信息就会json格式化到响应return new ApplicationBeans(contexts);
}
// 返回的对象
public static final class ApplicationBeans {private final Map<String, ContextBeans> contexts;private ApplicationBeans(Map<String, ContextBeans> contexts) {this.contexts = contexts;}public Map<String, ContextBeans> getContexts() {return this.contexts;}}

(3)执行结果

在这里插入图片描述

3、MappingsEndpoint自动装配原理

(1)自动装配类

@AutoConfiguration
@ConditionalOnAvailableEndpoint(endpoint = MappingsEndpoint.class)
public class MappingsEndpointAutoConfiguration {// 配置Mapping@Beanpublic MappingsEndpoint mappingsEndpoint(ApplicationContext applicationContext,ObjectProvider<MappingDescriptionProvider> descriptionProviders) {return new MappingsEndpoint(descriptionProviders.orderedStream().collect(Collectors.toList()),applicationContext);}// 后面还有关于Servlet的相关配置,此处就先不看

(2)MappingsEndpoint

// web访问路径
@Endpoint(id = "mappings")
public class MappingsEndpoint {
// org.springframework.boot.actuate.web.mappings.MappingsEndpoint#mappings
@ReadOperation
public ApplicationMappings mappings() {ApplicationContext target = this.context;Map<String, ContextMappings> contextMappings = new HashMap<>();while (target != null) {// 查找所有的MappingcontextMappings.put(target.getId(), mappingsForContext(target));target = target.getParent();}return new ApplicationMappings(contextMappings);// 返回的信息就会json格式化到响应
}// org.springframework.boot.actuate.web.mappings.MappingsEndpoint#mappingsForContext
private ContextMappings mappingsForContext(ApplicationContext applicationContext) {Map<String, Object> mappings = new HashMap<>();this.descriptionProviders.forEach((provider) -> mappings.put(provider.getMappingName(), provider.describeMappings(applicationContext)));return new ContextMappings(mappings,(applicationContext.getParent() != null) ? applicationContext.getId() : null);
}

在这里插入图片描述

(3)执行结果

在这里插入图片描述
在这里插入图片描述

4、ShutdownEndpoint自动装配原理

(1)自动装配类

@AutoConfiguration
@ConditionalOnAvailableEndpoint(endpoint = ShutdownEndpoint.class)
public class ShutdownEndpointAutoConfiguration {@Bean(destroyMethod = "")@ConditionalOnMissingBeanpublic ShutdownEndpoint shutdownEndpoint() {return new ShutdownEndpoint();}}

(2)ShutdownEndpoint

// 默认是不开启的,因为安全性要求极高
@Endpoint(id = "shutdown", enableByDefault = false)
public class ShutdownEndpoint implements ApplicationContextAware {private static final Map<String, String> NO_CONTEXT_MESSAGE = Collections.unmodifiableMap(Collections.singletonMap("message", "No context to shutdown."));private static final Map<String, String> SHUTDOWN_MESSAGE = Collections.unmodifiableMap(Collections.singletonMap("message", "Shutting down, bye..."));private ConfigurableApplicationContext context;// 写操作@WriteOperationpublic Map<String, String> shutdown() {if (this.context == null) {return NO_CONTEXT_MESSAGE; // 返回的信息就会json格式化到响应}try {return SHUTDOWN_MESSAGE;}finally {// 另起一个线程进行shutdown操作,目的是优雅关机。Thread thread = new Thread(this::performShutdown);thread.setContextClassLoader(getClass().getClassLoader());thread.start();}}private void performShutdown() {try {// 暂停500毫秒Thread.sleep(500L);}catch (InterruptedException ex) {Thread.currentThread().interrupt();}// 执行Spring容器关闭操作this.context.close();}@Overridepublic void setApplicationContext(ApplicationContext context) throws BeansException {if (context instanceof ConfigurableApplicationContext) {this.context = (ConfigurableApplicationContext) context;}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/704836.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue_pdf,word,excel,pptx等文件预览

项目背景&#xff1a;vue3elementPlusvite 1.pdf 1.1 iframe预览 #toolbar0 拼接到src后&#xff0c;可隐藏iframe顶部的工具栏 <template><div class"viewPDF.vue"><uploadFile file"getFile" accept".pdf,.PDF" ></up…

redis八股

文章目录 数据类型字符串实现使用场景 List 列表实现使用场景 Hash 哈希实现使用场景 Set 集合实现使用场景 ZSet 有序集合实现使用场景 BitMap实现使用场景 Stream使用场景pubsub为什么不能作为消息队列 数据结构机制SDS 简单动态字符串压缩列表哈希表整数集合跳表quicklistli…

vue3+electron开发桌面应用,静态资源处理方式及路径问题总结

1、静态资源放到src/assets/目录下 静态资源,例如图片、静态的JSON文件、视频、CSS等等,放到src/assets目录下。 不然会很蛋疼,这个坑我踩过了。切记,切记!! 以下是CHATGPT-4 Turbo的回答: 在 Vue 应用程序中,src/assets 目录确实有特别的处理。当你使用 Vue CLI 创…

每日五道java面试题之spring篇(七)

目录&#xff1a; 第一题. 什么是Spring beans&#xff1f;第二题. 一个 Spring Bean 定义 包含什么&#xff1f;第三题. 如何给Spring 容器提供配置元数据&#xff1f;Spring有几种配置方式?第四题. Spring基于xml注入bean的几种方式?第五题&#xff1a;你怎样定义类的作用域…

【USENIX论文阅读】Day2

Birds of a Feather Flock Together: How Set Bias Helps to Deanonymize You via Revealed Intersection Sizes&#xff08;"物以类聚&#xff1a;集合偏差如何帮助去匿名化——通过揭示交集大小&#xff09; Xiaojie Guo, Ye Han, Zheli Liu, Ding Wang, Yan Jia, Jin L…

thinkphp6定时任务

这里主要是教没有用过定时任务没有头绪的朋友, 定时任务可以处理一些定时备份数据库等一系列操作, 具体根据自己的业务逻辑进行更改 直接上代码 首先, 是先在 tp 中的 command 方法中声明, 如果没有就自己新建一个, 代码如下 然后就是写你的业务逻辑 执行定时任务 方法写好了…

ConvNeXt V2:用MAE训练CNN

论文名称&#xff1a;ConvNeXt V2: Co-designing and Scaling ConvNets with Masked Autoencoders 发表时间&#xff1a;CVPR2023 code链接&#xff1a;代码 作者及组织: Sanghyun Woo&#xff0c;Shoubhik Debnath来自KAIST和Meta AI。 前言 ConvNextV2是借助MAE的思想来训练…

【PyTorch][chapter 18][李宏毅深度学习]【无监督学习][ VAE]

前言: VAE——Variational Auto-Encoder&#xff0c;变分自编码器&#xff0c;是由 Kingma 等人于 2014 年提出的基于变分贝叶斯&#xff08;Variational Bayes&#xff0c;VB&#xff09;推断的生成式网络结构。与传统的自编码器通过数值的方式描述潜在空间不同&#xff0c;它…

排序(9.17)

1.排序的概念及其运用 1.1排序的概念 排序 &#xff1a;所谓排序&#xff0c;就是使一串记录&#xff0c;按照其中的某个或某些关键字的大小&#xff0c;递增或递减的排列起来的操作。 稳定性 &#xff1a;假定在待排序的记录序列中&#xff0c;存在多个具有相同的关键字的记…

实战 vue3 使用百度编辑器ueditor

前言 在开发项目由于需求vue自带对编辑器不能满足使用&#xff0c;所以改为百度编辑器&#xff0c;但是在网上搜索发现都讲得非常乱&#xff0c;所以写一篇使用流程的文章 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、下载ueditor编辑器 一个“…

三数之和(哈希,双指针)

15. 三数之和 - 力扣&#xff08;LeetCode&#xff09; 方法1&#xff1a;哈希算法&#xff08;不推荐&#xff09; 缺点&#xff1a;时间复杂度O&#xff08;N^2&#xff09;&#xff0c;去重情况复杂 class Solution { public:vector<vector<int>> threeSum(ve…

【Java EE初阶二十五】简单的表白墙(一)

1. 前端部分 1.1 前端代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"wid…

2步破解官方sublime4

sublime简要破解流程 1.下载sublime官方最新版2. 破解流程 1.下载sublime官方最新版 打开 官方网站下载 portable version 版&#xff0c;省的安装。。解压到任意位置&#xff0c;备份 sublime_text.exe 文件 2. 破解流程 打开网址把文件 sublime_text.exe 拖入网页搜索替换…

【非递归版】归并排序算法(2)

目录 MergeSortNonR归并排序 非递归&归并排序VS快速排序 整体思想 图解分析​ 代码实现 时间复杂度 归并排序在硬盘上的应用&#xff08;外排序&#xff09; MergeSortNonR归并排序 前面的快速排序的非递归实现&#xff0c;我们借助栈实现。这里我们能否也借助栈去…

国产服务器操作系统

为何记录 最近的开发工作经常接触到国产服务器操作系统的业务&#xff0c;经常被x86、arm、龙芯、鲲鹏、欧拉...搞得一脸懵逼&#xff0c;遂记之&#xff01; 操作系统 这里按照应用场景分&#xff1a; 桌面操作系统&#xff1a;主要用于pc&#xff0c;如Windows、macOS、Li…

MATLAB练习题:电子管的更换策略问题

​讲解视频&#xff1a;可以在bilibili搜索《MATLAB教程新手入门篇——数学建模清风主讲》。​ MATLAB教程新手入门篇&#xff08;数学建模清风主讲&#xff0c;适合零基础同学观看&#xff09;_哔哩哔哩_bilibili 在一台设备上&#xff0c;安装有四只型号和规格完全相同的电子…

腾讯design vue项目 上传桶 腾讯云的桶 对象存储 打包web端项目上传dist

1.说明 将腾讯design 项目上传到 腾讯云的对象存储中 &#xff0c;但是发现 再这个腾讯design项目中 直接npm run build 打包以后 上传 发现 不能用 需要配置东西 2.解决 使用腾讯云的cos-nodejs-sdk-v5 插件 代码上传 cos-nodejs-sdk-v5 - npm npm i cos-nodejs-sdk-v5 …

[算法沉淀记录]排序算法 —— 快速排序

排序算法 —— 快速排序介绍 基本概念 快速排序&#xff08;Quicksort&#xff09;是一种排序算法&#xff0c;最早由东尼霍尔提出。在平均状况下&#xff0c;排序 n 个项目要 Ο(n log n) 次比较。在最坏状况下则需要 Ο(n2) 次比较&#xff0c;但这种状况并不常见。事实上&…

《论文阅读》一个基于情感原因的在线共情聊天机器人 SIGIR 2021

《论文阅读》一个基于情感原因的在线共情聊天机器人 前言简介数据集构建模型架构损失函数实验结果咨询策略总结前言 亲身阅读感受分享,细节画图解释,再也不用担心看不懂论文啦~ 无抄袭,无复制,纯手工敲击键盘~ 今天为大家带来的是《Towards an Online Empathetic Chatbot…

EMQX Enterprise 5.5 发布:新增 Elasticsearch 数据集成

EMQX Enterprise 5.5.0 版本已正式发布&#xff01; 在这个版本中&#xff0c;我们引入了一系列新的功能和改进&#xff0c;包括对 Elasticsearch 的集成、Apache IoTDB 和 OpenTSDB 数据集成优化、授权缓存支持排除主题等功能。此外&#xff0c;新版本还进行了多项改进以及 B…