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
http://localhost:8080/community/actuator/info
其他的端口需要配置。
在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,"获取连接失败!");
}
}
}
记得端点的路径还要做权限管理,不然很危险