spring监视器actuator

引入依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
    <version>3.1.0version>
dependency>

配置

# actuator
# 将所有路径都加入监视
management.endpoints.web.exposure.include=*
# 将某几个路径取消监控
management.endpoints.web.exposure.exclude=info,caches

应用

自带的路径

localhost:8080/mycommunity/actuator/health

spring监视器actuator_第1张图片

自定义路径

@Component
@Endpoint(id = "database")
public class DatabaseEndpoint {

    private static final Logger logger = LoggerFactory.getLogger(DatabaseEndpoint.class);

    @Autowired
    private DataSource dataSource;

    // readOperation means this method can only be accessed by get request
    // writeOperation means this method can only be accessed by post request
    @ReadOperation
    public String checkConnection(){
        try(
                Connection connection = dataSource.getConnection();
           ) {
            return CommunityUtil.getJSONString(0,"success");
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error("failed");
        }
        return CommunityUtil.getJSONString(1, "failed");
    }
}

访问浏览器:localhost:8080/mycommunity/actuator/database
spring监视器actuator_第2张图片
注意actuator路径只能对管理员访问,注意做权限管理

你可能感兴趣的:(java杂文,spring,java,后端)