8.2專案監控
阿新 • • 發佈:2021-01-20
技術標籤:SpringBoot社群專案
Spring Boot Actuator
(1)Endpoints:監控應用的入口,Spring Boot內建了很多端點,也支援自定義端點。
(2)監控方式:HTTP或JMX。
(3)訪問路徑:例如"/actuator/health"。
(4)注意事項:按需配置暴露的端點,並對所有端點進行許可權控制。
Spring Boot Actuator預設有20多個端點,幾乎所有端點都是啟用的,只有一個端點是禁用的(關閉伺服器的post請求,一個後門,別啟用了)。但只暴露了兩個端點,其他端點要訪問的話需要配置。
預設開啟的端點:
http://localhost:8080/community/actuator/health
其他的埠需要配置。
在application.properties裡新增配置
# actuator
#management.endpoints.web.exposure.include=beans,loggers
management.endpoints.web.exposure.include=*
#禁掉info,caches
management.endpoints.web.exposure.exclude=info,caches
自定義端點:
@Component
@Endpoint(id="database" )
public class DataBaseEndPoint {
private static final Logger logger = LoggerFactory.getLogger(DataBaseEndPoint.class);
//可通過連線池或連線引數(manager)獲取連線,這裡用連線池
@Autowired
private DataSource dataSource;
@ReadOperation //表示是用get請求來訪問
public String checkConnection(){
try (
// 小括號裡面的會自動關閉掉
Connection conn = dataSource.getConnection();
){
return CommunityUtil.getJSONString(0,"獲取連線成功!");
} catch (SQLException e) {
logger.error("獲取連線失敗:"+e.getMessage());
return CommunityUtil.getJSONString(1,"獲取連線失敗!");
}
}
}
記得端點的路徑還要做許可權管理,不然很危險