除了內(nèi)置端點(diǎn)之外,管理依賴(lài)項(xiàng)還提供對(duì)創(chuàng)建自定義端點(diǎn)的支持。這些可以像內(nèi)置端點(diǎn)一樣啟用和配置,并可用于檢索和返回任何指標(biāo)或其他應(yīng)用程序數(shù)據(jù)。
端點(diǎn)注解
可以通過(guò)使用端點(diǎn)注釋對(duì)類(lèi)進(jìn)行注釋并為其提供(至少)端點(diǎn) ID 來(lái)創(chuàng)建端點(diǎn)。
FooEndpoint.java
@Endpoint("foo")
class FooEndpoint {
...
}
如果向注解提供了單個(gè)字符串參數(shù),則將其用作端點(diǎn) ID。
可以為注釋提供額外的(命名的)參數(shù)。 @Endpoint 的其他可能參數(shù)如下表所述:
表 1. 端點(diǎn)參數(shù)
參數(shù) |
描述 |
端點(diǎn)示例 |
String id
|
端點(diǎn) ID(或名稱(chēng))
|
@Endpoint(id = "foo")
|
String prefix
|
用于配置端點(diǎn)的前綴
|
@Endpoint(prefix = "foo")
|
boolean defaultEnabled
|
設(shè)置在未設(shè)置配置時(shí)是否啟用端點(diǎn)
|
@Endpoint(defaultEnabled = false)
|
boolean defaultSensitive
|
如果未設(shè)置配置,則設(shè)置端點(diǎn)是否敏感
|
@Endpoint(defaultSensitive = false)
|
自定義端點(diǎn)示例
以下示例端點(diǎn)類(lèi)創(chuàng)建一個(gè)可在 /date 訪問(wèn)的端點(diǎn):
CurrentDateEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.management.endpoint.annotation.Endpoint;
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
public class CurrentDateEndpoint {
//.. endpoint methods
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
class CurrentDateEndpoint {
//.. endpoint methods
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
@Endpoint(id = "date", prefix = "custom", defaultEnabled = true, defaultSensitive = false)
class CurrentDateEndpoint {
//.. endpoint methods
}
|
端點(diǎn)方法
端點(diǎn)響應(yīng) GET(“讀取”)、POST(“寫(xiě)入”)和 DELETE(“刪除”)請(qǐng)求。要從端點(diǎn)返回響應(yīng),請(qǐng)使用以下注釋之一對(duì)其公共方法進(jìn)行注釋?zhuān)?/p>
表 1. 端點(diǎn)方法注解
注解 |
描述 |
Read
|
響應(yīng) GET 請(qǐng)求
|
Write
|
響應(yīng) POST 請(qǐng)求
|
Delete
|
響應(yīng) DELETE 請(qǐng)求
|
讀取方法
使用 Read 注釋注釋方法會(huì)導(dǎo)致它響應(yīng) GET 請(qǐng)求。
CurrentDateEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Read;
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
public class CurrentDateEndpoint {
private Date currentDate;
@Read
public Date currentDate() {
return currentDate;
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
class CurrentDateEndpoint {
private Date currentDate
@Read
Date currentDate() {
currentDate
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
@Endpoint(id = "date", prefix = "custom", defaultEnabled = true, defaultSensitive = false)
class CurrentDateEndpoint {
private var currentDate: Date? = null
@Read
fun currentDate(): Date? {
return currentDate
}
}
|
上述方法響應(yīng)以下請(qǐng)求:
$ curl -X GET localhost:55838/date
1526085903689
Read 注釋接受一個(gè)可選的 produces 參數(shù),它設(shè)置從方法返回的媒體類(lèi)型(默認(rèn)為 application/json):
CurrentDateEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Read;
import io.micronaut.http.MediaType;
import io.micronaut.management.endpoint.annotation.Selector;
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
public class CurrentDateEndpoint {
private Date currentDate;
@Read(produces = MediaType.TEXT_PLAIN) //(1)
public String currentDatePrefix(@Selector String prefix) {
return prefix + ": " + currentDate;
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Selector
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
class CurrentDateEndpoint {
private Date currentDate
@Read(produces = MediaType.TEXT_PLAIN) //(1)
String currentDatePrefix(@Selector String prefix) {
"$prefix: $currentDate"
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Selector
@Endpoint(id = "date", prefix = "custom", defaultEnabled = true, defaultSensitive = false)
class CurrentDateEndpoint {
private var currentDate: Date? = null
@Read(produces = [MediaType.TEXT_PLAIN]) //(1)
fun currentDatePrefix(@Selector prefix: String): String {
return "$prefix: $currentDate"
}
}
|
支持的媒體類(lèi)型由 MediaType 表示
上述方法響應(yīng)以下請(qǐng)求:
$ curl -X GET localhost:8080/date/the_date_is
the_date_is: Fri May 11 19:24:21 CDT
寫(xiě)方法
使用 Write 注釋對(duì)方法進(jìn)行注釋會(huì)導(dǎo)致它響應(yīng) POST 請(qǐng)求。
CurrentDateEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Write;
import io.micronaut.http.MediaType;
import io.micronaut.management.endpoint.annotation.Selector;
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
public class CurrentDateEndpoint {
private Date currentDate;
@Write
public String reset() {
currentDate = new Date();
return "Current date reset";
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Write
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Selector
@Endpoint(id = "date",
prefix = "custom",
defaultEnabled = true,
defaultSensitive = false)
class CurrentDateEndpoint {
private Date currentDate
@Write
String reset() {
currentDate = new Date()
return "Current date reset"
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Write
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Selector
@Endpoint(id = "date", prefix = "custom", defaultEnabled = true, defaultSensitive = false)
class CurrentDateEndpoint {
private var currentDate: Date? = null
@Write
fun reset(): String {
currentDate = Date()
return "Current date reset"
}
}
|
上述方法響應(yīng)以下請(qǐng)求:
$ curl -X POST http://localhost:39357/date
Current date reset
Write 注釋接受一個(gè)可選的 consumes 參數(shù),它設(shè)置該方法接受的媒體類(lèi)型(默認(rèn)為 application/json):
MessageEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.context.annotation.Requires;
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Write;
import io.micronaut.http.MediaType;
@Endpoint(id = "message", defaultSensitive = false)
public class MessageEndpoint {
String message;
@Write(consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.TEXT_PLAIN)
public String updateMessage(String newMessage) {
this.message = newMessage;
return "Message updated";
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Write
import io.micronaut.http.MediaType
@Endpoint(id = "message", defaultSensitive = false)
class MessageEndpoint {
String message
@Write(consumes = MediaType.APPLICATION_FORM_URLENCODED, produces = MediaType.TEXT_PLAIN)
String updateMessage(String newMessage) { //(1)
message = newMessage
return "Message updated"
}
}
|
import io.micronaut.context.annotation.Requires
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Write
import io.micronaut.http.MediaType
@Endpoint(id = "message", defaultSensitive = false)
class MessageEndpoint {
internal var message: String? = null
@Write(consumes = [MediaType.APPLICATION_FORM_URLENCODED], produces = [MediaType.TEXT_PLAIN])
fun updateMessage(newMessage: String): String { //(1)
this.message = newMessage
return "Message updated"
}
}
|
上述方法響應(yīng)以下請(qǐng)求:
$ curl -X POST http://localhost:65013/message -H 'Content-Type: application/x-www-form-urlencoded' -d newMessage=A new message'
Message updated
刪除方法
使用 Delete 注釋對(duì)方法進(jìn)行注釋會(huì)導(dǎo)致它響應(yīng) DELETE 請(qǐng)求。
MessageEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.context.annotation.Requires;
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Delete;
@Endpoint(id = "message", defaultSensitive = false)
public class MessageEndpoint {
String message;
@Delete
public String deleteMessage() {
this.message = null;
return "Message deleted";
}
}
|
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Delete
@Endpoint(id = "message", defaultSensitive = false)
class MessageEndpoint {
String message
@Delete
String deleteMessage() {
message = null
return "Message deleted"
}
}
|
import io.micronaut.context.annotation.Requires
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Delete
@Endpoint(id = "message", defaultSensitive = false)
class MessageEndpoint {
internal var message: String? = null
@Delete
fun deleteMessage(): String {
this.message = null
return "Message deleted"
}
}
|
上述方法響應(yīng)以下請(qǐng)求:
$ curl -X DELETE http://localhost:65013/message
Message deleted
端點(diǎn)敏感度
可以通過(guò)端點(diǎn)注釋和配置來(lái)控制整個(gè)端點(diǎn)的端點(diǎn)敏感性。但是,各個(gè)方法可以獨(dú)立于端點(diǎn)作為一個(gè)整體進(jìn)行配置。 @Sensitive 注釋可以應(yīng)用于方法以控制它們的敏感性。
AlertsEndpoint
Java |
Groovy |
Kotlin |
import io.micronaut.http.MediaType;
import io.micronaut.management.endpoint.annotation.Delete;
import io.micronaut.management.endpoint.annotation.Endpoint;
import io.micronaut.management.endpoint.annotation.Read;
import io.micronaut.management.endpoint.annotation.Sensitive;
import io.micronaut.management.endpoint.annotation.Write;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@Endpoint(id = "alerts", defaultSensitive = false) // (1)
public class AlertsEndpoint {
private final List<String> alerts = new CopyOnWriteArrayList<>();
@Read
List<String> getAlerts() {
return alerts;
}
@Delete
@Sensitive(true) // (2)
void clearAlerts() {
alerts.clear();
}
@Write(consumes = MediaType.TEXT_PLAIN)
@Sensitive(property = "add.sensitive", defaultValue = true) // (3)
void addAlert(String alert) {
alerts.add(alert);
}
}
|
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Delete
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
import io.micronaut.management.endpoint.annotation.Sensitive
import io.micronaut.management.endpoint.annotation.Write
import java.util.concurrent.CopyOnWriteArrayList
@Endpoint(id = "alerts", defaultSensitive = false) // (1)
class AlertsEndpoint {
private final List<String> alerts = new CopyOnWriteArrayList<>();
@Read
List<String> getAlerts() {
alerts
}
@Delete
@Sensitive(true) // (2)
void clearAlerts() {
alerts.clear()
}
@Write(consumes = MediaType.TEXT_PLAIN)
@Sensitive(property = "add.sensitive", defaultValue = true) // (3)
void addAlert(String alert) {
alerts << alert
}
}
|
import io.micronaut.http.MediaType
import io.micronaut.management.endpoint.annotation.Delete
import io.micronaut.management.endpoint.annotation.Endpoint
import io.micronaut.management.endpoint.annotation.Read
import io.micronaut.management.endpoint.annotation.Sensitive
import io.micronaut.management.endpoint.annotation.Write
import java.util.concurrent.CopyOnWriteArrayList
@Endpoint(id = "alerts", defaultSensitive = false) // (1)
class AlertsEndpoint {
private val alerts: MutableList<String> = CopyOnWriteArrayList()
@Read
fun getAlerts(): List<String> {
return alerts
}
@Delete
@Sensitive(true) // (2)
fun clearAlerts() {
alerts.clear()
}
@Write(consumes = [MediaType.TEXT_PLAIN])
@Sensitive(property = "add.sensitive", defaultValue = true) // (3)
fun addAlert(alert: String) {
alerts.add(alert)
}
}
|
端點(diǎn)默認(rèn)不敏感,使用端點(diǎn)的默認(rèn)前綴。
無(wú)論任何其他因素如何,此方法始終是敏感的
屬性值附加到前綴和 id 以查找配置值
如果設(shè)置了配置鍵 endpoints.alerts.add.sensitive,則該值決定了 addAlert 方法的敏感度。
端點(diǎn)是第一個(gè)標(biāo)記,因?yàn)樗嵌它c(diǎn)注釋中前綴的默認(rèn)值,并且未在此示例中明確設(shè)置。
alerts 是下一個(gè)標(biāo)記,因?yàn)槟鞘嵌它c(diǎn) ID
add.sensitive 是下一個(gè)標(biāo)記,因?yàn)樗菫?nbsp;@Sensitive 注釋的屬性成員設(shè)置的值。
如果未設(shè)置配置鍵,則使用 defaultValue(默認(rèn)為 true)。
端點(diǎn)配置
具有 endpoints 前綴的端點(diǎn)可以通過(guò)其默認(rèn)端點(diǎn) ID 進(jìn)行配置。如果存在 ID 為 foo 的端點(diǎn),則可以通過(guò) endpoints.foo 對(duì)其進(jìn)行配置。此外,可以通過(guò) all 前綴提供默認(rèn)值。
例如,考慮以下端點(diǎn)。
FooEndpoint.java
@Endpoint("foo")
class FooEndpoint {
...
}
默認(rèn)情況下,端點(diǎn)已啟用。要禁用它,請(qǐng)將 endpoints.foo.enabled 設(shè)置為 false。如果未設(shè)置 endpoints.foo.enabled 且 endpoints.all.enabled 為 false,則端點(diǎn)將被禁用。
端點(diǎn)的配置值覆蓋所有端點(diǎn)的配置值。如果 endpoints.foo.enabled 為 true 而 endpoints.all.enabled 為 false,端點(diǎn)將被啟用。
對(duì)于所有端點(diǎn),可以設(shè)置以下配置值。
Properties |
Yaml |
Toml |
Groovy |
Hocon |
JSON |
endpoints.<any endpoint id>.enabled=Boolean
endpoints.<any endpoint id>.sensitive=Boolean
|
endpoints:
<any endpoint id>:
enabled: Boolean
sensitive: Boolean
|
[endpoints]
sensitive="Boolean"
|
endpoints {
<any endpoint id> {
enabled = "Boolean"
sensitive = "Boolean"
}
}
|
{
endpoints {
<any endpoint id> {
enabled = "Boolean"
sensitive = "Boolean"
}
}
}
|
{
"endpoints": {
"<any endpoint id>": {
"enabled": "Boolean",
"sensitive": "Boolean"
}
}
}
|
默認(rèn)情況下,所有端點(diǎn)的基本路徑都是 /。如果您希望端點(diǎn)在不同的基本路徑下可用,請(qǐng)配置 endpoints.all.path。例如,如果該值設(shè)置為 /endpoints/,則可以在 /endpoints/foo 相對(duì)于上下文路徑訪問(wèn) foo 端點(diǎn)。請(qǐng)注意,除非設(shè)置了 micronaut.server.context-path,否則 endpoints.all.path 需要前導(dǎo)和尾隨 /,在這種情況下,前導(dǎo) / 不是必需的。
更多建議: