http://blog.csdn.net/kinginblue/article/details/52132113

*******************************************

一、前言

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.1.2、开启监控

添加 @EnableAdminServer 注解开启监控

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这里未指定 Server 运行的端口,默认是 8080,如果要指定,则需要在 application.properties 文件中设置:
application.properties

server.port=8080
  • 1
  • 1

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

这里的 spring-boot-admin-starter-client 会自动依赖 jolokia-core,jolokia是用于 JMX-bean 管理的。

2.2.2、触发自动配置、指明 Server 注册地址

application.properties

spring.boot.admin.url=http://localhost:8080
  • 1
  • 1

上面 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@
  • 1
  • 1

这里设置显示的版本号为 Maven pom.xml 中的构建版本号。

2.5、JMX-bean管理

JMX-bean 管理需要使用第三方的 jolokia ,因为 spring-boot-admin-starter-client 会自动依赖 jolokia-core,所以这里不需要显示依赖了,第三节的基于 Eureka 注册发现的配置中,就需要显示地依赖:
pom.xml

<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2.6、Loglevel 管理

当前日志级别管理仅限 Logback,通过 JMX 实现,所以需要依赖 jolokia 。同时,还需要配置 Logback 的 JMXConfigurator
logback.xml

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这个 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 示例里有基于 ConsulZookeper 的配置。

配置好之后,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>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Eureka Server 启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

application.properties

spring.application.name=eureka-server
server.port=8761
  • 1
  • 2
  • 1
  • 2

在 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/
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

此文件作用与 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 端

官方示例:spring-boot-admin-sample-eureka

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.2.2、添加 @EnableDiscoveryClient 开启发现

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.2.3、指明去哪注册

application.properties

eureka.instance.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
  • 1
  • 1

也就是我们在 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@
  • 1
  • 1

pom.xml

<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

logback.xml

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意要添加 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);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

添加 @EnableDiscoveryClient@EnableEurekaClient 注解到启动类上,将自己注册到 Erueka Server。

3.3.3、指明去哪注册

bootstrap.properties

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/
  • 1
  • 1

3.3.4、其他项配置

application.properties

info.version=@project.version@
  • 1
  • 1

logback.xml

<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<jmxConfigurator/>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

pom.xml

<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

四、通知

官方提供好几种通知方式,这里贴一下邮件通知的配置,其他 PagerdutyHipchatSlackReminder 的通知配置请参见官方文档。

使用 spring-boot-starter-mail 依赖配置 JavaMailSender

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

application.properties

spring.mail.host=smtp.example.com
spring.boot.admin.notify.mail.to=admin@example.com
  • 1
  • 2
  • 1
  • 2

表格:邮件配置选项

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 中。

Spring Boot Admin 的使用 2的更多相关文章

  1. Spring Boot Admin的使用

    http://www.jianshu.com/p/e20a5f42a395 ******************************* 上一篇文章中了解了Spring Boot提供的监控接口,例如 ...

  2. Spring Boot Admin Reference Guide

    1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...

  3. spring boot admin

    这里记录一个spring cloud的模板,有的模块spring cloud eureka + spring boot admin + spring cloud zuul + 一个普通spring c ...

  4. spring boot admin + spring boot actuator + erueka 微服务监控

    关于spring boot actuator简单使用,请看 简单的spring boot actuator 使用,点击这里 spring boot admin 最新的正式版本是1.5.3 与 spri ...

  5. Spring Boot admin 2.0 详解

    一.什么是Spring Boot Admin ? Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序. 应用程序作为Spring Boot Admin C ...

  6. Spring boot admin 节点状态一直为DOWN的排查

    项目中需要监控各个微服务节点的健康状态,找到了spring boot admin这个全家桶监控工具,它其实是Vue.js美化过的Spring Boot Actuator,官方的解释是: codecen ...

  7. SpringCloud(8)微服务监控Spring Boot Admin

    1.简介 Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件.Spring Boot Admin 分为 Server 端和 Client 端,Spring ...

  8. Spring Boot Admin 的使用

    Spring Boot 版本: 1.5.20 一.Spring Boot Admin Server 1.在pom.xml中增加 <dependency> <groupId>or ...

  9. 物联网架构成长之路(30)-Spring Boot Admin微服务WebUI监控

    0. 前言 一个完整的微服务解决方案包含了许多微服务,基于我们需要观察各个微服务的运行状态,因此Spring Boot 生态提供了Spring Boot Admin 这个组件来实现微服务管理WEB U ...

随机推荐

  1. 【后台测试】Linux下小试jmeter

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处.  转载请注明出处:http://www.cnblogs.com/by-dream/p/5784288.html 前言 上一篇主要讲了在Window ...

  2. Nodejs开发(2.连接MongoDB)

    一.先配置MongoDB Win10下下载那个安装版,zip版的会报却各种DLL,安装在你希望的路径,实在安装错了,就剪切过来也行(本例E:\mongodb). 然后是配置启动脚本,就是写一个bat文 ...

  3. iOS开发之音频口通信-通过方波来收发数据

    之前做过的项目有需要通过音频口通信用方波来收发数据,由于这方面的资料比较少,下面就介绍下其原理,希望能给大家帮助. 一. 音频通信简介大家应该都知道支付宝声波支付和拉卡拉吧,它们都是利用手机的音频口( ...

  4. 怎样实现excel隔行隔列变色效果的方法

    大家在使用excel的过程中,一定见过别人编排的excel文档,隔行添加单元格背景颜色,也就是通常所说的隔行变色效果.Excel中隔行变色效果的好处是:当在Excel中浏览一个非常大的工作簿中的数据时 ...

  5. tableView和scrollView滚动起冲突

    tableView和scrollView滚动起冲突 tableView也是继承的scrollView,所以在滚动的时候也会触发scrollView的代理方法,在scrollViewDidScroll中 ...

  6. ios-高仿别踩白块游戏的实现

    先看下效果图片 前几天看到一个游戏叫别踩白块,下载量还挺大几百万了都,下载下来玩了玩看了看,这个游戏还挺简单的.俗话说想一千遍,一万遍不如动手做一遍来的实在.昨晚以及今天白天闲的没事就开搞了,下午六点 ...

  7. Bibtex使用方法

    BibTeX 是一个使用数据库的的方式来管理参考文献程序, 用于协调LaTeX的参考文献处理. BibTeX 文件的后缀名为 .bib . 先来看一个例子: @article{Gettys90,aut ...

  8. sql 遍历结果集

    declare @temp table ( [id] int IDENTITY(1,1), [Name] varchar(10) ) --select * from @temp declare @te ...

  9. 用mysql++读写二进制

    方法1: // mysqlTest.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <mysql++.h> #inc ...

  10. 嵌入式Linux内核制作【转】

    本文转载自:http://blog.csdn.net/coding__madman/article/details/51291316 1. Linux体系结构 从整体上来分,linux可以分为User ...