SpringBoot 开启 Actuator
在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使用如下:
1、引入依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
如果使用http调用的方式,还需要这个依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
2、配置:
application.yml中指定监控的HTTP端口(如果不指定,则使用和server相同的端口):
management:
port: 54001
# close security. 关闭身份验证,否则无法查询出数据
security:
enabled: false
3、使用:
查看health指标:http://localhost:54001/health
- {"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4、自定义指标:
4.1 /health:在某个类中implements HealthIndicator接口,然后实现其中的health()方法即可:
代码:
- @SpringBootApplication
- @EnableScheduling
- public class MySpringBootApplication implements HealthIndicator{
- private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class);
- public static void main(String[] args) {
- SpringApplication.run(MySpringBootApplication.class, args);
- logger.info("My Spring Boot Application Started");
- }
- /**
- * 在/health接口调用的时候,返回多一个属性:"mySpringBootApplication":{"status":"UP","hello":"world"}
- */
- @Override
- public Health health() {
- return Health.up().withDetail("hello", "world").build();
- }
- }
/health 运行结果(注意第二个指标):
{"status":"UP","mySpringBootApplication":{"status":"UP","hello":"world"},"diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4.2 /info:配置如下,可以直接给一个字符串,也可以从pom.xml配置中获取
- info:
- app:
- name: "@project.name@" #从pom.xml中获取
- description: "@project.description@"
- version: "@project.version@"
- spring-boot-version: "@project.parent.version@"
/info的结果如下:
{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}
官网:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
源代码参考:https://github.com/xujijun/my-spring-boot
----------------------------
/health 只有status信息,没有其他
{"status" : "UP"}
/metrics 提示没有权限
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Mon Nov 20 10:42:15 CST 2017There was an unexpected error (type=Unauthorized, status=401).Full authentication is required to access this resource.
- 方式1-关闭验证
application.properties添加配置参数management.security.enabled=false
- 方式2-开启HTTP basic认证
- 添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
- application.properties 添加用户名和密码
security.user.name=adminsecurity.user.password=123456management.security.enabled=truemanagement.security.role=ADMIN
- 访问URL http://localhost:8080/env 后,就看到需要输入用户名和密码了。
- Actuator endpoints 【断点】:
Actuator endpoints allow you to monitor and interact with your application.Spring Boot includes a number of built-in endpoints and you can also add your own.For example the health endpoint provides basic application health information.Actuator 端点允许您监视和与您的应用程序进行交互。Spring Boot包含许多内置的端点,您也可以添加自己的端点。例如, health端点提供基本的应用程序健康信息。The way that endpoints are exposed will depend on the type of technology that you choose.Most applications choose HTTP monitoring, where the ID of the endpoint is mapped to a URL.For example, by default, the health endpoint will be mapped to /health.端点的暴露方式取决于您选择的技术类型。大多数应用程序选择HTTP监视,其中端点的ID映射到一个URL。例如,默认情况下,health端点将被映射到/health。
The following technology agnostic endpoints are available:
| ID | Description | Sensitive Default |
|---|---|---|
|
|
Provides a hypermedia-based “discovery page” for the other endpoints. Requires Spring HATEOAS to be on the classpath. 为其他端点提供基于超媒体的“发现页面”。要求Spring HATEOAS在类路径上。 |
true |
|
|
Exposes audit events information for the current application. 公开当前应用程序的审计事件信息。 |
true |
|
|
Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied. 显示一个auto-configuration的报告,该报告展示所有auto-configuration候选者及它们被应用或未被应用的原因 |
true |
|
|
Displays a complete list of all the Spring beans in your application. 显示一个应用中所有Spring Beans的完整列表 |
true |
|
|
Displays a collated list of all 显示一个所有@ConfigurationProperties的整理列表 |
true |
|
|
Performs a thread dump. 执行一个线程转储 |
true |
|
|
Exposes properties from Spring’s 暴露来自Spring ConfigurableEnvironment的属性 |
true |
|
|
Shows any Flyway database migrations that have been applied. 显示已应用的所有Flyway数据库迁移。 |
true |
|
|
Shows application health information (when the application is secure, a simple ‘status’ when accessed over an unauthenticated connection or full message details when authenticated). 显示应用程序运行状况信息(应用程序安全时,通过未经身份验证的连接访问时的简单'状态'或通过身份验证时的完整邮件详细信息)。 |
false |
|
|
Displays arbitrary application info. 显示任意的应用信息。 |
false |
|
|
Shows and modifies the configuration of loggers in the application. 显示和修改应用程序中的记录器配置。 |
true |
|
|
Shows any Liquibase database migrations that have been applied. 显示已经应用的任何Liquibase数据库迁移。 |
true |
|
|
Shows ‘metrics’ information for the current application. 显示当前应用程序的“指标”信息。 |
true |
|
|
Displays a collated list of all 显示所有 |
true |
|
|
Allows the application to be gracefully shutdown (not enabled by default). 允许应用程序正常关机(默认情况下不启用)。 |
true |
|
|
Displays trace information (by default the last 100 HTTP requests). 显示跟踪信息(默认最后100个HTTP请求)。 |
true |
- Accessing sensitive endpoints【访问敏感端点】
By default all sensitive HTTP endpoints are secured such that only users that have an ACTUATOR role may access them.
Security is enforced using the standard HttpServletRequest.isUserInRole method.
(默认情况下,所有敏感的HTTP端点都是安全的,只有具有ACTUATOR角色的用户 可以访问它们。
安全性是使用标准HttpServletRequest.isUserInRole方法强制执行的 。)
Use the management.security.roles property if you want something different to ACTUATOR.
If you are deploying applications behind a firewall, you may prefer that all your actuator endpoints can be accessed without requiring authentication.
You can do this by changing the management.security.enabled property:
application.properties.
management.security.enabled=false
By default, actuator endpoints are exposed on the same port that serves regular HTTP traffic.Take care not to accidentally expose sensitive informationif you change the management.security.enabled property.(默认情况下,执行器端点暴露在提供常规HTTP通信的相同端口上。注意不要在更改management.security.enabled属性时意外暴露敏感信息。)
If you’re deploying applications publicly, you may want to add ‘Spring Security’ to handle user authentication.
When ‘Spring Security’ is added, by default ‘basic’ authentication will be used with the username user and a generated password (which is printed on the console when the application starts).
(如果您公开部署应用程序,则可能需要添加“Spring Security”来处理用户身份验证。
当添加“Spring Security”时,默认情况下,“基本”身份验证将与用户名user和生成的密码一起使用(在应用程序启动时在控制台上打印)。)
Generated passwords are logged as the application starts. Search for ‘Using default security password’.生成的密码在应用程序启动时被记录。搜索“使用默认安全密码”。
You can use Spring properties to change the username and password and to change the security role(s) required to access the endpoints.
For example, you might set the following in your application.properties:
security.user.name=admin
security.user.password=secret
management.security.roles=SUPERUSER
If your application has custom security configuration and you want all your actuator endpoints to be accessible without authentication, you need to explicitly configure that in your security configuration. Along with that, you need to change the management.security.enabledproperty to false.
(如果您的应用程序具有自定义安全配置,并且您希望所有执行器端点无需身份验证即可访问,则需要在安全配置中明确配置该端点。与此同时,你需要改变management.security.enabled 属性false。)
If your custom security configuration secures your actuator endpoints, you also need to ensure that the authenticated user has the roles specified under management.security.roles.
(如果您的自定义安全配置保护您的执行器端点,则还需要确保经过身份验证的用户具有在下指定的角色management.security.roles。)
If you don’t have a use case for exposing basic health information to unauthenticated users,and you have secured the actuator endpoints with customsecurity, you can set management.security.enabled to false.This will inform Spring Boot to skip the additional role check.(如果您没有用于向未经验证的用户公开基本健康信息的用例,并且已经使用自定义安全保护了执行器端点,则可以设置management.security.enabled为false。这将通知Spring Boot跳过额外的角色检查。)
SpringBoot 开启 Actuator的更多相关文章
- SpringBoot系列: Actuator监控
Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- Spring boot 入门五:springboot 开启声明式事务
springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事务.这里以spring整合myb ...
- (转)SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
springboot开启事务很简单,只需要一个注解@Transactional 就可以了.因为在springboot中已经默认对jpa.jdbc.mybatis开启了事事务,引入它们依赖的时候,事物就 ...
- SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
转载请标明出处: http://blog.csdn.net/forezp/article/details/70833629 本文出自方志朋的博客 springboot开启事务很简单,只需要一个注解@T ...
- springBoot开启热部署
springBoot开启热部署 这里使用devtools工具开启热部署 〇.搭建springbboot基础环境 一.添加依赖 <dependency> <groupId>org ...
- SpringBoot集成actuator模块的基本使用
© 版权声明:本文为博主原创文章,转载请注明出处 1. 版本 SpringBoot:2.0.0.RELEASE 2. 集成 SpringBoot集成actuator模块非常简单,只需要引入actuat ...
- springboot开启定时任务 添加定时任务 推送
最近在自学Java的springboot框架,要用到定时推送消息.参考了网上的教程,自己调试,终于调好了.下面将网上的教程归纳下,总结复习下. springboot开启定时任务 在SpringBo ...
- SpringBoot集成Actuator监控管理
1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...
随机推荐
- Mac 下安装Ruby环境
步骤1 - 安装 RVM RVM 是干什么的这里就不解释了,后面你将会慢慢搞明确. $ curl -L https://get.rvm.io | bash -s stable 期间可能会问你sudo管 ...
- 浏览器中beforeunload的使用
打开一些慢的网站的时候只见浏览器在不停转圈,但是页面还停留在当前页面的,有些网站的效果是你点击链接要跳到另一个页面的时候,在当前页面弹出一个框提示“正在加载中....”, 用到了浏览器的beforeu ...
- 【Unity】4.1 创建组件
分类:Unity.C#.VS2015 创建日期:2016-04-05 一.简介 组件(Component)在Unity游戏开发工作中非常重要,可以说是实现一切功能所必需的. 1.游戏对象(Game O ...
- nginx、Apache、Lighttpd启用HSTS
302跳转 通常情况下,我们将用户的 HTTP 请求 302 跳转到 HTTPS,这会存在两个问题: 不够安全,302 跳转会暴露用户访问站点,也容易被劫持 拖慢访问速度,302 跳转需要一个 RTT ...
- impress.js 一个创建在线幻灯的js库
真的好奇怪,我居然会写前端技术的博客.没有办法的,最近实习,看的大多是前端.所以今天就用这个来练练手了. Impress.js 是一个非常棒的用来创建在线演示的Javascript库.它基于CSS3转 ...
- 使用tar命令解压的时候报错not in gzip format
使用tar命令解压一个xx.tar.gz压缩包的时候报错not in gzip format,后用file xx.tar.gz查看一下格式发现是html document text格式的...
- socket.io笔记三之子命名空间的socket连接
当客户端发送admin命名空间下的连接,如果主连接也监听了connetion事件,主连接的connection事件会先触发执行,然后紧接着触发执行admin命名空间下的connection事件.如果客 ...
- 解决windows10 里vs2015 附件进程调试提示“此任务要求应用程序有提升的权限”
刚用windows10 ,感觉有些地方别扭.就在是vs2015开发程序的时候,就遇到了个问题. 首先 我是使用adminitrator账号登陆的. 双击vs解决方案,打开iis,然后结合vs2015里 ...
- 微网站|h5弹窗|手机网站 html5 弹窗、弹层、提示框、加载条
为了精确体验,您可通过Chrome设备模式浏览.或通过[url=]手机扫二维码进入[/url] layer mobile是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层 ...
- Lua函数[转]
在大多数Lua语法分析中可以获得这些标准Lua函数. 无可争辩, 我们可以查阅Lua网站, 但是一些少了的函数被Blizzard进行了调整. 下面列出了所有Lua函数. WoW API中的Lua注意在 ...