8.2项目监控

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,"获取连接失败!");
        }
    }
}

在这里插入图片描述
记得端点的路径还要做权限管理,不然很危险
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值