最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解析springBoot-actuator項(xiàng)目構(gòu)造中health端點(diǎn)工作原理

 更新時(shí)間:2022年02月25日 15:29:46   作者:kl  
這篇文章主要介紹了springBoot-actuator中health端點(diǎn)工作原理,對(duì)spring-boot-actuator的項(xiàng)目構(gòu)造,工作原理進(jìn)行了全面的梳理,側(cè)重health健康檢查部分

前言

最近在一個(gè)webflux項(xiàng)目中使用spring-boot-actuator提供的健康檢查端點(diǎn)時(shí)出了點(diǎn)問(wèn)題,故對(duì)spring-boot-actuator的項(xiàng)目構(gòu)造,工作原理進(jìn)行了全面的梳理,標(biāo)題之所以寫明health的工作原理,是因?yàn)閟pring-boot-actuator著實(shí)是個(gè)大工程,除了提供health端點(diǎn),還包含了env,log,dump等諸多功能,下面會(huì)側(cè)重health健康檢查部分,詳細(xì)探索下。

actuator功能和集成分離

一般在spring boot中使用actuator的時(shí)候,會(huì)引入下面這個(gè)starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在這個(gè)starter里面會(huì)包含兩個(gè)依賴,一個(gè)是功能實(shí)現(xiàn)spring-boot-actuator

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-actuator</artifactId>
      <version>2.1.0.RELEASE</version>
</dependency>

還有一個(gè)是和spring boot做集成的config配置,以及Bean自動(dòng)裝配的依賴,如下:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-actuator-autoconfigure</artifactId>
      <version>2.1.0.RELEASE</version>
</dependency>

actuator自動(dòng)裝載

找到spring-boot-actuator-autoconfigure依賴,定位到org.springframework.boot.actuate.autoconfigure.health包下,有如下的結(jié)構(gòu):

如箭頭所指向的HealthEndpointAutoConfiguration.java自動(dòng)配置類就是actuator中health的啟動(dòng)入口,源碼如下:

@Configuration
@EnableConfigurationProperties({ HealthEndpointProperties.class,
      HealthIndicatorProperties.class })
@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)
@Import({ HealthEndpointConfiguration.class,
      HealthEndpointWebExtensionConfiguration.class })
public class HealthEndpointAutoConfiguration {

}

閱讀上面代碼需要了解spring boot自動(dòng)裝載機(jī)制,這里簡(jiǎn)單解讀下,首先@Configuration開(kāi)啟了配置特性,@EnableConfigurationProperties啟用了健康檢查端點(diǎn)、健康檢查指示器的屬性配置,@AutoConfigureAfter定義了健康檢查自動(dòng)裝配要在HealthIndicatorAutoConfiguration之后,@Import包含了兩個(gè)自動(dòng)裝載類,下面詳解下三個(gè)主要的配置類:

健康檢查指示器配置

HEALTHINDICATORAUTOCONFIGURATION

健康檢查指示器定義了哪些組件需要被檢測(cè),常見(jiàn)的指示器有JDBC數(shù)據(jù)源(DataSourceHealthIndicator.java),磁盤健康指示器(DiskSpaceHealthIndicator.java)等。每個(gè)指示器對(duì)應(yīng)了一個(gè)自動(dòng)裝配的類,根據(jù)Bean初始化條件去初始化,如JDBC數(shù)據(jù)源的初始化條件如下:

當(dāng)上Spring上下文中包含DataSource實(shí)施,即開(kāi)啟JDBC健康檢查指示器。這些指示器最終會(huì)被收集到指示器注冊(cè)器中DefaultHealthIndicatorRegistry.java

健康檢查指示器配置就是完成了指示器注冊(cè)器的初始化動(dòng)作,代碼如:

@Bean
	@ConditionalOnMissingBean(HealthIndicatorRegistry.class)
	public HealthIndicatorRegistry healthIndicatorRegistry(
			ApplicationContext applicationContext) {
		return HealthIndicatorRegistryBeans.get(applicationContext);
	}
	public static HealthIndicatorRegistry get(ApplicationContext applicationContext) {
		Map<String, HealthIndicator> indicators = new LinkedHashMap<>();
		indicators.putAll(applicationContext.getBeansOfType(HealthIndicator.class));
		if (ClassUtils.isPresent("reactor.core.publisher.Flux", null)) {
			new ReactiveHealthIndicators().get(applicationContext)
					.forEach(indicators::putIfAbsent);
		}
		HealthIndicatorRegistryFactory factory = new HealthIndicatorRegistryFactory();
		return factory.createHealthIndicatorRegistry(indicators);
	}

可以看到,就是去Spring 應(yīng)用上下文ApplicationContext中找Bean類型是HealthIndicator.class的實(shí)例,如果項(xiàng)目中使用了webFlux,會(huì)額外注冊(cè)Reactive相關(guān)的指示器

健康檢查端點(diǎn)配置

端點(diǎn)配置比較簡(jiǎn)單,就是實(shí)例化一個(gè)HealthEndpoint.java,最終健康檢查所有的功能入口都會(huì)被抽象匯聚到這個(gè)實(shí)例里,配置代碼如下:

@Configuration
@ConditionalOnSingleCandidate(HealthIndicatorRegistry.class)
class HealthEndpointConfiguration {

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnEnabledEndpoint
	public HealthEndpoint healthEndpoint(HealthAggregator healthAggregator,
			HealthIndicatorRegistry registry) {
		return new HealthEndpoint(
				new CompositeHealthIndicator(healthAggregator, registry));
	}
}

可以看到前提條件是已經(jīng)有一個(gè)健康指示注冊(cè)器單例實(shí)例了

health健康檢查實(shí)現(xiàn)

在spring-boot-actuator中,定義了@Endpoint注解,用以聲明一個(gè)actuator端點(diǎn),health端點(diǎn)也是一樣,通過(guò)@Endpoint(id="health")暴露了/actuator/health接口。并通過(guò)@ReadOperation注解映射了三個(gè)方法,如下:

Health health()

       訪問(wèn)http://127.0.0.1:8080/actuator/health時(shí)會(huì)執(zhí)行這個(gè)方法,調(diào)用所有的健康指示器實(shí)現(xiàn),并返回結(jié)果

Health healthForComponent(@Selector String component)

       訪問(wèn)http://127.0.0.1:8080/actuator/health/${component}時(shí)會(huì)執(zhí)行這個(gè)方法,會(huì)根據(jù)component的值,找到相關(guān)的指示器,并檢查返回結(jié)果

Health healthForComponentInstance(@Selector String component, @Selector String instance)

       訪問(wèn)http://127.0.0.1:8080/actuator/health/${component}/${instance}時(shí)會(huì)執(zhí)行這個(gè)方法,會(huì)根據(jù)component、instance的值,找到相關(guān)的指示器,并檢查返回結(jié)果。其中component是組件的name,instance是組件實(shí)例的name值。component的name由執(zhí)行器組件配置類上的注解@ConditionalOnEnabledHealthIndicator來(lái)指定,目前包含的指示器組件有如:

我們以redis的指示器RedisHealthIndicator.java來(lái)看下,最終指示器是怎么判斷組件是否健康的,實(shí)現(xiàn)如:

public class RedisHealthIndicator extends AbstractHealthIndicator {
	static final String VERSION = "version";
	static final String REDIS_VERSION = "redis_version";
	private final RedisConnectionFactory redisConnectionFactory;
	public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
		super("Redis health check failed");
		Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
		this.redisConnectionFactory = connectionFactory;
	}
	@Override
	protected void doHealthCheck(Health.Builder builder) throws Exception {
		RedisConnection connection = RedisConnectionUtils
				.getConnection(this.redisConnectionFactory);
		try {
			if (connection instanceof RedisClusterConnection) {
				ClusterInfo clusterInfo = ((RedisClusterConnection) connection)
						.clusterGetClusterInfo();
				builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
						.withDetail("slots_up", clusterInfo.getSlotsOk())
						.withDetail("slots_fail", clusterInfo.getSlotsFail());
			}
			else {
				Properties info = connection.info();
				builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION));
			}
		}
		finally {
			RedisConnectionUtils.releaseConnection(connection,
					this.redisConnectionFactory);
		}
	}
}

可以看到,首先判斷了連接的類型時(shí)集群模式還是單機(jī)模式,然后分別調(diào)用了info指令,去拿redis的版本信息

自定義健康檢查指示器

了解到這里,自定義實(shí)現(xiàn)一個(gè)組件的健康檢查就容易了。首先自定義指示器繼承AbstractHealthIndicator類,實(shí)現(xiàn)doHealthCheck方法,然后定義自定義指示器的配置類繼承CompositeHealthIndicatorConfiguration就ok了,偽代碼如下:

@ConditionalOnEnabledHealthIndicator("myDb")
@Configuration
public class MyHealthIndicatorAutoConfiguration extends CompositeHealthIndicatorConfiguration<DataSourceHealthIndicator,DataSource> {
    @Bean
    @ConditionalOnMissingBean(name = "myDbHealthIndicator")
    public HealthIndicator dbHealthIndicator() {
        return new MyHealthIndicator();
    }
}
class MyHealthIndicator extends AbstractHealthIndicator{
    @Override
    protected void doHealthCheck(Health.Builder builder) {
        //這里定義組建健康的邏輯
        builder.up();
    }
}

health其他使用細(xì)節(jié)

除了上面提到的健康檢查不只/actuator/health端點(diǎn),還能指定組件檢查外,還提供了很多可以通過(guò)配置控制的特性,如指示器的開(kāi)關(guān),什么時(shí)候顯示健康檢查詳情等,具體如下:

management.endpoints.web.base-path=/actuator
management.endpoint.health.enabled=true
management.endpoint.health.show-details=never
management.endpoint.health.roles=admin
management.health.db.enabled=true

文末結(jié)語(yǔ)

本著用好每一個(gè)組件,不放過(guò)任何一個(gè)實(shí)現(xiàn)細(xì)節(jié)的原則,對(duì)spring-boot-actuator中的health實(shí)現(xiàn)原理剖析了下。不過(guò)actuator真的是個(gè)大家伙,光健康檢查指示器就有18個(gè)實(shí)現(xiàn),特別要說(shuō)明下的是,針對(duì)health,在做健康檢查指示器時(shí),會(huì)區(qū)分web和webFlux。主要原因是在webFlux的環(huán)境下,相關(guān)的組件也會(huì)出Reactive的客戶端,比如redis在webFlux下就可以使用Lettuce。

以上就是解析springBoot-actuator中health端點(diǎn)工作原理的詳細(xì)內(nèi)容,更多關(guān)于springBoot-actuator中health原理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot如何通過(guò)@Profile注解配置多環(huán)境

    SpringBoot如何通過(guò)@Profile注解配置多環(huán)境

    在Spring中,可以使用配置文件的方式來(lái)指定不同環(huán)境下所需要的配置信息,本文給大家介紹SpringBoot如何通過(guò)@Profile注解配置多環(huán)境,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • 一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    這篇文章主要介紹了Mybatis中Mapper配置文件獲取參數(shù)的五種方式,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • Spring JDBC的使用方法詳解

    Spring JDBC的使用方法詳解

    這篇文章主要介紹了Spring JDBC的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • spring data jpa分頁(yè)查詢示例代碼

    spring data jpa分頁(yè)查詢示例代碼

    本篇文章主要介紹了spring data jpa分頁(yè)查詢示例代碼,分頁(yè)在很多項(xiàng)目中都能使用,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-03-03
  • java線程池參數(shù)位置導(dǎo)致的奪命故障宿主機(jī)打不開(kāi)

    java線程池參數(shù)位置導(dǎo)致的奪命故障宿主機(jī)打不開(kāi)

    這篇文章主要為大家介紹了java線程池參數(shù)位置導(dǎo)致的奪命故障宿主機(jī)打不開(kāi)的問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 一文詳解Reactor模型與實(shí)現(xiàn)示例

    一文詳解Reactor模型與實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了Reactor模型與實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Java行為型設(shè)計(jì)模式之外觀設(shè)計(jì)模式詳解

    Java行為型設(shè)計(jì)模式之外觀設(shè)計(jì)模式詳解

    外觀模式為多個(gè)復(fù)雜的子系統(tǒng),提供了一個(gè)一致的界面,使得調(diào)用端只和這個(gè)接口發(fā)生調(diào)用,而無(wú)須關(guān)系這個(gè)子系統(tǒng)內(nèi)部的細(xì)節(jié)。本文將通過(guò)示例詳細(xì)為大家講解一下外觀模式,需要的可以參考一下
    2022-11-11
  • Java二叉搜索樹(shù)遍歷操作詳解【前序、中序、后序、層次、廣度優(yōu)先遍歷】

    Java二叉搜索樹(shù)遍歷操作詳解【前序、中序、后序、層次、廣度優(yōu)先遍歷】

    這篇文章主要介紹了Java二叉搜索樹(shù)遍歷操作,結(jié)合實(shí)例形式詳細(xì)分析了Java二叉搜索樹(shù)前序、中序、后序、層次、廣度優(yōu)先遍歷等相關(guān)原理與操作技巧,需要的朋友可以參考下
    2020-03-03
  • JVM,JRE和JDK的區(qū)別小結(jié)

    JVM,JRE和JDK的區(qū)別小結(jié)

    在Java環(huán)境配置和項(xiàng)目啟動(dòng)中,JVM,JRE和JDK這三者的配置是項(xiàng)目啟動(dòng)的基礎(chǔ)保證,本文就詳細(xì)的介紹一下這三者的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • java面試常見(jiàn)問(wèn)題---ConcurrentHashMap

    java面試常見(jiàn)問(wèn)題---ConcurrentHashMap

    ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見(jiàn)問(wèn)題---ConcurrentHashMap知識(shí),一起看看吧
    2021-06-06

最新評(píng)論

大庆市| 三河市| 沿河| 台东县| 繁峙县| 堆龙德庆县| 缙云县| 皋兰县| 忻州市| 中方县| 康平县| 永城市| 呈贡县| 吴堡县| 滨海县| 嘉祥县| 若尔盖县| 如东县| 田阳县| 台南市| 依安县| 阳信县| 思南县| 陇川县| 洛南县| 九寨沟县| 广南县| 平昌县| 老河口市| 应城市| 武城县| 乐至县| 临武县| 瑞昌市| 枣强县| 八宿县| 绥宁县| 嫩江县| 昭通市| 桂平市| 兰西县|