Spring Boot Admin 的使用详解
一、前言
SpringBootAdmin用于监控基于SpringBoot的应用。官方文档在这里(v1.3.4):《SpringBootAdminReferenceGuide》
实践的过程中,感觉这个UserGuide结构上还是说的不太明白。所以我就大概写一遍我的实践过程与理解。
阅读此文前提条件是:
- 使用过Maven。
- 你跑过基于SpringBoot的helloworld程序。
- 第三节需要你会点SpringCloud的EurekaServer配置。
二、在SpringBoot项目中配置
这种配置中,SpringBootAdmin作为Server,其他SpringBoot应用作为Client,Client把自身的信息“注册”到Server,我们就能在Server上看到“注册”的SpringBoot应用的状态信息了。
2.1、Server端
新建一个项目
2.1.1、添加依赖
pom.xml
de.codecentric spring-boot-admin-server 1.3.4 de.codecentric spring-boot-admin-server-ui 1.3.4
2.1.2、开启监控
添加@EnableAdminServer注解开启监控
@Configuration @EnableAutoConfiguration @EnableAdminServer publicclassSpringBootAdminApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(SpringBootAdminApplication.class,args); } }
这里未指定Server运行的端口,默认是8080,如果要指定,则需要在application.properties文件中设置:
application.properties
server.port=8080
2.2、Client端
2.2.1、添加依赖
pom.xml
de.codecentric spring-boot-admin-starter-client 1.3.4
这里的spring-boot-admin-starter-client会自动依赖jolokia-core,jolokia是用于JMX-bean管理的。
2.2.2、触发自动配置、指明Server注册地址
application.properties
spring.boot.admin.url=http://localhost:8080
上面3.1.2中Server端我们使用默认的8080端口,所以这里声明Server的地址为:http://localhost:8080
2.3、开始管理
至此,启动Server端和Client端,在浏览器输入Server的地址:http://localhost:8080就能看到“注册”进来的SpringBoot应用信息了。
2.4、显示应用版本
为了在SpringBootAdmin的应用管理列表显示被管理应用的版本号,你需要设置info.version,例如使用mavenfiltering:
application.properties
info.version=@project.version@
这里设置显示的版本号为Mavenpom.xml中的构建版本号。
2.5、JMX-bean管理
JMX-bean管理需要使用第三方的jolokia,因为spring-boot-admin-starter-client会自动依赖jolokia-core,所以这里不需要显示依赖了,第三节的基于Eureka注册发现的配置中,就需要显示地依赖:
pom.xml
org.jolokia jolokia-core
2.6、Loglevel管理
当前日志级别管理仅限Logback,通过JMX实现,所以需要依赖jolokia。同时,还需要配置Logback的JMXConfigurator:
logback.xml
这个logback.xml放在与application.properties同级的目录就可以了,如果不配置Logback,那么SpringBootAdmin就无法管理应用的日志级别。
2.7、Server端监控自己
以上的配置,基本就可以很好工作了。
但是有一个问题,我们没有监控作为Server端的SpringBootAdmin自身。如果要监控到Server自己,把Server端也当作是Client一样来配置就可以实现了:把2.2.1、2.2.2、2.4、2.6的步骤在Server端也配置一遍。
三、在SpringCloud项目的Eureka中配置
这里示例的SpringCloud项目是使用Eureka来做注册/发现的,官方Github示例里有基于Consul和Zookeper的配置。
配置好之后,SpringBootAdmin就可以管理所有注册到EurekaServer的应用了,包括SpringBootAdmin自己(因为自己也会注册到EurekaServer)。
3.1、一个简单的EurekaServer
关于EurekaServer这里不做详细介绍,只列一下配置经过:
pom.xml
org.springframework.cloud spring-cloud-starter-eureka-server
EurekaServer启动类
@SpringBootApplication @EnableEurekaServer publicclassEurekaServerApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(EurekaServerApplication.class,args); } }
application.properties
spring.application.name=eureka-server server.port=8761
在application.properties同级目录下新建bootstrap.properties文件:bootstrap.properties
eureka.instance.hostname=localhost eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
此文件作用与application.properties几乎样,只是但是作用在applicationcontext启动时期。原话是:likeapplication.propertiesbutforthebootstrapphaseofanapplicationcontext。
以上配置表明,我们的EurekaServer运行在8761端口。服务注册地址是:http://localhost:8761/eureka/
3.2、Server端
官方示例:spring-boot-admin-sample-eureka
3.2.1、添加spring-cloud-starter-eureka依赖
pom.xml
de.codecentric spring-boot-admin-server 1.3.4 de.codecentric spring-boot-admin-server-ui 1.3.4 org.springframework.cloud spring-cloud-starter-eureka
3.2.2、添加@EnableDiscoveryClient开启发现
@Configuration @EnableAutoConfiguration @EnableDiscoveryClient @EnableAdminServer publicclassSpringBootAdminApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(SpringBootAdminApplication.class,args); } }
3.2.3、指明去哪注册
application.properties
eureka.instance.client.serviceUrl.defaultZone:http://localhost:8761/eureka/
也就是我们在3.1中配置的EurekaServer服务地址。
这个配置我测试时并不成功,改为eureka.client.serviceUrl.defaultZone:http://localhost:8761/eureka/才可以,不知为何。
3.2.4、官方未说明的
3.2.1~3.2.3的配置,会把Server注册到EurekaServer,也就是说SpringBootAdmin也可以管理自身,但现在的Server配置还不全面(比如自身还缺的配置有:版本信息、JMX管理和Loglevel管理)。加上以下配置:application.properties
info.version=@project.version@
pom.xml
org.jolokia jolokia-core
logback.xml
3.3、Client端
Client端的配置主要是把自己注册到EurekaServer中就可以被SpringBootAdmin管理了,免去了手工配置SpringBootAdmin服务地址的操作(即2.2.2节操作)。
3.3.1、依赖
pom.xml
org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator
注意要添加spring-boot-starter-actuator依赖,因为获取应用信息是通过actuator中的相关endpoints获取的。
之所以Server端不需要添加此依赖,是因为spring-boot-admin-server依赖于spring-boot-admin-starter-client,而spring-boot-admin-starter-client依赖于spring-boot-starter-actuator。
3.3.2、启动类
@SpringBootApplication @EnableEurekaClient publicclassClientEurekaSampleApplication{ publicstaticvoidmain(String[]args){ SpringApplication.run(ClientEurekaSampleApplication.class,args); } }
添加@EnableDiscoveryClient或@EnableEurekaClient注解到启动类上,将自己注册到EruekaServer。
3.3.3、指明去哪注册
bootstrap.properties
eureka.client.serviceUrl.defaultZone:http://localhost:8761/eureka/
3.3.4、其他项配置
application.properties
info.version=@project.version@
logback.xml
pom.xml
org.jolokia jolokia-core
四、通知
官方提供好几种通知方式,这里贴一下邮件通知的配置,其他Pagerduty、Hipchat、Slack和Reminder的通知配置请参见官方文档。
使用spring-boot-starter-mail依赖配置JavaMailSender
pom.xml
org.springframework.boot spring-boot-starter-mail
application.properties
spring.mail.host=smtp.example.com spring.boot.admin.notify.mail.to=admin@example.com
表格:邮件配置选项
Propertyname
Description
Defaultvalue
中文说明
spring.boot.admin.notify.mail.enabled
Enablemailnotifications
true
默认启用
spring.boot.admin.notify.mail.ignore-changes
Comma-delimitedlistofstatuschangestobeignored.Format:“:”.Wildcardsallowed.
“UNKNOWN:UP”
需要忽略的状态改变通知,逗号分隔
spring.boot.admin.notify.mail.to
Comma-delimitedlistofmailrecipients
“root@localhost”
接收通知的邮箱地址,逗号分隔
spring.boot.admin.notify.mail.cc
Comma-delimitedlistofcarbon-copyrecipients
抄送
spring.boot.admin.notify.mail.from
Mailsender
发送人
spring.boot.admin.notify.mail.subject
Mailsubject.SpEL-expressionsaresupported
“#{application.name}(#{application.id})is#{to.status}”
主题
spring.boot.admin.notify.mail.text
Mailbody.SpEL-expressionsaresupported
“#{application.name}(#{application.id})\nstatuschangedfrom#{from.status}to#{to.status}\n\n#{application.healthUrl}”
内容
五、附:SpringBootAdminServer配置说明
表格:SpringBootAdminServer配置选项
Propertyname
Description
Defaultvalue
中文说明
spring.boot.admin.context-path
Thecontext-pathprefixesthepathwheretheAdminServer'sstaticsassetsandAPIshouldbeserved.RelativetotheDispatcher-Servlet.
AdminServer保留的静态访问和API的前缀(当你在业务应用中使用而不是单独使用时就很有必要了)
spring.boot.admin.monitor.period
Timeintervalinmstoupdatethestatusofapplicationswithexpiredstatus-informations.
10.000
更新应用信息的频率,单位毫秒
spring.boot.admin.monitor.status-lifetime
Lifetimeofapplicationstatusesinms.Theapplications/health-endpointwillnotbequerieduntilthelifetimehasexpired.
10.000
被监控的应用信息的过期时间,单位毫秒
5.1、SpringCloud对自动发现的支持
来自被发现的应用的状态信息是经过ServiceInstanceConverter转换过的,自动配置时,使用了SpringBootAdmin自带的Eureka转换实现。你也可以实现相关接口并并添加到上下文以替换默认的。
表格:注册发现配置选项
Propertyname
Description
Defaultvalue
中文说明
spring.boot.admin.discovery.enabled
EnablestheDiscoveryClient-supportfortheadminserver.
true
默认开启
spring.boot.admin.discovery.converter.management-context-path
Willbeappendedtotheservice-urlofthediscoveredservicewhenthemanagment-urlisconvertedbytheDefaultServiceInstanceConverter.
spring.boot.admin.discovery.converter.health-endpoint
Willbeappendedtothemanagement-urlofthediscoveredservicewhenthehealth-urlisconvertedbytheDefaultServiceInstanceConverter.
“health”
spring.boot.admin.discovery.ignored-services
Thisserviceswillbeignoredwhenusingdiscoveryandnotregisteredasapplication.
六、附:SpringBootAdminClient配置说明
SpringBootAdminClient注册到SpringBootAdminServer,Client定期地发送HttpPost到admin提供自己的应用信息。如果需要管理loglevels或JMX-beans,则要在依赖中添加Jolokia,使得JMX-beans也可以通过http访问。
表格:SpringBootAdminClient配置选项
七、问答
这部分我也啰嗦一下翻译出来。
我可以把spring-boot-admin添加到我的业务应用中吗?
答:可以,但不应该这么做。你可以设置spring.boot.admin.context-path来改变adminserver保留的UI和REST-API的访问,取决于你的应用复杂性,你可能会陷入困境。另一方面,当你的应用挂掉后,你的监控也一起挂掉,那么要监控有什么用呢?
该怎么自定义UI?
答:修改UI你仅可以复制并修改spring-boot-admin-ui,并添加你自己的模块到classpath中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。