Micronaut 內(nèi)置 Endpoints

2023-03-14 15:43 更新

將管理依賴項(xiàng)添加到您的項(xiàng)目后,默認(rèn)情況下會(huì)啟用以下內(nèi)置端點(diǎn):

表 1. 默認(rèn)端點(diǎn)
端點(diǎn) URI 描述

BeansEndpoint

/beans

返回有關(guān)應(yīng)用程序中加載的 bean 定義的信息

HealthEndpoint

/health

返回有關(guān)應(yīng)用程序“健康”的信息

InfoEndpoint

/info

從應(yīng)用程序狀態(tài)返回靜態(tài)信息

LoggersEndpoint

/loggers

返回有關(guān)可用記錄器的信息并允許更改配置的日志級(jí)別

MetricsEndpoint

/metrics

返回應(yīng)用程序指標(biāo)。需要類路徑上的千分尺核心配置。

RefreshEndpoint

/refresh

刷新應(yīng)用程序狀態(tài)

RoutesEndpoint

/routes

返回有關(guān)可供您的應(yīng)用程序調(diào)用的 URI 的信息

ThreadDumpEndpoint

/threaddump

返回有關(guān)應(yīng)用程序中當(dāng)前線程的信息。

此外,以下內(nèi)置端點(diǎn)由管理依賴項(xiàng)提供,但默認(rèn)情況下未啟用:

表 2. 禁用端點(diǎn)
端點(diǎn) URI 描述

EnvironmentEndpoint

/env

返回有關(guān)環(huán)境及其屬性源的信息

CachesEndpoint

/caches

返回有關(guān)緩存的信息并允許使它們失效

ServerStopEndpoint

/stop

關(guān)閉應(yīng)用程序服務(wù)器

定義 endpoints.all.sensitive: false 可以打開所有端點(diǎn)以進(jìn)行未經(jīng)身份驗(yàn)證的訪問,但這應(yīng)該謹(jǐn)慎使用,因?yàn)樗饺撕兔舾行畔⒈槐┞丁?/p>

管理端口

默認(rèn)情況下,所有管理端點(diǎn)都通過與應(yīng)用程序相同的端口公開。您可以通過指定 endpoints.all.port 設(shè)置來更改此行為:

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.all.port=8085
endpoints:
  all:
    port: 8085
[endpoints]
  [endpoints.all]
    port=8085
endpoints {
  all {
    port = 8085
  }
}
{
  endpoints {
    all {
      port = 8085
    }
  }
}
{
  "endpoints": {
    "all": {
      "port": 8085
    }
  }
}

在上面的示例中,管理端點(diǎn)僅通過端口 8085 公開。

JMX

Micronaut 提供了使用 JMX 注冊(cè)端點(diǎn)的功能。請(qǐng)參閱有關(guān) JMX 的部分以開始使用。

Beans 端點(diǎn)

beans 端點(diǎn)返回有關(guān)應(yīng)用程序中加載的 bean 定義的信息。默認(rèn)返回的bean數(shù)據(jù)是一個(gè)對(duì)象,其中鍵是bean定義類名,值是關(guān)于bean的屬性的對(duì)象。

要執(zhí)行 beans 端點(diǎn),請(qǐng)向 /beans 發(fā)送 GET 請(qǐng)求。

配置

要配置 beans 端點(diǎn),請(qǐng)通過 endpoints.beans 提供配置。

Beans 端點(diǎn)配置示例

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.beans.enabled=Boolean
endpoints.beans.sensitive=Boolean
endpoints:
  beans:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.beans]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  beans {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    beans {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "beans": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

自定義

beans 端點(diǎn)由 bean 定義數(shù)據(jù)收集器和 bean 數(shù)據(jù)實(shí)現(xiàn)組成。 bean 定義數(shù)據(jù)收集器 (BeanDefinitionDataCollector) 負(fù)責(zé)返回一個(gè)發(fā)布者,該發(fā)布者返回響應(yīng)中使用的數(shù)據(jù)。 bean 定義數(shù)據(jù) (BeanDefinitionData) 負(fù)責(zé)返回有關(guān)單個(gè) bean 定義的數(shù)據(jù)。

要覆蓋任一幫助器類的默認(rèn)行為,要么擴(kuò)展默認(rèn)實(shí)現(xiàn)(DefaultBeanDefinitionDataCollector、DefaultBeanDefinitionData),要么直接實(shí)現(xiàn)相關(guān)接口。為確保使用您的實(shí)現(xiàn)而不是默認(rèn)實(shí)現(xiàn),請(qǐng)將 @Replaces 注釋添加到您的類中,并將值為默認(rèn)實(shí)現(xiàn)。

信息端點(diǎn)

info 端點(diǎn)從應(yīng)用程序的狀態(tài)返回靜態(tài)信息。公開的信息可以由任意數(shù)量的“信息源”提供。

要執(zhí)行信息端點(diǎn),請(qǐng)向 /info 發(fā)送 GET 請(qǐng)求。

配置

要配置信息端點(diǎn),請(qǐng)通過 endpoints.info 提供配置。

信息端點(diǎn)配置示例

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.info.enabled=Boolean
endpoints.info.sensitive=Boolean
endpoints:
  info:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.info]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  info {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    info {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "info": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

自定義

信息端點(diǎn)由信息聚合器和任意數(shù)量的信息源組成。要添加信息源,請(qǐng)創(chuàng)建一個(gè)實(shí)現(xiàn) InfoSource 的 bean 類。如果您的信息源需要從 Java 屬性文件中檢索數(shù)據(jù),請(qǐng)擴(kuò)展 PropertiesInfoSource 接口,該接口為此提供了一個(gè)輔助方法。

所有信息源 bean 都與信息聚合器一起收集。要提供您自己的信息聚合器實(shí)現(xiàn),請(qǐng)創(chuàng)建一個(gè)實(shí)現(xiàn) InfoAggregator 的類并將其注冊(cè)為 bean。為確保使用您的實(shí)現(xiàn)而不是默認(rèn)實(shí)現(xiàn),請(qǐng)將 @Replaces 注釋添加到您的類中,并將值為默認(rèn)實(shí)現(xiàn)。

默認(rèn)信息聚合器返回一個(gè)映射,其中包含所有信息源返回的組合屬性。該地圖以 JSON 形式從 /info 端點(diǎn)返回。

提供的信息來源

配置信息源

ConfigurationInfoSource 返回 info 鍵下的配置屬性。除了字符串、整數(shù)和布爾值之外,更復(fù)雜的屬性可以在 JSON 輸出中作為映射公開(如果配置格式支持的話)。

Info Source Example (application.groovy)

info.demo.string = "demo string"
info.demo.number = 123
info.demo.map = [key: 'value', other_key: 123]

上面的配置導(dǎo)致來自 info 端點(diǎn)的以下 JSON 響應(yīng):

{
  "demo": {
    "string": "demo string",
    "number": 123,
    "map": {
      "key": "value",
      "other_key": 123
    }
  }
}

配置

可以使用 endpoints.info.config.enabled 屬性禁用配置信息源。

Git 信息源

如果 git.properties 文件在類路徑上可用,則 GitInfoSource 會(huì)在 git 鍵下公開該文件中的值。生成 git.properties 文件必須配置為構(gòu)建的一部分。 Gradle 用戶的一個(gè)簡(jiǎn)單選擇是 Gradle Git Properties Plugin。 Maven 用戶可以使用 Maven Git Commit ID Plugin。

配置

要指定屬性文件的備用路徑或名稱,請(qǐng)?jiān)?nbsp;endpoints.info.git.location 屬性中提供自定義值。

可以使用 endpoints.info.git.enabled 屬性禁用 git 信息源。

構(gòu)建信息源

如果 META-INF/build-info.properties 文件在類路徑上可用,則 BuildInfoSource 會(huì)在構(gòu)建鍵下公開該文件中的值。生成 build-info.properties 文件必須配置為構(gòu)建的一部分。 Gradle 用戶的一個(gè)簡(jiǎn)單選擇是 Gradle 構(gòu)建信息插件。 Maven 用戶的一個(gè)選項(xiàng)是 Spring Boot Maven 插件

配置

要指定屬性文件的備用路徑/名稱,請(qǐng)?jiān)?nbsp;endpoints.info.build.location 屬性中提供自定義值。

可以使用 endpoints.info.build.enabled 屬性禁用構(gòu)建信息源。

健康端點(diǎn)

健康端點(diǎn)返回有關(guān)應(yīng)用程序“健康”的信息,該信息由任意數(shù)量的“健康指標(biāo)”決定。

要執(zhí)行健康端點(diǎn),請(qǐng)向 /health 發(fā)送 GET 請(qǐng)求。此外,health 端點(diǎn)公開了 /health/liveness 和 /health/readiness 健康指標(biāo)。

配置

要配置健康端點(diǎn),請(qǐng)通過 endpoints.health 提供配置。

Health Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.health.enabled=Boolean
endpoints.health.sensitive=Boolean
endpoints.health.details-visible=String
endpoints.health.status.http-mapping=Map<String, HttpStatus>
endpoints:
  health:
    enabled: Boolean
    sensitive: Boolean
    details-visible: String
    status:
      http-mapping: Map<String, HttpStatus>
[endpoints]
  [endpoints.health]
    enabled="Boolean"
    sensitive="Boolean"
    details-visible="String"
    [endpoints.health.status]
      http-mapping="Map<String, HttpStatus>"
endpoints {
  health {
    enabled = "Boolean"
    sensitive = "Boolean"
    detailsVisible = "String"
    status {
      httpMapping = "Map<String, HttpStatus>"
    }
  }
}
{
  endpoints {
    health {
      enabled = "Boolean"
      sensitive = "Boolean"
      details-visible = "String"
      status {
        http-mapping = "Map<String, HttpStatus>"
      }
    }
  }
}
{
  "endpoints": {
    "health": {
      "enabled": "Boolean",
      "sensitive": "Boolean",
      "details-visible": "String",
      "status": {
        "http-mapping": "Map<String, HttpStatus>"
      }
    }
  }
}
  • details-visible 是 DetailsVisibility 之一

details-visible 設(shè)置控制是否向未通過身份驗(yàn)證的用戶公開健康詳細(xì)信息。

例如,設(shè)置:

Using details-visible

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.health.details-visible=ANONYMOUS
endpoints:
  health:
    details-visible: ANONYMOUS
[endpoints]
  [endpoints.health]
    details-visible="ANONYMOUS"
endpoints {
  health {
    detailsVisible = "ANONYMOUS"
  }
}
{
  endpoints {
    health {
      details-visible = "ANONYMOUS"
    }
  }
}
{
  "endpoints": {
    "health": {
      "details-visible": "ANONYMOUS"
    }
  }
}

向匿名未經(jīng)身份驗(yàn)證的用戶公開有關(guān)應(yīng)用程序健康狀態(tài)的各種健康指標(biāo)的詳細(xì)信息。

endpoints.health.status.http-mapping 設(shè)置控制為每個(gè)健康狀態(tài)返回哪些狀態(tài)代碼。下表描述了默認(rèn)值:

狀態(tài) HTTP Code

UP

OK (200)

UNKNOWN

OK (200)

DOWN

SERVICE_UNAVAILABLE (503)

您可以在配置文件中提供自定義映射(例如 application.yml):

Custom Health Status Codes

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.health.status.http-mapping.DOWN=200
endpoints:
  health:
    status:
      http-mapping:
        DOWN: 200
[endpoints]
  [endpoints.health]
    [endpoints.health.status]
      [endpoints.health.status.http-mapping]
        DOWN=200
endpoints {
  health {
    status {
      httpMapping {
        DOWN = 200
      }
    }
  }
}
{
  endpoints {
    health {
      status {
        http-mapping {
          DOWN = 200
        }
      }
    }
  }
}
{
  "endpoints": {
    "health": {
      "status": {
        "http-mapping": {
          "DOWN": 200
        }
      }
    }
  }
}

即使 HealthStatus 為 DOWN,上面的代碼也會(huì)返回 OK (200)。

自定義

健康端點(diǎn)由健康聚合器和任意數(shù)量的健康指標(biāo)組成。要添加健康指示器,請(qǐng)創(chuàng)建一個(gè)實(shí)現(xiàn) HealthIndicator 的 bean 類。建議同時(shí)使用 @Liveness 或 @Readiness 限定符。如果不使用限定符,則健康指標(biāo)將成為 /health 和 /health/readiness 端點(diǎn)的一部分?;?nbsp;AbstractHealthIndicator 可用于子類化以簡(jiǎn)化該過程。

所有健康指示器 bean 都與健康聚合器一起收集。要提供您自己的健康聚合器實(shí)現(xiàn),請(qǐng)創(chuàng)建一個(gè)實(shí)現(xiàn) HealthAggregator 的類并將其注冊(cè)為 bean。為確保使用您的實(shí)現(xiàn)而不是默認(rèn)實(shí)現(xiàn),請(qǐng)將 @Replaces 注釋添加到您的類中,并將值為默認(rèn)實(shí)現(xiàn) DefaultHealthAggregator。

默認(rèn)運(yùn)行狀況聚合器返回根據(jù)指標(biāo)的運(yùn)行狀況計(jì)算的總體狀態(tài)。健康狀況由幾條信息組成。

Name

狀態(tài)名稱

Description

狀態(tài)說明

Operational

指標(biāo)所代表的功能是否正常

Severity

狀態(tài)有多嚴(yán)重。數(shù)字越大越嚴(yán)重

“最差”狀態(tài)作為整體狀態(tài)返回。選擇非運(yùn)行狀態(tài)而不是運(yùn)行狀態(tài)。選擇較高的嚴(yán)重性而不是較低的嚴(yán)重性。

提供的指標(biāo)

Micronaut 提供的所有健康指標(biāo)都暴露在 /health 和 /health/readiness 端點(diǎn)上。

磁盤空間

提供了一個(gè)健康指標(biāo),它根據(jù)可用磁盤空間量確定應(yīng)用程序的健康狀況。可以在 endpoints.health.disk-space 鍵下提供磁盤空間健康指示器的配置。

Disk Space Indicator Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.health.disk-space.enabled=Boolean
endpoints.health.disk-space.path=String
endpoints.health.disk-space.threshold=String | Long
endpoints:
  health:
    disk-space:
      enabled: Boolean
      path: String
      threshold: String | Long
[endpoints]
  [endpoints.health]
    [endpoints.health.disk-space]
      enabled="Boolean"
      path="String"
      threshold="String | Long"
endpoints {
  health {
    diskSpace {
      enabled = "Boolean"
      path = "String"
      threshold = "String | Long"
    }
  }
}
{
  endpoints {
    health {
      disk-space {
        enabled = "Boolean"
        path = "String"
        threshold = "String | Long"
      }
    }
  }
}
{
  "endpoints": {
    "health": {
      "disk-space": {
        "enabled": "Boolean",
        "path": "String",
        "threshold": "String | Long"
      }
    }
  }
}
  • path 指定用于確定磁盤空間的路徑

  • 閾值指定最小可用空間量

閾值可以作為字符串提供,例如“10MB”或“200KB”,或字節(jié)數(shù)。

JDBC

JDBC 健康指標(biāo)根據(jù)在應(yīng)用程序上下文中成功創(chuàng)建到數(shù)據(jù)源的連接的能力來確定應(yīng)用程序的健康狀況。唯一支持的配置選項(xiàng)是通過 endpoints.health.jdbc.enabled 鍵啟用或禁用指示器。

Discovery Client

如果您的應(yīng)用程序使用服務(wù)發(fā)現(xiàn),則會(huì)包含一個(gè)健康指示器來監(jiān)控發(fā)現(xiàn)客戶端的健康狀況。返回的數(shù)據(jù)可以包括可用服務(wù)的列表。

指標(biāo)端點(diǎn)

Micronaut 可以通過與 Micrometer 集成來公開應(yīng)用程序指標(biāo)。

使用 CLI

如果您使用 Micronaut CLI 創(chuàng)建您的項(xiàng)目,請(qǐng)?zhí)峁┢渲幸粋€(gè)千分尺功能以啟用指標(biāo)并在您的項(xiàng)目中預(yù)配置選定的注冊(cè)表。例如:

$ mn create-app my-app --features micrometer-atlas

指標(biāo)端點(diǎn)返回有關(guān)應(yīng)用程序“指標(biāo)”的信息。要執(zhí)行指標(biāo)端點(diǎn),請(qǐng)向 /metrics 發(fā)送 GET 請(qǐng)求。這將返回可用指標(biāo)名稱的列表。

您可以使用 /metrics/[name] 獲取特定指標(biāo),例如 /metrics/jvm.memory.used。

刷新端點(diǎn)

刷新端點(diǎn)刷新應(yīng)用程序狀態(tài),導(dǎo)致上下文中的所有 Refreshable bean 被銷毀并在進(jìn)一步請(qǐng)求時(shí)重新實(shí)例化。這是通過在 Application Context 中發(fā)布 RefreshEvent 來完成的。

要執(zhí)行刷新端點(diǎn),請(qǐng)將 POST 請(qǐng)求發(fā)送到 /refresh。

$ curl -X POST http://localhost:8080/refresh

在沒有正文的情況下執(zhí)行時(shí),端點(diǎn)首先刷新環(huán)境并執(zhí)行差異以檢測(cè)任何更改,然后僅在檢測(cè)到更改時(shí)才執(zhí)行刷新。要跳過此檢查并刷新所有 @Refreshable bean 而不管環(huán)境更改(例如,強(qiáng)制刷新來自第三方服務(wù)的緩存響應(yīng)),請(qǐng)?jiān)?nbsp;POST 請(qǐng)求正文中添加一個(gè) force 參數(shù)。

$ curl -X POST http://localhost:8080/refresh -H 'Content-Type: application/json' -d '{"force": true}'

配置

要配置刷新端點(diǎn),請(qǐng)通過 endpoints.refresh 提供配置。

Beans Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.refresh.enabled=Boolean
endpoints.refresh.sensitive=Boolean
endpoints:
  refresh:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.refresh]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  refresh {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    refresh {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "refresh": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

路由端點(diǎn)

routes 端點(diǎn)返回有關(guān)可供您的應(yīng)用程序調(diào)用的 URI 的信息。默認(rèn)情況下,返回的數(shù)據(jù)包括 URI、允許的方法、生成的內(nèi)容類型以及有關(guān)將要執(zhí)行的方法的信息。

要執(zhí)行路由端點(diǎn),請(qǐng)向 /routes 發(fā)送 GET 請(qǐng)求。

配置

要配置路由端點(diǎn),請(qǐng)通過 endpoints.routes 提供配置。

Routes Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.routes.enabled=Boolean
endpoints.routes.sensitive=Boolean
endpoints:
  routes:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.routes]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  routes {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    routes {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "routes": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

自定義

路由端點(diǎn)由路由數(shù)據(jù)收集器和路由數(shù)據(jù)實(shí)現(xiàn)組成。路由數(shù)據(jù)收集器(RouteDataCollector)負(fù)責(zé)返回一個(gè)發(fā)布者,該發(fā)布者返回響應(yīng)中使用的數(shù)據(jù)。路線數(shù)據(jù)(RouteData)負(fù)責(zé)返回有關(guān)單個(gè)路線的數(shù)據(jù)。

要覆蓋任一幫助器類的默認(rèn)行為,請(qǐng)擴(kuò)展默認(rèn)實(shí)現(xiàn)(DefaultRouteDataCollector、DefaultRouteData),或直接實(shí)現(xiàn)相關(guān)接口。為確保使用您的實(shí)現(xiàn)而不是默認(rèn)實(shí)現(xiàn),請(qǐng)將 @Replaces 注釋添加到您的類中,并將值為默認(rèn)實(shí)現(xiàn)。

記錄器端點(diǎn)

記錄器端點(diǎn)返回有關(guān)應(yīng)用程序中可用記錄器的信息,并允許配置它們的日志級(jí)別。

默認(rèn)情況下禁用記錄器端點(diǎn),必須使用設(shè)置 endpoints.loggers.enabled=true 顯式啟用。

要按名稱及其配置的有效日志級(jí)別獲取所有記錄器的集合,請(qǐng)向 /loggers 發(fā)送 GET 請(qǐng)求。這也提供了可用日志級(jí)別的列表。

$ curl http://localhost:8080/loggers

{
    "levels": [
        "ALL", "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF", "NOT_SPECIFIED"
    ],
    "loggers": {
        "ROOT": {
            "configuredLevel": "INFO",
            "effectiveLevel": "INFO"
        },
        "io": {
            "configuredLevel": "NOT_SPECIFIED",
            "effectiveLevel": "INFO"
        },
        "io.micronaut": {
            "configuredLevel": "NOT_SPECIFIED",
            "effectiveLevel": "INFO"
        },
        // etc...
    }
}

要獲取特定記錄器的日志級(jí)別,請(qǐng)?jiān)?nbsp;GET 請(qǐng)求中包含記錄器名稱。例如,要訪問記錄器“io.micronaut.http”的日志級(jí)別:

$ curl http://localhost:8080/loggers/io.micronaut.http

{
    "configuredLevel": "NOT_SPECIFIED",
    "effectiveLevel": "INFO"
}

如果指定的記錄器不存在,則會(huì)使用未指定(即 NOT_SPECIFIED)配置的日志級(jí)別創(chuàng)建它(其有效日志級(jí)別通常是根記錄器的級(jí)別)。

要更新單個(gè)記錄器的日志級(jí)別,請(qǐng)向指定的記錄器 URL 發(fā)送 POST 請(qǐng)求,并包含一個(gè)提供要配置的日志級(jí)別的正文。

$ curl -i -X POST \
       -H "Content-Type: application/json" \
       -d '{ "configuredLevel": "ERROR" }' \
       http://localhost:8080/loggers/ROOT

HTTP/1.1 200 OK

$ curl http://localhost:8080/loggers/ROOT

{
    "configuredLevel": "ERROR",
    "effectiveLevel": "ERROR"
}

配置

要配置記錄器端點(diǎn),請(qǐng)通過 endpoints.loggers 提供配置。

Loggers Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.loggers.enabled=Boolean
endpoints.loggers.sensitive=Boolean
endpoints:
  loggers:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.loggers]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  loggers {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    loggers {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "loggers": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

默認(rèn)情況下,端點(diǎn)不允許未經(jīng)授權(quán)的用戶更改日志級(jí)別(即使 sensitive 設(shè)置為 false)。要允許這樣做,您必須將 endpoints.loggers.write-sensitive 設(shè)置為 false。

自定義

記錄器端點(diǎn)由兩個(gè)可自定義的部分組成:LoggersManager 和 LoggingSystem。有關(guān)自定義日志系統(tǒng)的信息,請(qǐng)參閱文檔的日志記錄部分。

LoggersManager 負(fù)責(zé)檢索和設(shè)置日志級(jí)別。如果默認(rèn)實(shí)現(xiàn)不足以滿足您的用例,只需提供您自己的實(shí)現(xiàn)并將 DefaultLoggersManager 替換為 @Replaces 注釋。

緩存端點(diǎn)

緩存端點(diǎn)文檔可在 micronaut-cache 項(xiàng)目中獲得。

服務(wù)器停止端點(diǎn)

停止端點(diǎn)關(guān)閉應(yīng)用程序服務(wù)器。

要執(zhí)行停止端點(diǎn),請(qǐng)向 /stop 發(fā)送 POST 請(qǐng)求。

配置

要配置停止端點(diǎn),請(qǐng)通過 endpoints.stop 提供配置。

Stop Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.stop.enabled=Boolean
endpoints.stop.sensitive=Boolean
endpoints:
  stop:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.stop]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  stop {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    stop {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "stop": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

默認(rèn)情況下,停止端點(diǎn)被禁用,必須明確啟用才能使用。

環(huán)境端點(diǎn)

環(huán)境端點(diǎn)返回有關(guān)環(huán)境及其 PropertySources 的信息。

配置

要啟用和配置環(huán)境端點(diǎn),請(qǐng)通過 endpoints.env 提供配置。

Environment Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.env.enabled=Boolean
endpoints.env.sensitive=Boolean
endpoints:
  env:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.env]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  env {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    env {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "env": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}
  • 啟用默認(rèn)為 false,敏感默認(rèn)為 true

默認(rèn)情況下,端點(diǎn)將屏蔽所有值。要自定義此屏蔽,您需要提供一個(gè)實(shí)現(xiàn) EnvironmentEndpointFilter 的 Bean。

第一個(gè)示例將屏蔽所有值,但前綴為 safe 的值除外

First example of environment masking

@Singleton
public class OnlySafePrefixedEnvFilter implements EnvironmentEndpointFilter {
    private static final Pattern SAFE_PREFIX_PATTERN = Pattern.compile("safe.*", Pattern.CASE_INSENSITIVE);

    @Override
    public void specifyFiltering(@NotNull EnvironmentFilterSpecification specification) {
        specification
                .maskAll() // All values will be masked apart from the supplied patterns
                .exclude(SAFE_PREFIX_PATTERN);
    }
}

也可以使用 maskNone-- 允許純文本中的所有值,然后指定將被屏蔽的名稱模式,即:

Deny instead of allow

@Singleton
public class AllPlainExceptSecretOrMatchEnvFilter implements EnvironmentEndpointFilter {
    // Mask anything starting with `sekrt`
    private static final Pattern SECRET_PREFIX_PATTERN = Pattern.compile("sekrt.*", Pattern.CASE_INSENSITIVE);

    // Mask anything exactly matching `exact-match`
    private static final String EXACT_MATCH = "exact-match";

    // Mask anything that starts with `private.`
    private static final Predicate<String> PREDICATE_MATCH = name -> name.startsWith("private.");

    @Override
    public void specifyFiltering(@NotNull EnvironmentFilterSpecification specification) {
        specification
                .maskNone() // All values will be in plain-text apart from the supplied patterns
                .exclude(SECRET_PREFIX_PATTERN)
                .exclude(EXACT_MATCH)
                .exclude(PREDICATE_MATCH);
    }
}

可以通過調(diào)用 legacyMasking-- 方法來應(yīng)用合理的默認(rèn)值。這將顯示除名稱中任何位置包含密碼、憑據(jù)、證書、密鑰、秘密或令牌的單詞之外的所有值。

獲取有關(guān)環(huán)境的信息

要執(zhí)行端點(diǎn),請(qǐng)向 /env 發(fā)送 GET 請(qǐng)求。

獲取有關(guān)特定 PropertySource 的信息

要執(zhí)行端點(diǎn),請(qǐng)向 /env/{propertySourceName} 發(fā)送 GET 請(qǐng)求。

ThreadDump 端點(diǎn)

threaddump 端點(diǎn)返回有關(guān)應(yīng)用程序中運(yùn)行的線程的信息。

要執(zhí)行 threaddump 端點(diǎn),請(qǐng)向 /threaddump 發(fā)送 GET 請(qǐng)求。

配置

要配置 threaddump 端點(diǎn),請(qǐng)通過 endpoints.threaddump 提供配置。

Threaddump Endpoint Configuration Example

 Properties Yaml  Toml  Groovy  Hocon  JSON 
endpoints.threaddump.enabled=Boolean
endpoints.threaddump.sensitive=Boolean
endpoints:
  threaddump:
    enabled: Boolean
    sensitive: Boolean
[endpoints]
  [endpoints.threaddump]
    enabled="Boolean"
    sensitive="Boolean"
endpoints {
  threaddump {
    enabled = "Boolean"
    sensitive = "Boolean"
  }
}
{
  endpoints {
    threaddump {
      enabled = "Boolean"
      sensitive = "Boolean"
    }
  }
}
{
  "endpoints": {
    "threaddump": {
      "enabled": "Boolean",
      "sensitive": "Boolean"
    }
  }
}

自定義

線程轉(zhuǎn)儲(chǔ)端點(diǎn)委托給 ThreadInfoMapper),它負(fù)責(zé)將 java.lang.management.ThreadInfo 對(duì)象轉(zhuǎn)換為任何其他要發(fā)送以進(jìn)行序列化的對(duì)象。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)