引入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>3.1.0</version>
</dependency>
配置
# actuator
# 将所有路径都加入监视
management.endpoints.web.exposure.include=*
# 将某几个路径取消监控
management.endpoints.web.exposure.exclude=info,caches
应用
自带的路径
localhost:8080/mycommunity/actuator/health
自定义路径
@Component
@Endpoint(id = "database")
public class DatabaseEndpoint {private static final Logger logger = LoggerFactory.getLogger(DatabaseEndpoint.class);@Autowiredprivate DataSource dataSource;// readOperation means this method can only be accessed by get request// writeOperation means this method can only be accessed by post request@ReadOperationpublic 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
注意actuator路径只能对管理员访问,注意做权限管理