欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot Admin 的使用詳解

 更新時(shí)間:2017年09月26日 10:53:12   作者:kinginblue  
這篇文章主要介紹了Spring Boot Admin 的使用詳解,Spring Boot Admin 用于監(jiān)控基于 Spring Boot 的應(yīng)用,有興趣的可以了解一下

一、前言

Spring Boot Admin 用于監(jiān)控基于 Spring Boot 的應(yīng)用。官方文檔在這里(v1.3.4):《Spring Boot Admin Reference Guide

實(shí)踐的過(guò)程中,感覺(jué)這個(gè) User Guide 結(jié)構(gòu)上還是說(shuō)的不太明白。所以我就大概寫一遍我的實(shí)踐過(guò)程與理解。

閱讀此文前提條件是:

  • 使用過(guò) Maven。
  • 你跑過(guò)基于 Spring Boot 的 hello world 程序。
  • 第三節(jié)需要你會(huì)點(diǎn) Spring Cloud 的 Eureka Server 配置。

二、在 Spring Boot 項(xiàng)目中配置

這種配置中,Spring Boot Admin 作為 Server,其他 Spring Boot 應(yīng)用作為 Client,Client 把自身的信息“注冊(cè)”到 Server,我們就能在 Server 上看到“注冊(cè)”的 Spring Boot 應(yīng)用的狀態(tài)信息了。

2.1、Server 端

新建一個(gè)項(xiàng)目

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、開啟監(jiān)控

添加 @EnableAdminServer 注解開啟監(jiān)控

@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootAdminApplication.class, args);
  }
}

這里未指定 Server 運(yùn)行的端口,默認(rèn)是 8080,如果要指定,則需要在 application.properties 文件中設(shè)置:
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 會(huì)自動(dòng)依賴 jolokia-core,jolokia是用于 JMX-bean 管理的。

2.2.2、觸發(fā)自動(dòng)配置、指明 Server 注冊(cè)地址

application.properties

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

上面 3.1.2 中 Server 端我們使用默認(rèn)的 8080 端口,所以這里聲明 Server 的地址為:http://localhost:8080

2.3、開始管理

至此,啟動(dòng) Server 端和 Client 端,在瀏覽器輸入 Server 的地址:http://localhost:8080 就能看到“注冊(cè)”進(jìn)來(lái)的 Spring Boot 應(yīng)用信息了。

2.4、顯示應(yīng)用版本

為了在 Spring Boot Admin 的應(yīng)用管理列表顯示被管理應(yīng)用的版本號(hào),你需要設(shè)置 info.version,例如使用 maven filtering:
application.properties

info.version=@project.version@

這里設(shè)置顯示的版本號(hào)為 Maven pom.xml 中的構(gòu)建版本號(hào)。

2.5、JMX-bean管理

JMX-bean 管理需要使用第三方的 jolokia ,因?yàn)?spring-boot-admin-starter-client 會(huì)自動(dòng)依賴 jolokia-core,所以這里不需要顯示依賴了,第三節(jié)的基于 Eureka 注冊(cè)發(fā)現(xiàn)的配置中,就需要顯示地依賴:

pom.xml

<dependency>
  <groupId>org.jolokia</groupId>
  <artifactId>jolokia-core</artifactId>
</dependency>

2.6、Loglevel 管理

當(dāng)前日志級(jí)別管理僅限 Logback,通過(guò) JMX 實(shí)現(xiàn),所以需要依賴 jolokia 。同時(shí),還需要配置 Logback 的 JMXConfigurator:
logback.xml

<configuration>
  <include resource="org/springframework/boot/logging/logback/base.xml"/>
  <jmxConfigurator/>
</configuration>

這個(gè) logback.xml 放在與 application.properties 同級(jí)的目錄就可以了,如果不配置 Logback,那么 Spring Boot Admin 就無(wú)法管理應(yīng)用的日志級(jí)別。

2.7、Server 端監(jiān)控自己

以上的配置,基本就可以很好工作了。

但是有一個(gè)問(wèn)題,我們沒(méi)有監(jiān)控作為 Server 端的 Spring Boot Admin 自身。如果要監(jiān)控到 Server 自己,把 Server 端也當(dāng)作是 Client 一樣來(lái)配置就可以實(shí)現(xiàn)了:把 2.2.1、2.2.2、2.4、2.6 的步驟在 Server 端也配置一遍。

三、在 Spring Cloud 項(xiàng)目的 Eureka 中配置

這里示例的 Spring Cloud 項(xiàng)目是使用 Eureka 來(lái)做注冊(cè)/發(fā)現(xiàn)的,官方 Github 示例里有基于 Consul 和 Zookeper 的配置。

配置好之后,Spring Boot Admin 就可以管理所有注冊(cè)到 Eureka Server 的應(yīng)用了,包括 Spring Boot Admin 自己(因?yàn)樽约阂矔?huì)注冊(cè)到 Eureka Server)。

3.1、一個(gè)簡(jiǎn)單的 Eureka Server

關(guān)于 Eureka Server 這里不做詳細(xì)介紹,只列一下配置經(jīng)過(guò):

pom.xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

Eureka Server 啟動(dòng)類

@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 同級(jí)目錄下新建 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 啟動(dòng)時(shí)期。原話是:like application.properties but for the bootstrap phase of an application context 。

以上配置表明,我們的 Eureka Server 運(yùn)行在 8761 端口。服務(wù)注冊(cè)地址是: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>

3.2.2、添加 @EnableDiscoveryClient 開啟發(fā)現(xiàn)

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootAdminApplication.class, args);
  }
}

3.2.3、指明去哪注冊(cè)

application.properties

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

也就是我們?cè)?3.1 中配置的 Eureka Server 服務(wù)地址。

這個(gè)配置我測(cè)試時(shí)并不成功,改為 eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/ 才可以,不知為何。

3.2.4、官方未說(shuō)明的

3.2.1 ~ 3.2.3 的配置,會(huì)把 Server 注冊(cè)到 Eureka Server,也就是說(shuō) Spring Boot Admin 也可以管理自身,但現(xiàn)在的 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 端的配置主要是把自己注冊(cè)到 Eureka Server 中就可以被 Spring Boot Admin 管理了,免去了手工配置 Spring Boot Admin 服務(wù)地址的操作(即 2.2.2 節(jié)操作)。

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 依賴,因?yàn)楂@取應(yīng)用信息是通過(guò) actuator 中的相關(guān) endpoints 獲取的。

之所以 Server 端不需要添加此依賴,是因?yàn)?spring-boot-admin-server 依賴于 spring-boot-admin-starter-client ,而 spring-boot-admin-starter-client 依賴于 spring-boot-starter-actuator 。

3.3.2、啟動(dòng)類

@SpringBootApplication
@EnableEurekaClient
public class ClientEurekaSampleApplication {
  public static void main(String[] args) {
    SpringApplication.run(ClientEurekaSampleApplication.class, args);
  }
}

添加 @EnableDiscoveryClient 或 @EnableEurekaClient 注解到啟動(dòng)類上,將自己注冊(cè)到 Erueka Server。

3.3.3、指明去哪注冊(cè)

bootstrap.properties

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

3.3.4、其他項(xiàng)配置

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 的通知配置請(qǐng)參見(jiàn)官方文檔。

使用 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

表格:郵件配置選項(xiàng)

 

Property name Description Default value 中文說(shuō)明
spring.boot.admin.notify.mail.enabled Enable mail notifications true 默認(rèn)啟用
spring.boot.admin.notify.mail.ignore-changes Comma-delimited list of status changes to be ignored. Format: “:”. Wildcards allowed. “UNKNOWN:UP” 需要忽略的狀態(tài)改變通知,逗號(hào)分隔
spring.boot.admin.notify.mail.to Comma-delimited list of mail recipients “root@localhost” 接收通知的郵箱地址,逗號(hào)分隔
spring.boot.admin.notify.mail.cc Comma-delimited list of carbon-copy recipients 抄送
spring.boot.admin.notify.mail.from Mail sender 發(fā)送人
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}” 內(nèi)容

五、附:Spring Boot Admin Server 配置說(shuō)明

表格:Spring Boot Admin Server 配置選項(xiàng)

 

Property name Description Default value 中文說(shuō)明
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 保留的靜態(tài)訪問(wèn)和API的前綴(當(dāng)你在業(yè)務(wù)應(yīng)用中使用而不是單獨(dú)使用時(shí)就很有必要了)
spring.boot.admin.monitor.period Time interval in ms to update the status of applications with expired status-informations. 10.000 更新應(yīng)用信息的頻率,單位毫秒
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 被監(jiān)控的應(yīng)用信息的過(guò)期時(shí)間,單位毫秒

5.1、Spring Cloud 對(duì)自動(dòng)發(fā)現(xiàn)的支持

來(lái)自被發(fā)現(xiàn)的應(yīng)用的狀態(tài)信息是經(jīng)過(guò) ServiceInstanceConverter 轉(zhuǎn)換過(guò)的,自動(dòng)配置時(shí),使用了 Spring Boot Admin 自帶的 Eureka 轉(zhuǎn)換實(shí)現(xiàn)。你也可以實(shí)現(xiàn)相關(guān)接口并并添加到上下文以替換默認(rèn)的。

表格:注冊(cè)發(fā)現(xiàn)配置選項(xiàng)

Property name Description Default value 中文說(shuō)明
spring.boot.admin.discovery.enabled Enables the DiscoveryClient-support for the admin server. true 默認(rèn)開啟
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 配置說(shuō)明

Spring Boot Admin Client 注冊(cè)到 Spring Boot Admin Server,Client 定期地發(fā)送 Http Post 到 admin 提供自己的應(yīng)用信息。如果需要管理 loglevels 或 JMX-beans ,則要在依賴中添加 Jolokia ,使得 JMX-beans 也可以通過(guò) http 訪問(wèn)。

表格:Spring Boot Admin Client配置選項(xiàng)

Property name Description Default value 中文說(shuō)明
spring.boot.admin.client.enabled Enables the Spring Boot Admin Client. true 默認(rèn)開啟
spring.boot.admin.url List of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory. admin server 的地址列表,此設(shè)置會(huì)觸發(fā)自動(dòng)配置,必須
spring.boot.admin.api-path Http-path of registration endpoint at your admin server. “api/applications” 注冊(cè)到 admin server 端點(diǎn)的 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. 注冊(cè)到 admin server 的賬號(hào)密碼
spring.boot.admin.period Interval for repeating the registration (in ms). 10.000 重試注冊(cè)的間隔時(shí)間
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 應(yīng)用啟動(dòng)后自動(dòng)執(zhí)行周期性的注冊(cè)任務(wù)
spring.boot.admin.auto-deregistration Switch to enable auto-deregistration at Spring Boot Admin server when context is closed. false 當(dāng)應(yīng)用關(guān)閉時(shí),自動(dòng)取消注冊(cè)
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. 注冊(cè)時(shí)的名字
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

七、問(wèn)答

這部分我也啰嗦一下翻譯出來(lái)。

我可以把 spring-boot-admin 添加到我的業(yè)務(wù)應(yīng)用中嗎?

答:可以,但不應(yīng)該這么做。你可以設(shè)置 spring.boot.admin.context-path 來(lái)改變 admin server 保留的 UI 和 REST-API 的訪問(wèn),取決于你的應(yīng)用復(fù)雜性,你可能會(huì)陷入困境。另一方面,當(dāng)你的應(yīng)用掛掉后,你的監(jiān)控也一起掛掉,那么要監(jiān)控有什么用呢?

該怎么自定義 UI ?

答:修改 UI 你僅可以復(fù)制并修改 spring-boot-admin-ui,并添加你自己的模塊到 classpath 中。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論