本文不进行Spring Boot Admin入门知识点说明
在Spring Boot Actuator
中提供很多像health
、metrics
等实时监控接口,可以方便我们随时跟踪服务的性能指标。Spring Boot
默认是开放这些接口提供调用的,那么就问题来了,如果这些接口公开在外网中,很容易被不法分子所利用,这肯定不是我们想要的结果。在这里我们提供一种比较好的解决方案。
为被保护的http请求添加请求前缀
management:
context-path: /example-context #<1>
eureka:
instance:
status-page-url-path: ${management.context-path}/info #<2>
health-check-url-path: ${management.context-path}/health
Spring Boot Admin
在启动的时候会去eureka
拉去服务信息,其中health
与info
需要特殊处理,这两者的地址是根据status-page-url-path
和health-check-url-path
的值。zuul
网关配置zuul
保护内部服务http接口
zuul:
ignoredPatterns: /*/example-context/** #<1>
/example-context/**
,由于网关存在项目前缀,需要往前一级,大家可以具体场景具体配置Spring Boot Admin
配置配置监控的指标参数
spring:
application:
name: monitor
boot:
admin:
discovery:
converter:
management-context-path: /example-context # The endpoints URL prefix #<1>
routes:
endpoints: env,metrics,dump,jolokia,info,configprops,trace,logfile,refresh,flyway,liquibase,heapdump,loggers,auditevents,hystrix.stream
turbine:
clusters: default
location: monitor
turbine:
aggregator:
clusterConfig: default
appConfig: monitor-example #<2>
clusterNameExpression: metadata['cluster']
management.context-path
相同Service-Id
,以逗号分隔讲解一下,通过创建一个请求前缀,可以在网关处使用前缀的方式将其排除,也就是外网将无法访问这些监控API,同时,内网还是可以进行加前缀的方式进行访问,为Spring Boot Admin
提供了支持条件。management
还支持port和ip的方式,但这两种方式有局限性,如果在同一台机器上部署多个服务,就会存在端口占用或者其他问题。这种方案还有一个好处,以上配置一旦确定以后,所有服务都不需要进行特殊化处理,可以直接使用。
在被监控的服务中添加
management.security.enabled=false