使用Spirng Boot Admin監(jiān)控Spring Cloud應(yīng)用項(xiàng)目
一. 介紹
GitHub: https://github.com/codecentric/spring-boot-admin
官方文檔: http://codecentric.github.io/spring-boot-admin/1.5.7/ (此文檔為1.5.7版本的文檔)
The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul).
官方文檔中介紹中提到,使用Spring Boot Admin監(jiān)控,需要一個(gè)Spring Boot Admin Server服務(wù),其他被監(jiān)控的服務(wù)即為一個(gè)Spring Boot Admin Client,或者是通過(guò)Spring Cloud中的服務(wù)注冊(cè)發(fā)現(xiàn)組件如:Eureak,Consul來(lái)進(jìn)行監(jiān)控服務(wù),這樣就不需要特意設(shè)置相關(guān)被監(jiān)控的服務(wù)作為一個(gè)client,只需注冊(cè)到Eureka或者Consul中即可。
注:本文是基于Spring Cloud Eureka做為服務(wù)注冊(cè)發(fā)現(xiàn)組件來(lái)實(shí)現(xiàn)對(duì)服務(wù)的監(jiān)控。提前是存在了Eureka的服務(wù)。 另:沒有使用eureka的需要在被監(jiān)控服務(wù)中引入client的jar,具體情況請(qǐng)參考上面的官方文檔。*
二.創(chuàng)建Spring Boot Admin Server服務(wù)
首先創(chuàng)建一個(gè)Spirng Boot項(xiàng)目,可以通過(guò) http://start.spring.io/ 快速搭建。
1、添加Spring Boot Admin Server 依賴
pom.xml
<dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>1.5.7</version> </dependency> </dependencies>
2、服務(wù)啟動(dòng)類添加 @EnableAdminServer 注解開啟監(jiān)控
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
3、將Spring Boot Admin Server 注冊(cè)到Eureka
添加Eureka依賴
pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
服務(wù)啟動(dòng)類添加 @EnableDiscoveryClient 開啟服務(wù)發(fā)現(xiàn)
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
添加Eureka服務(wù)注冊(cè)配置
eureka: instance: leaseRenewalIntervalInSeconds: 10 client: registryFetchIntervalSeconds: 5 serviceUrl: defaultZone: http://10.1.3.54:8761/eureka/ # 關(guān)閉spring boot actuator的安全,否則敏感路徑訪問(wèn)是401 management: security: enabled: false
啟動(dòng)項(xiàng)目,訪問(wèn)ip:port 即進(jìn)入管理的頁(yè)面
如圖:

圖中顯示的這些項(xiàng)目都是已經(jīng)注冊(cè)到Eureka上的服務(wù),通過(guò)將Spring Boot Admin Server注冊(cè)到Eureka上來(lái)監(jiān)控項(xiàng)目。
三.配置設(shè)置
1、登錄UI
Admin管理服務(wù)沒有任何的安全措施,任何人知道ip和port就能訪問(wèn),這就很不安全,而Spring Boot Admin也提供了登錄頁(yè)面。需要和Spring Security 配合使用。
添加Spring Boot Admin UI依賴:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-login</artifactId> <version>1.5.7</version> </dependency>
添加Spring Security依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
在項(xiàng)目啟動(dòng)類中添加配置代碼:
package com.aspire.springbootadmin;
import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringbootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminApplication.class, args);
}
@Configuration
public static class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// Page with login form is served as /login.html and does a POST on /login
http.formLogin().loginPage("/login.html").loginProcessingUrl("/login").permitAll();
// The UI does a POST on /logout on logout
http.logout().logoutUrl("/logout");
// The ui currently doesn't support csrf
http.csrf().disable();
// Requests for the login page and the static assets are allowed
http.authorizeRequests()
.antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**")
.permitAll();
// ... and any other request needs to be authorized
http.authorizeRequests().antMatchers("/**").authenticated();
// Enable so that the clients can authenticate via HTTP basic for registering
http.httpBasic();
}
}
}
添加配置文件
security: user: name: admin password: 123123 basic: enabled: false
再次訪問(wèn)會(huì)出現(xiàn)一個(gè)登陸頁(yè)面

輸入配置中配置的用戶名和密碼點(diǎn)擊Login登錄。進(jìn)入后導(dǎo)航欄上也會(huì)多出一個(gè)退出按鈕。點(diǎn)擊后即返回到登錄頁(yè)面。

2、Client應(yīng)用的版本信息
想要在Application列表中顯示版本,使用maven的 build-info 插件,在pom.xml文件添加插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如圖:沒有添加的就沒有顯示。

3、Client應(yīng)用JMX bean管理
要在管理界面中與JMX-beans進(jìn)行交互,您必須在應(yīng)用程序中包含 Jolokia 。 添加pom依賴:
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
添加后在監(jiān)控的應(yīng)用detial菜單里就可以看到JMX菜單

4、Client應(yīng)用添加日志Log
在Client應(yīng)用的properties中配置如下:
logging: path: /xxx/xxx/xxx/
指定日志輸出路徑,然后就可以顯示日志了:

5、hystrix ui支持
spring boot admin還可以集成hystrix展示。 前提是Client端需要開啟hystrix才可以。
在Spring Boot Admin Server 添加依賴
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-hystrix</artifactId> <version>1.4.6</version> </dependency>
在Server的配置文件中添加endpoints節(jié)點(diǎn)
點(diǎn)擊開啟了Hystrix的Client,點(diǎn)擊Hystrix菜單既能看到Hystrix信息

6.Turbine UI 支持
spring boot admin還可以集成turbine展示。
加入依賴:
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-server-ui-turbine</artifactId> <version>1.5.7</version> </dependency>
配置turbine的cluster和location,clusters為turbin服務(wù)中配置的clusters一致,location需要指定注冊(cè)到Eureka中的Turbine服務(wù)名稱,例如你的Turbine服務(wù)名稱為TURBINE-SERVER,配置就為下所示。(其實(shí)Spring Boot Admin Server本身就可以當(dāng)做Turbine服務(wù),如果之前就存在了Turbine服務(wù)的話就可以直接在這里配置。若不存在Turbine服務(wù),就在Spring Boot Admin Server添加Turbine相關(guān)依賴和配置,這樣Spring Boot Admin Server也就成了Turbine服務(wù)。)
spring.boot.admin.turbine: clusters: default location: TURBINE-SERVER
最后在配置文件中添加turbine.stream的endpoint
界面
導(dǎo)航欄中會(huì)多出TURBINE的Tab,點(diǎn)擊就能看到Turbine信息

7.郵件通知
Spring Boot Admin 同時(shí)支持郵件通知。
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
添加Spring郵件配置(這里記一個(gè)小坑,開始使用的是qq郵箱,結(jié)果死活連不上qq郵箱的smtp服務(wù)器,查了下原因是騰訊那邊把公司內(nèi)網(wǎng)ip給禁止了,結(jié)果連上4G網(wǎng)就沒有問(wèn)題了。)
spring: mail: host: mmmail.aspire-tech.com password: xxxxx port: 25 username: xxxx
添加Spring Boot Admin郵件配置
boot: admin: notify: mail: to: xxxx@aspirecn.com from: chufule@aspirecn.com enabled: true
效果如圖,監(jiān)控的服務(wù)啟動(dòng),停止都會(huì)有郵件通知。


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合websocket實(shí)現(xiàn)群聊思路代碼詳解
通過(guò)springboot引入websocket,實(shí)現(xiàn)群聊,通過(guò)在線websocket測(cè)試進(jìn)行展示。本文重點(diǎn)給大家介紹springboot整合websocket實(shí)現(xiàn)群聊功能,代碼超級(jí)簡(jiǎn)單,感興趣的朋友跟隨小編一起學(xué)習(xí)吧2021-05-05
SpringBoot整合jnotify實(shí)現(xiàn)針對(duì)指定目錄及其(動(dòng)態(tài))子目錄的監(jiān)聽的方法
本文介紹了JNotify這一Java庫(kù)在SpringBoot中的應(yīng)用,JNotify允許應(yīng)用程序監(jiān)聽文件系統(tǒng)事件,包括文件夾/文件的創(chuàng)建、刪除、修改和重命名,由于JNotify底層調(diào)用的關(guān)鍵部分是C語(yǔ)言開發(fā)的,所以在使用前需要在系統(tǒng)中加入相應(yīng)的動(dòng)態(tài)庫(kù)2024-10-10
quartz定時(shí)執(zhí)行任務(wù),并配置web.xml的操作方法
下面小編就為大家?guī)?lái)一篇quartz定時(shí)執(zhí)行任務(wù),并配置web.xml的操作方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
使用idea開發(fā)javaWeb應(yīng)用程序的思路(實(shí)現(xiàn)用戶的增刪改查)
這篇文章主要介紹了使用idea開發(fā)javaWeb應(yīng)用程序的思路(實(shí)現(xiàn)用戶的增刪改查),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
java實(shí)現(xiàn)簡(jiǎn)易版簡(jiǎn)易版dubbo
dubbo是阿里開源的rpc框架,目前是apache頂級(jí)開源項(xiàng)目,可以用來(lái)構(gòu)建微服務(wù)。本文主要介紹了如何通過(guò)java實(shí)現(xiàn)簡(jiǎn)易版的dubbo,感興趣的小伙伴可以了解一下2021-11-11
springboot的緩存技術(shù)的實(shí)現(xiàn)
這篇文章主要介紹了springboot的緩存技術(shù)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Jenkins Host key verification failed問(wèn)題解決
這篇文章主要介紹了Jenkins Host key verification failed問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Idea設(shè)置全局highlighting?level為Syntax問(wèn)題
這篇文章主要介紹了Idea設(shè)置全局highlighting?level為Syntax問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

