Spring Boot Admin 的使用详解
一、前言
spring boot admin 用于监控基于 spring boot 的应用。官方文档在这里(v1.3.4):《spring boot admin reference guide》
实践的过程中,感觉这个 user guide 结构上还是说的不太明白。所以我就大概写一遍我的实践过程与理解。
阅读此文前提条件是:
- 使用过 maven。
- 你跑过基于 spring boot 的 hello world 程序。
- 第三节需要你会点 spring cloud 的 eureka server 配置。
二、在 spring boot 项目中配置
这种配置中,spring boot admin 作为 server,其他 spring boot 应用作为 client,client 把自身的信息“注册”到 server,我们就能在 server 上看到“注册”的 spring boot 应用的状态信息了。
2.1、server 端
新建一个项目
2.1.1、添加依赖
pom.xml
<dependency> <groupid>de.codecentric</groupid> <artifactid>spring-boot-admin-server</artifactid> <version>1.3.4</version> </dependency> <dependency> <groupid>de.codecentric</groupid> <artifactid>spring-boot-admin-server-ui</artifactid> <version>1.3.4</version> </dependency>
2.1.2、开启监控
添加 @enableadminserver 注解开启监控
@configuration @enableautoconfiguration @enableadminserver public class springbootadminapplication { public static void main(string[] args) { springapplication.run(springbootadminapplication.class, args); } }
这里未指定 server 运行的端口,默认是 8080,如果要指定,则需要在 application.properties 文件中设置:
application.properties
server.port=8080
2.2、client 端
2.2.1、添加依赖
pom.xml
<dependency> <groupid>de.codecentric</groupid> <artifactid>spring-boot-admin-starter-client</artifactid> <version>1.3.4</version> </dependency>
这里的 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 就能看到“注册”进来的 spring boot 应用信息了。
2.4、显示应用版本
为了在 spring boot admin 的应用管理列表显示被管理应用的版本号,你需要设置 info.version,例如使用 maven filtering:
application.properties
info.version=@project.version@
这里设置显示的版本号为 maven pom.xml 中的构建版本号。
2.5、jmx-bean管理
jmx-bean 管理需要使用第三方的 ,因为 spring-boot-admin-starter-client 会自动依赖 jolokia-core,所以这里不需要显示依赖了,第三节的基于 eureka 注册发现的配置中,就需要显示地依赖:
pom.xml
<dependency> <groupid>org.jolokia</groupid> <artifactid>jolokia-core</artifactid> </dependency>
2.6、loglevel 管理
当前日志级别管理仅限 logback,通过 jmx 实现,所以需要依赖 jolokia 。同时,还需要配置 logback 的 jmxconfigurator:
logback.xml
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxconfigurator/> </configuration>
这个 logback.xml 放在与 application.properties 同级的目录就可以了,如果不配置 logback,那么 spring boot admin 就无法管理应用的日志级别。
2.7、server 端监控自己
以上的配置,基本就可以很好工作了。
但是有一个问题,我们没有监控作为 server 端的 spring boot admin 自身。如果要监控到 server 自己,把 server 端也当作是 client 一样来配置就可以实现了:把 2.2.1、2.2.2、2.4、2.6 的步骤在 server 端也配置一遍。
三、在 spring cloud 项目的 eureka 中配置
这里示例的 spring cloud 项目是使用 eureka 来做注册/发现的,官方 github 示例里有基于 consul 和 zookeper 的配置。
配置好之后,spring boot admin 就可以管理所有注册到 eureka server 的应用了,包括 spring boot admin 自己(因为自己也会注册到 eureka server)。
3.1、一个简单的 eureka server
关于 eureka server 这里不做详细介绍,只列一下配置经过:
pom.xml
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency>
eureka server 启动类
@springbootapplication @enableeurekaserver public class eurekaserverapplication { public static void main(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 几乎样,只是但是作用在 application context 启动时期。原话是:like application.properties but for the bootstrap phase of an application context 。
以上配置表明,我们的 eureka server 运行在 8761 端口。服务注册地址是:http://localhost:8761/eureka/
3.2、server 端
官方示例:
3.2.1、添加 spring-cloud-starter-eureka 依赖
pom.xml
<dependency> <groupid>de.codecentric</groupid> <artifactid>spring-boot-admin-server</artifactid> <version>1.3.4</version> </dependency> <dependency> <groupid>de.codecentric</groupid> <artifactid>spring-boot-admin-server-ui</artifactid> <version>1.3.4</version> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency>
3.2.2、添加 @enablediscoveryclient 开启发现
@configuration @enableautoconfiguration @enablediscoveryclient @enableadminserver public class springbootadminapplication { public static void main(string[] args) { springapplication.run(springbootadminapplication.class, args); } }
3.2.3、指明去哪注册
application.properties
eureka.instance.client.serviceurl.defaultzone: http://localhost:8761/eureka/
也就是我们在 3.1 中配置的 eureka server 服务地址。
这个配置我测试时并不成功,改为 eureka.client.serviceurl.defaultzone: http://localhost:8761/eureka/ 才可以,不知为何。
3.2.4、官方未说明的
3.2.1 ~ 3.2.3 的配置,会把 server 注册到 eureka server,也就是说 spring boot admin 也可以管理自身,但现在的 server 配置还不全面(比如自身还缺的配置有:版本信息、 jmx 管理和 loglevel 管理)。加上以下配置: application.properties
info.version=@project.version@
pom.xml
<dependency> <groupid>org.jolokia</groupid> <artifactid>jolokia-core</artifactid> </dependency>
logback.xml
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxconfigurator/> </configuration>
3.3、client 端
client 端的配置主要是把自己注册到 eureka server 中就可以被 spring boot admin 管理了,免去了手工配置 spring boot admin 服务地址的操作(即 2.2.2 节操作)。
3.3.1、依赖
pom.xml
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency>
注意要添加 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 public class clienteurekasampleapplication { public static void main(string[] args) { springapplication.run(clienteurekasampleapplication.class, args); } }
添加 @enablediscoveryclient 或 @enableeurekaclient 注解到启动类上,将自己注册到 erueka server。
3.3.3、指明去哪注册
bootstrap.properties
eureka.client.serviceurl.defaultzone: http://localhost:8761/eureka/
3.3.4、其他项配置
application.properties
info.version=@project.version@
logback.xml
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxconfigurator/> </configuration>
pom.xml
<dependency> <groupid>org.jolokia</groupid> <artifactid>jolokia-core</artifactid> </dependency>
四、通知
官方提供好几种通知方式,这里贴一下邮件通知的配置,其他 pagerduty、hipchat 、slack 和 reminder 的通知配置请参见官方文档。
使用 spring-boot-starter-mail 依赖配置 javamailsender
pom.xml
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-mail</artifactid> </dependency>
application.properties
spring.mail.host=smtp.example.com spring.boot.admin.notify.mail.to=admin@example.com
表格:邮件配置选项
property name | description | default value | 中文说明 |
---|---|---|---|
spring.boot.admin.notify.mail.enabled | enable mail notifications | true | 默认启用 |
spring.boot.admin.notify.mail.ignore-changes | comma-delimited list of status changes to be ignored. format: “:”. wildcards allowed. | “unknown:up” | 需要忽略的状态改变通知,逗号分隔 |
spring.boot.admin.notify.mail.to | comma-delimited list of mail recipients | “root@localhost” | 接收通知的邮箱地址,逗号分隔 |
spring.boot.admin.notify.mail.cc | comma-delimited list of carbon-copy recipients | 抄送 | |
spring.boot.admin.notify.mail.from | mail sender | 发送人 | |
spring.boot.admin.notify.mail.subject | mail subject. spel-expressions are supported | “#{application.name} (#{application.id}) is #{to.status}” | 主题 |
spring.boot.admin.notify.mail.text | mail body. spel-expressions are supported | “#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthurl}” | 内容 |
五、附:spring boot admin server 配置说明
表格:spring boot admin server 配置选项
property name | description | default value | 中文说明 |
---|---|---|---|
spring.boot.admin.context-path | the context-path prefixes the path where the admin server's statics assets and api should be served. relative to the dispatcher-servlet. | admin server 保留的静态访问和api的前缀(当你在业务应用中使用而不是单独使用时就很有必要了) | |
spring.boot.admin.monitor.period | time interval in ms to update the status of applications with expired status-informations. | 10.000 | 更新应用信息的频率,单位毫秒 |
spring.boot.admin.monitor.status-lifetime | lifetime of application statuses in ms. the applications /health-endpoint will not be queried until the lifetime has expired. | 10.000 | 被监控的应用信息的过期时间,单位毫秒 |
5.1、spring cloud 对自动发现的支持
来自被发现的应用的状态信息是经过 serviceinstanceconverter 转换过的,自动配置时,使用了 spring boot admin 自带的 eureka 转换实现。你也可以实现相关接口并并添加到上下文以替换默认的。
表格:注册发现配置选项
property name | description | default value | 中文说明 |
---|---|---|---|
spring.boot.admin.discovery.enabled | enables the discoveryclient-support for the admin server. | true | 默认开启 |
spring.boot.admin.discovery.converter.management-context-path | will be appended to the service-url of the discovered service when the managment-url is converted by the defaultserviceinstanceconverter. | ||
spring.boot.admin.discovery.converter.health-endpoint | will be appended to the management-url of the discovered service when the health-url is converted by the defaultserviceinstanceconverter. | “health” | |
spring.boot.admin.discovery.ignored-services | this services will be ignored when using discovery and not registered as application. |
六、附:spring boot admin client 配置说明
spring boot admin client 注册到 spring boot admin server,client 定期地发送 http post 到 admin 提供自己的应用信息。如果需要管理 loglevels 或 jmx-beans ,则要在依赖中添加 jolokia ,使得 jmx-beans 也可以通过 http 访问。
表格:spring boot admin client配置选项
property name | description | default value | 中文说明 |
---|---|---|---|
spring.boot.admin.client.enabled | enables the spring boot admin client. | true | 默认开启 |
spring.boot.admin.url | list of urls of the spring boot admin server to register at. this triggers the autoconfiguration. mandatory. | admin server 的地址列表,此设置会触发自动配置,必须 | |
spring.boot.admin.api-path | http-path of registration endpoint at your admin server. | “api/applications” | 注册到 admin server 端点的 http-path |
spring.boot.admin.username spring.boot.admin.password | username and password for http-basic authentication. if set the registration uses http-basic-authentication when registering at the admin server. | 注册到 admin server 的账号密码 | |
spring.boot.admin.period | interval for repeating the registration (in ms). | 10.000 | 重试注册的间隔时间 |
spring.boot.admin.auto-registration | if set to true the periodic task to register the application is automatically scheduled after the application is ready. | true | 应用启动后自动执行周期性的注册任务 |
spring.boot.admin.auto-deregistration | switch to enable auto-deregistration at spring boot admin server when context is closed. | false | 当应用关闭时,自动取消注册 |
spring.boot.admin.client.health-url | client-health-url to register with. can be overridden in case the reachable url is different (e.g. docker). must be unique in registry. | guessed based on management-url and endpoints.health.id. | |
spring.boot.admin.client.management-url | client-management-url to register with. can be overridden in case the reachable url is different (e.g. docker). | guessed based on service-url, server.servlet-path, management.port and management.context-path. | |
spring.boot.admin.client.service-url | client-service-url to register with. can be overridden in case the reachable url is different (e.g. docker). | guessed based on hostname, server.port and server.context-path. | |
spring.boot.admin.client.name | name to register with. | ${spring.application.name} if set, “spring-boot-application” otherwise. | 注册时的名字 |
spring.boot.admin.client.prefer-ip | use the ip-address rather then the hostname in the guessed urls. if server.address / management.address is set, it get used. otherwise the ip address returned from inetaddress.getlocalhost() gets used. | false |
七、问答
这部分我也啰嗦一下翻译出来。
我可以把 spring-boot-admin 添加到我的业务应用中吗?
答:可以,但不应该这么做。你可以设置 spring.boot.admin.context-path 来改变 admin server 保留的 ui 和 rest-api 的访问,取决于你的应用复杂性,你可能会陷入困境。另一方面,当你的应用挂掉后,你的监控也一起挂掉,那么要监控有什么用呢?
该怎么自定义 ui ?
答:修改 ui 你仅可以复制并修改 spring-boot-admin-ui,并添加你自己的模块到 classpath 中。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: JAVA