目標(biāo)
項(xiàng)目搭建,整合actuator
開干
1.創(chuàng)建項(xiàng)目通過idea的Initializr快速創(chuàng)建
如圖,首先創(chuàng)建了整理工程pay-template-framework,隨后先創(chuàng)建了子moudle:udm-server期虾。
這是我們第一個(gè)項(xiàng)目模塊,后面業(yè)務(wù)模塊在介紹其作用驯嘱,下面先看整合actuator镶苞。
actuator是spring boot項(xiàng)目中非常強(qiáng)大的一個(gè)功能,有助于對(duì)應(yīng)用程序進(jìn)行監(jiān)控和管理鞠评,通過restful api請(qǐng)求來監(jiān)管茂蚓、審計(jì)、收集應(yīng)用的運(yùn)行情況谢澈。
uri | 說明 | 敏感信息需要開啟權(quán)限訪問 |
---|---|---|
auditevents | 顯示當(dāng)前應(yīng)用程序的審計(jì)事件信息 | Yes |
beans | 顯示應(yīng)用Spring Beans的完整列表 | Yes |
conditions | 顯示自動(dòng)裝配類的狀態(tài)及及應(yīng)用信息 | Yes |
configprops | 顯示所有 @ConfigurationProperties 列表 | Yes |
env | 顯示 ConfigurableEnvironment 中的屬性 | Yes |
flyway | 顯示 Flyway 數(shù)據(jù)庫(kù)遷移信息 | Yes |
health | 顯示應(yīng)用的健康信息(未認(rèn)證只顯示status煌贴,認(rèn)證顯示全部信息詳情) | no |
info | 顯示任意的應(yīng)用信息(在資源文件寫info.xxx即可) | no |
metrics | 展示當(dāng)前應(yīng)用的 metrics 信息 | Yes |
mappings | 顯示所有 @RequestMapping 路徑集列表 | Yes |
scheduledtasks | 顯示應(yīng)用程序中的計(jì)劃任務(wù) | Yes |
sessions | 允許從Spring會(huì)話支持的會(huì)話存儲(chǔ)中檢索和刪除用戶會(huì)話 | Yes |
shutdown | 允許應(yīng)用以優(yōu)雅的方式關(guān)閉(默認(rèn)情況下不啟用),通過endpoints.shutdown.enabled: true配置 | Yes |
dump | 顯示應(yīng)用dump | Yes |
httptrace | 顯示HTTP跟蹤信息(默認(rèn)顯示最后100個(gè)HTTP請(qǐng)求 - 響應(yīng)交換) | Yes |
下面看使用方法:
- 首先加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--spring-boot-starter-security 安全機(jī)制,對(duì)應(yīng)actuator敏感信息需進(jìn)行賬號(hào)密碼訪問 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- application.yml配置
server:
port: 8000
info:
app:
name: @project.artifactId@
head: head
body: body
endpoints:
shutdown:
enabled: true
id: shut
beans:
enabled: false
security:
basic:
enabled: true
user:
name: root
password: 12
spring:
Jackson:
serialization:
indent-output: true
如上配置锥忿,此時(shí)shutdown的uri將改為shut牛郑,并且此時(shí)開啟了賬號(hào)密碼訪問,如下
curl -d dd=dd http://root:12@localhost:8000/shut
頁面訪問可以直接輸入賬號(hào)密碼(也可以通過management: security: enabled: true配置關(guān)閉安全檢查敬鬓,不過生產(chǎn)不推薦).
- 自定義endpoints入口
package open.template.work.udm.udm.server.endpoint;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@ConfigurationProperties(prefix = "endpoints.systemTime")
public class MyEndPoint extends AbstractEndpoint<Map<String, Object>> {
public MyEndPoint() {
//參數(shù)=名字淹朋,是否敏感
super("systemTime", false);
}
@Override
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<String, Object>();
Date dateTime = new Date();
result.put("當(dāng)前時(shí)間", dateTime.toString());
result.put("當(dāng)前時(shí)間戳", dateTime.getTime());
return result;
}
}
注冊(cè)配置類
package open.template.work.udm.udm.server;
import open.template.work.udm.udm.server.endpoint.MyEndPoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
@SpringBootApplication
@Configuration
public class UdmServerApplication {
public static void main(String[] args) {
SpringApplication.run(UdmServerApplication.class, args);
}
@Bean
public static Endpoint<Map<String, Object>> servertime() {
return new MyEndPoint();
}
}
此時(shí)訪問配置節(jié)點(diǎn):