Springboot輕量級(jí)的監(jiān)控組件SpringbootAdmin
簡介
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è)簡單的接口過濾器實(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)文章
Spring的CorsFilter會(huì)失效的原因及解決方法
眾所周知CorsFilter是Spring提供的跨域過濾器,我們可能會(huì)做以下的配置,基本上就是允許任何跨域請(qǐng)求,我利用Spring的CorsFilter做跨域操作但是出現(xiàn)報(bào)錯(cuò),接下來小編就給大家介紹一Spring的CorsFilter會(huì)失效的原因及解決方法,需要的朋友可以參考下2023-09-09
SpringMVC框架實(shí)現(xiàn)上傳圖片的示例代碼
本篇文章主要介紹了SpringMVC框架實(shí)現(xiàn)上傳圖片的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
mybatis-plus使用@EnumValue處理枚舉類型的示例代碼
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring中@PropertySource的使用方法和運(yùn)行原理詳解
這篇文章主要介紹了Spring中@PropertySource的使用方法和運(yùn)行原理詳解,PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment?Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下2023-11-11
詳細(xì)聊聊SpringBoot中動(dòng)態(tài)切換數(shù)據(jù)源的方法
在大型分布式項(xiàng)目中,經(jīng)常會(huì)出現(xiàn)多數(shù)據(jù)源的情況,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中動(dòng)態(tài)切換數(shù)據(jù)源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
Java中實(shí)現(xiàn)雙數(shù)組Trie樹實(shí)例
這篇文章主要介紹了Java中實(shí)現(xiàn)雙數(shù)組Trie樹實(shí)例,雙數(shù)組Trie就是一種優(yōu)化了空間的Trie樹,本文給出了實(shí)現(xiàn)代碼、測試代碼和測試結(jié)果,需要的朋友可以參考下2015-01-01

