springbootadmin结合eureka的使用

Spring Boot Admin 的使用详解spring

1、前言
Spring Boot Admin 用于监控基于 Spring Boot 的应用。官方文档在这里(v1.3.4):《Spring Boot Admin Reference Guide》
2、在 Spring Boot 项目中配置
这种配置中,Spring Boot Admin 做为 Server,其余 Spring Boot 应用做为 Client,Client 把自身的信息“注册”到 Server,咱们就能在 Server 上看到“注册”的 Spring Boot 应用的状态信息了。
2.一、Server 端
新建一个项目
2.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>express

2.1.二、开启监控
添加 @EnableAdminServer 注解开启监控
@Configuration@EnableAutoConfiguration@EnableAdminServerpublic class SpringBootAdminApplication {  public static void main(String[] args) {    SpringApplication.run(SpringBootAdminApplication.class, args);  }}bootstrap

这里未指定 Server 运行的端口,默认是 8080,若是要指定,则须要在 application.properties 文件中设置: 
application.properties
server.port=8080api

2.二、Client 端
2.2.一、添加依赖
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.二、触发自动配置、指明 Server 注册地址
application.properties
spring.boot.admin.url=http://localhost:8080app

上面 3.1.2 中 Server 端咱们使用默认的 8080 端口,因此这里声明 Server 的地址为:http://localhost:8080
2.三、开始管理
至此,启动 Server 端和 Client 端,在浏览器输入 Server 的地址:http://localhost:8080 就能看到“注册”进来的 Spring Boot 应用信息了。
2.四、显示应用版本
为了在 Spring Boot Admin 的应用管理列表显示被管理应用的版本号,你须要设置 info.version,例如使用 maven filtering: 
application.properties
info.version=@project.version@maven

这里设置显示的版本号为 Maven pom.xml 中的构建版本号。
2.五、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>ide

2.六、Loglevel 管理
当前日志级别管理仅限 Logback,经过 JMX 实现,因此须要依赖 jolokia 。同时,还须要配置 Logback 的 JMXConfigurator: 
logback.xml
<configuration>  <include resource="org/springframework/boot/logging/logback/base.xml"/>  <jmxConfigurator/></configuration>spring-boot

这个 logback.xml 放在与 application.properties 同级的目录就能够了,若是不配置 Logback,那么 Spring Boot Admin 就没法管理应用的日志级别。
2.七、Server 端监控本身
以上的配置,基本就能够很好工做了。
可是有一个问题,咱们没有监控做为 Server 端的 Spring Boot Admin 自身。若是要监控到 Server 本身,把 Server 端也看成是 Client 同样来配置就能够实现了:把 2.2.一、2.2.二、2.四、2.6 的步骤在 Server 端也配置一遍。
3、在 Spring Cloud 项目的 Eureka 中配置
这里示例的 Spring Cloud 项目是使用 Eureka 来作注册/发现的,官方 Github 示例里有基于 Consul 和 Zookeper 的配置。
配置好以后,Spring Boot Admin 就能够管理全部注册到 Eureka Server 的应用了,包括 Spring Boot Admin 本身(由于本身也会注册到 Eureka Server)。
3.一、一个简单的 Eureka Server
关于 Eureka Server 这里不作详细介绍,只列一下配置通过:
pom.xml
<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>测试

Eureka Server 启动类
@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {  public static void main(String[] args) {    SpringApplication.run(EurekaServerApplication.class, args);  }}

application.properties
spring.application.name=eureka-serverserver.port=8761

在 application.properties 同级目录下新建 bootstrap.properties 文件: bootstrap.properties
eureka.instance.hostname=localhosteureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseeureka.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.二、Server 端
官方示例:spring-boot-admin-sample-eureka
3.2.一、添加 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.二、添加 @EnableDiscoveryClient 开启发现
@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableAdminServerpublic class SpringBootAdminApplication {  public static void main(String[] args) {    SpringApplication.run(SpringBootAdminApplication.class, args);  }}

3.2.三、指明去哪注册
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.四、官方未说明的
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.三、Client 端
Client 端的配置主要是把本身注册到 Eureka Server 中就能够被 Spring Boot Admin 管理了,免去了手工配置 Spring Boot Admin 服务地址的操做(即 2.2.2 节操做)。
3.3.一、依赖
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.二、启动类
@SpringBootApplication@EnableEurekaClientpublic class ClientEurekaSampleApplication {  public static void main(String[] args) {    SpringApplication.run(ClientEurekaSampleApplication.class, args);  }}

添加 @EnableDiscoveryClient 或 @EnableEurekaClient 注解到启动类上,将本身注册到 Erueka Server。
3.3.三、指明去哪注册
bootstrap.properties
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

3.3.四、其余项配置
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>

4、通知
官方提供好几种通知方式,这里贴一下邮件通知的配置,其余 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.comspring.boot.admin.notify.mail.to=admin@example.com

表格:邮件配置选项
[td]

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}nn#{application.healthUrl}” 内容
5、附:Spring Boot Admin Server 配置说明
表格:Spring Boot Admin Server 配置选项
[td]

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.一、Spring Cloud 对自动发现的支持
来自被发现的应用的状态信息是通过 ServiceInstanceConverter 转换过的,自动配置时,使用了 Spring Boot Admin 自带的 Eureka 转换实现。你也能够实现相关接口并并添加到上下文以替换默认的。
表格:注册发现配置选项
[td]

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.    
6、附: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配置选项
[td]

Property nameDescriptionDefault 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-pathspring.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

相关文章
相关标签/搜索