11.5 Endpoint监控接口

我们来尝试访问:http://127.0.0.1:8000/application/beans ,浏览器显示如下信息:

  1. Whitelabel Error Page
  2. This application has no explicit mapping for /error, so you are seeing this as a fallback.
  3. Mon Jul 17 21:30:35 CST 2017
  4. There was an unexpected error (type=Unauthorized, status=401).
  5. Full authentication is required to access this resource.

提示没有权限访问。我们去控制台日志可以看到下面这行输出:

  1. s.b.a.e.m.MvcEndpointSecurityInterceptor : Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.

意思是说,要访问这些Endpoints需要权限,可以通过Spring Security来实现权限控制,或者把权限限制去掉:把management.security.enabled设置为false

我们在application.properties里面添加配置:

  1. management.security.enabled=false

重启应用,再次访问,我们可以看到如下输出:

  1. [
  2. {
  3. "context": "application:8000",
  4. "parent": null,
  5. "beans": [
  6. {
  7. "bean": "chapter11KotlinSpringbootApplication",
  8. "aliases": [
  9. ],
  10. "scope": "singleton",
  11. "type": "com.easy.kotlin.chapter11_kotlin_springboot.Chapter11KotlinSpringbootApplication$$EnhancerBySpringCGLIB$$353fd63e",
  12. "resource": "null",
  13. "dependencies": [
  14. ]
  15. },
  16. ...
  17. {
  18. "bean": "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration",
  19. "aliases": [
  20. ],
  21. "scope": "singleton",
  22. "type": "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$24d31c25",
  23. "resource": "null",
  24. "dependencies": [
  25. "dataSource",
  26. "spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties"
  27. ]
  28. },
  29. {
  30. "bean": "transactionManager",
  31. "aliases": [
  32. ],
  33. "scope": "singleton",
  34. "type": "org.springframework.orm.jpa.JpaTransactionManager",
  35. "resource": "class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]",
  36. "dependencies": [
  37. ]
  38. },
  39. ...
  40. {
  41. "bean": "spring.devtools-org.springframework.boot.devtools.autoconfigure.DevToolsProperties",
  42. "aliases": [
  43. ],
  44. "scope": "singleton",
  45. "type": "org.springframework.boot.devtools.autoconfigure.DevToolsProperties",
  46. "resource": "null",
  47. "dependencies": [
  48. ]
  49. },
  50. {
  51. "bean": "org.springframework.orm.jpa.SharedEntityManagerCreator#0",
  52. "aliases": [
  53. ],
  54. "scope": "singleton",
  55. "type": "com.sun.proxy.$Proxy86",
  56. "resource": "null",
  57. "dependencies": [
  58. "entityManagerFactory"
  59. ]
  60. }
  61. ]
  62. }
  63. ]

可以看出,我们一行代码还没写,只是加了几行配置,SpringBoot已经自动配置初始化了这么多的Bean。我们再访问 http://127.0.0.1:8000/application/health

  1. {
  2. "status": "UP",
  3. "diskSpace": {
  4. "status": "UP",
  5. "total": 120108089344,
  6. "free": 1724157952,
  7. "threshold": 10485760
  8. },
  9. "db": {
  10. "status": "UP",
  11. "database": "MySQL",
  12. "hello": 1
  13. }
  14. }

从上面我们可以看到一些应用的健康状态信息,例如:应用状态、磁盘空间、数据库状态等信息。