Springboot輕量級(jí)的監(jiān)控組件SpringbootAdmin
簡(jiǎn)介
Springboot Admin是一個(gè)管理和監(jiān)控Springboot項(xiàng)目的組件,分為服務(wù)端和客戶端,兩端通過http進(jìn)行通信。由于其輕量級(jí)的特性,所以特別適合中小項(xiàng)目使用。
其效果圖如下:

服務(wù)端配置
1,引入Springboot admin和Spring Security依賴。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2,配置相關(guān)屬性。
server:
port: 8080
servlet:
context-path: /server
spring:
security:
user:
#admin Server端登錄時(shí)用的賬戶密碼
name: server123
password: 123456
boot:
admin:
instance-auth:
#啟用header驗(yàn)證
enabled: true
#Server訪問client接口時(shí)會(huì)使用下面的配置生成authorization
default-user-name: "name_shishan"
default-password: "pwd_shishan"
3,配置@EnableAdminServer注解
@SpringBootApplication
@Configuration
@EnableAdminServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
經(jīng)過以上3步,服務(wù)端就可以啟動(dòng)了。
訪問 http://localhost:8080/server/,就可以看到以下登錄界面。

使用在yml文件中配置的賬戶密碼就可以登錄了。

客戶端配置
1,在我們要監(jiān)控的客戶端中加入以下依賴。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.1</version>
</dependency>
2,暴露監(jiān)控接口以及配置Server地址。
客戶端在啟動(dòng)后會(huì)向配置的Server發(fā)起注冊(cè)申請(qǐng),此時(shí)為了安全性還需要Server端的賬戶密碼進(jìn)行校驗(yàn)。
spring:
boot:
admin:
client:
#admin注冊(cè)地址
url: http://localhost:8080/server
#配置admin的賬戶
username: server123
password: 123456
admin:
header:
auth:
name: "name_shishan"
password: "pwd_shishan"
#暴露出端口
management:
endpoints:
web:
exposure:
include: "*"
3,對(duì)暴露的接口進(jìn)行權(quán)限校驗(yàn)。
由于我們將監(jiān)控接口進(jìn)行了暴露,所以必須對(duì)相關(guān)的接口進(jìn)行權(quán)限校驗(yàn),否則就有可能泄露相關(guān)信息。
對(duì)接口進(jìn)行權(quán)限過濾有很多種選擇,比如設(shè)置IP訪問的白名單,只允許admin Server所在的服務(wù)器訪問,也可以配置相關(guān)的token等等。
下面我們以一個(gè)簡(jiǎn)單的接口過濾器實(shí)現(xiàn)對(duì)/actuator/**相關(guān)接口的權(quán)限校驗(yàn)。
@Component
public class PathFilter implements Filter {
@Value("${admin.header.auth.name}")
private String username;
@Value("${admin.header.auth.password}")
private String password;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
AntPathMatcher antPathMatcher = new AntPathMatcher();
if (antPathMatcher.match("/actuator/**", request.getServletPath())) {
String authorization = request.getHeader("authorization");
if (StringUtils.hasText(authorization)) {
String token = Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
if (authorization.equals("Basic " + token)) {
//token匹配才放行
filterChain.doFilter(request, servletResponse);
return;
}
}
response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.getWriter().print("權(quán)限不足");
return;
}
//其他接口直接放行
filterChain.doFilter(request, servletResponse);
}
}
在這個(gè)filter中,對(duì)actuator相關(guān)的接口進(jìn)行了header參數(shù)的校驗(yàn),只有通過校驗(yàn)才可以訪問暴露出的actuator接口。
當(dāng)然,如果我們使用了SpringSecurity或者SaToken這樣的第三方權(quán)限框架,也可以去重寫相關(guān)的配置完成權(quán)限的判斷,原理都是一樣的。
下面我們看一下最終的監(jiān)控效果:




最后
除了通過普通http請(qǐng)求方式獲取監(jiān)控信息以外,Springboot admin還支持通過注冊(cè)中心的方式獲取相關(guān)信息,在其官方文檔大家也可以看到相關(guān)的配置。
官方文檔:codecentric.github.io/spring-boot…
以上就是Springboot輕量級(jí)的監(jiān)控組件SpringbootAdmin的詳細(xì)內(nèi)容,更多關(guān)于Springboot監(jiān)控組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
IDEA 自動(dòng)生成 JPA 實(shí)體類的圖文教程
這篇文章主要介紹了IDEA 自動(dòng)生成 JPA 實(shí)體類的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
spring boot 自動(dòng)更新靜態(tài)文件和后臺(tái)代碼的實(shí)例
下面小編就為大家分享一篇spring boot 自動(dòng)更新靜態(tài)文件和后臺(tái)代碼的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
使用vue3.x+vite+element-ui+vue-router+vuex+axios搭建項(xiàng)目
因?yàn)関ue3出了一段時(shí)間了,element也出了基于vue3.x版本的element-plus,這篇文章就拿他們搭建一個(gè)項(xiàng)目,希望能給你帶來幫助2021-08-08
java小程序之控制臺(tái)字符動(dòng)畫的實(shí)現(xiàn)
這篇文章主要給大家介紹了java小程序之控制臺(tái)字符動(dòng)畫實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
詳解 Java中日期數(shù)據(jù)類型的處理之格式轉(zhuǎn)換的實(shí)例
這篇文章主要介紹了詳解 Java中日期數(shù)據(jù)類型的處理之格式轉(zhuǎn)換的實(shí)例的相關(guān)資料,日期以及時(shí)間格式處理,在Java中時(shí)間格式一般會(huì)涉及到的數(shù)據(jù)類型包括Calendar類和Date類,需要的朋友可以參考下2017-08-08
Java實(shí)現(xiàn)讀取文章中重復(fù)出現(xiàn)的中文字符串
本文主要介紹了Java實(shí)現(xiàn)讀取文章中重復(fù)出現(xiàn)的中文字符串的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
java設(shè)計(jì)模式學(xué)習(xí)之代理模式
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式學(xué)習(xí)之代理模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
springboot 集成cas5.3 實(shí)現(xiàn)sso單點(diǎn)登錄詳細(xì)流程
SSO的定義是在多個(gè)應(yīng)用系統(tǒng)中,用戶只需要登錄一次就可以訪問所有相互信任的應(yīng)用系統(tǒng)。單點(diǎn)登錄是目前比較流行的企業(yè)業(yè)務(wù)整合的解決方案之一,本文給大家介紹springboot 集成cas5.3 實(shí)現(xiàn)sso單點(diǎn)登錄功能,感興趣的朋友一起看看吧2021-10-10
詳解使用Spring?Data?repository進(jìn)行數(shù)據(jù)層的訪問問題
這篇文章主要介紹了使用Spring?Data?repository進(jìn)行數(shù)據(jù)層的訪問,抽象出Spring Data repository是因?yàn)樵陂_發(fā)過程中,常常會(huì)為了實(shí)現(xiàn)不同持久化存儲(chǔ)的數(shù)據(jù)訪問層而寫大量的大同小異的代碼,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤
這篇文章主要介紹了Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04

