SpringBoot實現監控Actuator,關閉redis監測

SpringBoot監控Actuator,關閉redis監測

方法

當我們導入瞭spring-boot-starter-actuator這個依賴後, SpringBoot會默認去監測一些信息。其中就包括redis、 會根據redis的默認初始配置, localhost:6379 嘗試連接redis。如果我們沒有用到redis, 啟動就會報錯

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

比如我在使用SpringBootAdmin的時候,其中內置瞭Actuator的依賴, 啟動報錯

[WARN ] 2020-05-28 09:28:17.179 [registrationTask1] de.codecentric.boot.admin.client.registration.ApplicationRegistrator – Failed to register application as Application(name=admin-client, managementUrl=http://Gordon-pc:8768/actuator, healthUrl=http://Gordon-pc:8768/actuator/health, serviceUrl=http://Gordon-pc:8768/) at spring-boot-admin ([http://localhost:8769/instances]): I/O error on POST request for “http://localhost:8769/instances”: Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect. Further attempts are logged on DEBUG level
[WARN ] 2020-05-28 09:28:19.075 [boundedElastic-1] org.springframework.boot.actuate.redis.RedisReactiveHealthIndicator – Redis health check failed
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1200)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1179)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedReactiveConnection(LettuceConnectionFactory.java:952)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:429)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:94)
at reactor.core.publisher.MonoSupplier.call(MonoSupplier.java:85)
at reactor.core.publisher.FluxSubscribeOnCallable$CallableSubscribeOnSubscription.run(FluxSubscribeOnCallable.java:225)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:234)
at io.lettuce.core.RedisClient.connect(RedisClient.java:207)
at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115)
at java.util.Optional.orElseGet(Optional.java:267)
at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1198)
… 15 common frames omitted

在網上搜瞭下解決方法, 發現都是提供的這一種方式:

@Component("redisHealthIndicator")
public class RedisHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }    
}

結果我在看配置文件的時候, 發現另一個簡單的方式: application.yml中加上

management:
  health:
    redis:
      enabled: false

就可以瞭~

springboot Actuator

通過Actuator可以深入的瞭解springboot應用程序的內部, 可以進行監控和度量一些配置信息。

這些特性可以通過rest端點、遠程shell和jmx獲得。

以下主要探討rest端點的使用。

查看配置明細

1)/beans 上下文bean的情況

查看spring上下文中bean的情況, /beans會返回一個json文檔,描述上下文裡每個Bean的情況,類型、依賴註入

在這裡插入圖片描述

2)/autoconfig 自動配置情況

positiveMatches指定的自動配置表示匹配成功的bean
negativeMatches對應的自動配置表示匹配失敗的bean

3)/env 查看配置屬性

包括:環境變量、JVM屬性、命令行參數,以及application.properties或application.yml文件提供的屬性

運行時度量

對運行時度量情況作一個快照,以此檢查應用程序的健康狀況。

1)/metrics 應用程序的度量值

在這裡插入圖片描述

2)/trace 跟蹤web請求的所有信息

包括請求方法、路徑、時間戳,以及請求和響應的頭信息。

3)/health 健康檢查器

可以檢查應用程序啟動狀況,以及相關依賴中間件的連接狀態。

在這裡插入圖片描述

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: