Admin怎么在SpringBoot中使用

本篇文章為大家展示了Admin 怎么在Spring Boot中使用,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

我們提供的服務(wù)有:成都網(wǎng)站設(shè)計、網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、道縣ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的道縣網(wǎng)站制作公司

一、前言

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

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

閱讀此文前提條件是:

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

二、在 Spring Boot 項目中配置

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

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

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

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

這里未指定 Server 運行的端口,默認(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 會自動依賴 jolokia-core,jolokia是用于 JMX-bean 管理的。

2.2.2、觸發(fā)自動配置、指明 Server 注冊地址

application.properties

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

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

2.3、開始管理

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

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

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

info.version=@project.version@

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

2.5、JMX-bean管理

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

pom.xml

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

2.6、Loglevel 管理

當(dāng)前日志級別管理僅限 Logback,通過 JMX 實現(xiàn),所以需要依賴 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 就無法管理應(yīng)用的日志級別。

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

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

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

三、在 Spring Cloud 項目的 Eureka 中配置

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

配置好之后,Spring Boot Admin 就可以管理所有注冊到 Eureka Server 的應(yīng)用了,包括 Spring Boot Admin 自己(因為自己也會注冊到 Eureka Server)。

3.1、一個簡單的 Eureka Server

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

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 端口。服務(wù)注冊地址是: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、指明去哪注冊

application.properties

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

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

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

3.2.4、官方未說明的

3.2.1 ~ 3.2.3 的配置,會把 Server 注冊到 Eureka Server,也就是說 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 端的配置主要是把自己注冊到 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īng)用信息是通過 actuator 中的相關(guān) 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 nameDescriptionDefault value中文說明
spring.boot.admin.notify.mail.enabledEnable mail notificationstrue默認(rèn)啟用
spring.boot.admin.notify.mail.ignore-changesComma-delimited list of status changes to be ignored. Format: “:”. Wildcards allowed.“UNKNOWN:UP”需要忽略的狀態(tài)改變通知,逗號分隔
spring.boot.admin.notify.mail.toComma-delimited list of mail recipients“root@localhost”接收通知的郵箱地址,逗號分隔
spring.boot.admin.notify.mail.ccComma-delimited list of carbon-copy recipients抄送
spring.boot.admin.notify.mail.fromMail sender發(fā)送人
spring.boot.admin.notify.mail.subjectMail subject. SpEL-expressions are supported“#{application.name} (#{application.id}) is #{to.status}”主題
spring.boot.admin.notify.mail.textMail 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 配置說明

表格:Spring Boot Admin Server 配置選項

Property nameDescriptionDefault value中文說明
spring.boot.admin.context-pathThe 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)訪問和API的前綴(當(dāng)你在業(yè)務(wù)應(yīng)用中使用而不是單獨使用時就很有必要了)
spring.boot.admin.monitor.periodTime interval in ms to update the status of applications with expired status-informations.10.000更新應(yīng)用信息的頻率,單位毫秒
spring.boot.admin.monitor.status-lifetimeLifetime of application statuses in ms. The applications /health-endpoint will not be queried until the lifetime has expired.10.000被監(jiān)控的應(yīng)用信息的過期時間,單位毫秒

5.1、Spring Cloud 對自動發(fā)現(xiàn)的支持

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

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

Property nameDescriptionDefault value中文說明
spring.boot.admin.discovery.enabledEnables the DiscoveryClient-support for the admin server.true默認(rèn)開啟
spring.boot.admin.discovery.converter.management-context-pathWill 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-endpointWill 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-servicesThis 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 定期地發(fā)送 Http Post 到 admin 提供自己的應(yīng)用信息。如果需要管理 loglevels 或 JMX-beans ,則要在依賴中添加 Jolokia ,使得 JMX-beans 也可以通過 http 訪問。

表格:Spring Boot Admin Client配置選項

Property nameDescriptionDefault value中文說明
spring.boot.admin.client.enabledEnables the Spring Boot Admin Client.true默認(rèn)開啟
spring.boot.admin.urlList of URLs of the Spring Boot Admin server to register at. This triggers the AutoConfiguration. Mandatory.admin server 的地址列表,此設(shè)置會觸發(fā)自動配置,必須
spring.boot.admin.api-pathHttp-path of registration endpoint at your admin server.“api/applications”注冊到 admin server 端點的 Http-path
spring.boot.admin.username spring.boot.admin.passwordUsername 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.periodInterval for repeating the registration (in ms).10.000重試注冊的間隔時間
spring.boot.admin.auto-registrationIf set to true the periodic task to register the application is automatically scheduled after the application is ready.true應(yīng)用啟動后自動執(zhí)行周期性的注冊任務(wù)
spring.boot.admin.auto-deregistrationSwitch to enable auto-deregistration at Spring Boot Admin server when context is closed.false當(dāng)應(yīng)用關(guān)閉時,自動取消注冊
spring.boot.admin.client.health-urlClient-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-urlClient-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-urlClient-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.nameName to register with.${spring.application.name} if set, “spring-boot-application” otherwise.注冊時的名字
spring.boot.admin.client.prefer-ipUse 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 添加到我的業(yè)務(wù)應(yīng)用中嗎?

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

該怎么自定義 UI ?

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

上述內(nèi)容就是Admin 怎么在Spring Boot中使用,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:Admin怎么在SpringBoot中使用
當(dāng)前地址:http://muchs.cn/article10/pieodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作動態(tài)網(wǎng)站、虛擬主機、云服務(wù)器、網(wǎng)站排名域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司