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

spring boot的健康檢查HealthIndicators實(shí)戰(zhàn)

 更新時(shí)間:2021年10月19日 12:13:09   作者:南北雪樹(shù)  
這篇文章主要介紹了spring boot的健康檢查HealthIndicators實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot 健康檢查HealthIndicators

想提供自定義健康信息,你可以注冊(cè)實(shí)現(xiàn)了HealthIndicator接口的Spring beans。

你需要提供一個(gè)health()方法的實(shí)現(xiàn),并返回一個(gè)Health響應(yīng)。

Health響應(yīng)需要包含一個(gè)status和可選的用于展示的詳情。

import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealth implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
} r
eturn Health.up().build();
}
}

除了Spring Boot預(yù)定義的Status類型,Health也可以返回一個(gè)代表新的系統(tǒng)狀態(tài)的自定義Status。

在這種情況下,需要提供一個(gè)HealthAggregator接口的自定義實(shí)現(xiàn),或使用management.health.status.order屬性配置默認(rèn)的實(shí)現(xiàn)。

例如,假設(shè)一個(gè)新的,代碼為FATAL的Status被用于你的一個(gè)HealthIndicator實(shí)現(xiàn)中。 為了配置嚴(yán)重程度, 你需要將下面的配

置添加到application屬性文件中:

management.health.status.order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP

如果使用HTTP訪問(wèn)health端點(diǎn), 你可能想要注冊(cè)自定義的status, 并使用HealthMvcEndpoint進(jìn)行映射。 例如, 你可以將

FATAL映射為HttpStatus.SERVICE_UNAVAILABLE。

springboot health indicator原理及其使用

作用

sping boot health 可以通過(guò)暴露的接口來(lái)提供系統(tǒng)及其系統(tǒng)組件是否可用。默認(rèn)通過(guò)/health來(lái)訪問(wèn)。返回結(jié)果如下:

{
  "status": "UP",
  "discoveryComposite": {
    "description": "Spring Cloud Eureka Discovery Client",
    "status": "UP",
    "discoveryClient": {
      "description": "Spring Cloud Eureka Discovery Client",
      "status": "UP",
      "services": [
        "..."
      ]
    },
    "eureka": {
      "description": "Remote status from Eureka server",
      "status": "UP",
      "applications": {
        "AOMS-MOBILE": 1,
        "DATA-EXCHANGE": 1,
        "CLOUD-GATEWAY": 2,
        "AOMS": 1,
        "AOMS-AIIS": 0,
        "AOMS-EUREKA": 2
      }
    }
  },
  "diskSpace": {
    "status": "UP",
    "total": 313759301632,
    "free": 291947081728,
    "threshold": 10485760
  },
  "refreshScope": {
    "status": "UP"
  },
  "hystrix": {
    "status": "UP"
  }
}

狀態(tài)說(shuō)明:

  • UNKNOWN:未知狀態(tài),映射HTTP狀態(tài)碼為503
  • UP:正常,映射HTTP狀態(tài)碼為200
  • DOWN:失敗,映射HTTP狀態(tài)碼為503
  • OUT_OF_SERVICE:不能對(duì)外提供服務(wù),但是服務(wù)正常。映射HTTP狀態(tài)碼為200

注意:UNKNOWN,DOWN,OUT_OF_SERVICE在為微服務(wù)環(huán)境下會(huì)導(dǎo)致注冊(cè)中心中的實(shí)例也為down狀態(tài),請(qǐng)根據(jù)具體的業(yè)務(wù)來(lái)正確使用狀態(tài)值。

自動(dòng)配置的Health Indicator

自動(dòng)配置的HealthIndicator主要有以下內(nèi)容:

Key Name Description
cassandra CassandraDriverHealthIndicator Checks that a Cassandra database is up.
couchbase CouchbaseHealthIndicator Checks that a Couchbase cluster is up.
datasource DataSourceHealthIndicator Checks that a connection to DataSource can be obtained.
diskspace DiskSpaceHealthIndicator Checks for low disk space.
elasticsearch ElasticsearchRestHealthIndicator Checks that an Elasticsearch cluster is up.
hazelcast HazelcastHealthIndicator Checks that a Hazelcast server is up.
influxdb InfluxDbHealthIndicator Checks that an InfluxDB server is up.
jms JmsHealthIndicator Checks that a JMS broker is up.
ldap LdapHealthIndicator Checks that an LDAP server is up.
mail MailHealthIndicator Checks that a mail server is up.
mongo MongoHealthIndicator Checks that a Mongo database is up.
neo4j Neo4jHealthIndicator Checks that a Neo4j database is up.
ping PingHealthIndicator Always responds with UP.
rabbit RabbitHealthIndicator Checks that a Rabbit server is up.
redis RedisHealthIndicator Checks that a Redis server is up.
solr SolrHealthIndicator Checks that a Solr server is up.

分組

可以通過(guò)一個(gè)別名來(lái)啟用一組指標(biāo)的訪問(wèn)。配置的格式如下:

management.endpoint.health.group.<name>
//demo:
management.endpoint.health.group.mysys.include=db,redis,mail
management.endpoint.health.group.custom.exclude=rabbit

如何管理Health Indicator

開(kāi)啟

可以通過(guò)management.health.key.enabled來(lái)啟用key對(duì)應(yīng)的indicator。例如:

management.health.db.enabled=true

關(guān)閉

management.health.db.enabled=false

RedisHealthIndicator源碼解析

下面,通過(guò)RedisHealthIndicator源碼來(lái)為什么可以這么寫(xiě)。

代碼結(jié)構(gòu)

自動(dòng)配置的health indicator有HealthIndicator和HealthIndicatorAutoConfiguration兩部分組成。

HealthIndicator所在包在org.springframework.boot.actuate,

HealthIndicatorAutoConfiguration所在包在org.springframework.boot.actuate.autoconfigure下

//RedisHealthIndicator.java
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í)現(xiàn)doHealthCheck方法來(lái)實(shí)現(xiàn)具體的判斷邏輯。注意,操作完成后在finally中釋放資源。

在父類AbstractHealthIndicator中,對(duì)doHealthCheck進(jìn)行了try catch,如果出現(xiàn)異常,則返回Down狀態(tài)。

 //AbstractHealthIndicator.java
 @Override
 public final Health health() {
  Health.Builder builder = new Health.Builder();
  try {
   doHealthCheck(builder);
  }
  catch (Exception ex) {
   if (this.logger.isWarnEnabled()) {
    String message = this.healthCheckFailedMessage.apply(ex);
    this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
      ex);
   }
   builder.down(ex);
  }
  return builder.build();
 }

RedisHealthIndicatorAutoConfiguration完成RedisHealthIndicator的自動(dòng)配置

@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({ RedisAutoConfiguration.class,
  RedisReactiveHealthIndicatorAutoConfiguration.class })
public class RedisHealthIndicatorAutoConfiguration extends
  CompositeHealthIndicatorConfiguration<RedisHealthIndicator, RedisConnectionFactory> {
 private final Map<String, RedisConnectionFactory> redisConnectionFactories;
 public RedisHealthIndicatorAutoConfiguration(
   Map<String, RedisConnectionFactory> redisConnectionFactories) {
  this.redisConnectionFactories = redisConnectionFactories;
 }
 @Bean
 @ConditionalOnMissingBean(name = "redisHealthIndicator")
 public HealthIndicator redisHealthIndicator() {
  return createHealthIndicator(this.redisConnectionFactories);
 }
}

重點(diǎn)說(shuō)明ConditionalOnEnabledHealthIndicator:如果management.health..enabled為true,則生效。

CompositeHealthIndicatorConfiguration 中會(huì)通過(guò)HealthIndicatorRegistry注冊(cè)創(chuàng)建的HealthIndicator

自定義Indicator

@Component
@ConditionalOnProperty(name="spring.dfs.http.send-url")
@Slf4j
public class DfsHealthIndicator implements HealthIndicator {
    @Value("${spring.dfs.http.send-url}")
    private String dsfSendUrl;
    @Override
    public Health health() {
        log.debug("正在檢查dfs配置項(xiàng)...");
        log.debug("dfs 請(qǐng)求地址:{}",dsfSendUrl);
        Health.Builder up = Health.up().withDetail("url", dsfSendUrl);
        try {
            HttpUtils.telnet(StringUtils.getIpFromUrl(dsfSendUrl),StringUtils.getPortFromUrl(dsfSendUrl));
            return up.build();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("DFS配置項(xiàng)錯(cuò)誤或網(wǎng)絡(luò)超時(shí)");
            return up.withException(e).build();
        }
    }
}

返回值:

{
    "dfs": {
    "status": "UP",
    "url": "10.254.131.197:8088",
    "error": "java.net.ConnectException: Connection refused (Connection refused)"
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解析Tars-Java客戶端源碼

    解析Tars-Java客戶端源碼

    Tars是基于名字服務(wù)使用Tars協(xié)議的高性能RPC開(kāi)發(fā)框架,同時(shí)配套一體化的服務(wù)治理平臺(tái),幫助個(gè)人或者企業(yè)快速的以微服務(wù)的方式構(gòu)建自己穩(wěn)定可靠的分布式應(yīng)用
    2021-06-06
  • SpringBoot對(duì)Druid配置SQL監(jiān)控功能失效問(wèn)題及解決方法

    SpringBoot對(duì)Druid配置SQL監(jiān)控功能失效問(wèn)題及解決方法

    這篇文章主要介紹了SpringBoot對(duì)Druid配置SQL監(jiān)控功能失效問(wèn)題的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Java之maven打完jar包之后將jar包放到指定位置匯總

    Java之maven打完jar包之后將jar包放到指定位置匯總

    這篇文章主要介紹了Java之maven打完jar包之后將jar包放到指定位置匯總,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • java 鍵盤(pán)輸入的多種實(shí)現(xiàn)方法

    java 鍵盤(pán)輸入的多種實(shí)現(xiàn)方法

    java不像C中擁有scanf這樣功能強(qiáng)大的函數(shù),大多是通過(guò)定義輸入輸出流對(duì)象。常用的類有BufferedReader,Scanner。
    2013-03-03
  • java設(shè)計(jì)模式--橋接模式詳解

    java設(shè)計(jì)模式--橋接模式詳解

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之橋接模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助
    2021-07-07
  • Java基礎(chǔ)夯實(shí)之線程問(wèn)題全面解析

    Java基礎(chǔ)夯實(shí)之線程問(wèn)題全面解析

    操作系統(tǒng)支持多個(gè)應(yīng)用程序并發(fā)執(zhí)行,每個(gè)應(yīng)用程序至少對(duì)應(yīng)一個(gè)進(jìn)程?。進(jìn)程是資源分配的最小單位,而線程是CPU調(diào)度的最小單位。本文將帶大家全面解析線程相關(guān)問(wèn)題,感興趣的可以了解一下
    2022-11-11
  • 關(guān)于JSON.toJSONString()和Gson.toJson()方法的比較

    關(guān)于JSON.toJSONString()和Gson.toJson()方法的比較

    本文介紹了兩種將Java對(duì)象轉(zhuǎn)換為JSON字符串的方法:阿里的`JSON.toJSONString()`和谷歌的`Gson.toJson()`,通過(guò)一個(gè)示例,展示了當(dāng)使用繼承關(guān)系且子類覆蓋父類字段時(shí),`Gson`會(huì)報(bào)錯(cuò),而`JSON`可以正常運(yùn)行,作者建議在處理JSON相關(guān)操作時(shí)使用阿里的`JSON`類
    2024-11-11
  • 一小時(shí)迅速入門(mén)Mybatis之增刪查改篇

    一小時(shí)迅速入門(mén)Mybatis之增刪查改篇

    這篇文章主要介紹了迅速入門(mén)Mybatis之增刪查改篇,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot集成JPA持久層框架,簡(jiǎn)化數(shù)據(jù)庫(kù)操作

    SpringBoot集成JPA持久層框架,簡(jiǎn)化數(shù)據(jù)庫(kù)操作

    JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化規(guī)范。主要是為了簡(jiǎn)化持久層開(kāi)發(fā)以及整合ORM技術(shù),結(jié)束Hibernate、TopLink、JDO等ORM框架各自為營(yíng)的局面。JPA是在吸收現(xiàn)有ORM框架的基礎(chǔ)上發(fā)展而來(lái),易于使用,伸縮性強(qiáng)。
    2021-06-06
  • Java微服務(wù)開(kāi)發(fā)之Swagger詳解

    Java微服務(wù)開(kāi)發(fā)之Swagger詳解

    Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步
    2021-10-10

最新評(píng)論

康平县| 明水县| 宕昌县| 双流县| 宣威市| 潮安县| 巴彦县| 璧山县| 佛坪县| 乡宁县| 乌鲁木齐县| 稷山县| 平阳县| 霍山县| 霍州市| 安宁市| 莱阳市| 深州市| 江阴市| 普定县| 水城县| 石台县| 泽普县| 兴国县| 西贡区| 新乡县| 德清县| 仁布县| 翁源县| 渑池县| 吉安县| 桐梓县| 开鲁县| 上蔡县| 岱山县| 准格尔旗| 阿鲁科尔沁旗| 石柱| 宣汉县| 平邑县| 昌平区|