Spring Boot Admin最佳实践

和自甴很熟 提交于 2019-12-09 23:58:58

本文不进行Spring Boot Admin入门知识点说明

Spring Boot Actuator中提供很多像healthmetrics等实时监控接口,可以方便我们随时跟踪服务的性能指标。Spring Boot默认是开放这些接口提供调用的,那么就问题来了,如果这些接口公开在外网中,很容易被不法分子所利用,这肯定不是我们想要的结果。在这里我们提供一种比较好的解决方案。

  • 被监控的服务配置

为被保护的http请求添加请求前缀

123456
management:  context-path: /example-context eureka:  instance:    status-page-url-path: ${management.context-path}/info     health-check-url-path: ${management.context-path}/health
  1. 添加请求前缀
  2. Spring Boot Admin在启动的时候会去eureka拉去服务信息,其中healthinfo需要特殊处理,这两者的地址是根据status-page-url-pathhealth-check-url-path的值。
  • zuul网关配置

zuul保护内部服务http接口

12
zuul:  ignoredPatterns: /*/example-context/** 
  1. 这里之所以不是/example-context/**,由于网关存在项目前缀,需要往前一级,大家可以具体场景具体配置
  • Spring Boot Admin配置

配置监控的指标参数

12345678910111213141516171819
spring:  application:    name: monitor  boot:    admin:      discovery:        converter:          management-context-path: /example-context       routes:        endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream      turbine:        clusters: default        location: monitorturbine:  aggregator:    clusterConfig: default  appConfig: monitor-example #<2>  clusterNameExpression: metadata['cluster']
  1. 与应用配置的management.context-path相同
  2. 添加需要被监控的应用Service-Id,以逗号分隔

讲解一下,通过创建一个请求前缀,可以在网关处使用前缀的方式将其排除,也就是外网将无法访问这些监控API,同时,内网还是可以进行加前缀的方式进行访问,为Spring Boot Admin提供了支持条件。management还支持port和ip的方式,但这两种方式有局限性,如果在同一台机器上部署多个服务,就会存在端口占用或者其他问题。这种方案还有一个好处,以上配置一旦确定以后,所有服务都不需要进行特殊化处理,可以直接使用。

问答:

  • 问题:Full authentication is required to access this resource

拓展阅读:

spring-boot-admin-samples

issue

jolokia

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!