Springboot輕量級的監(jiān)控組件SpringbootAdmin
簡介
Springboot Admin是一個管理和監(jiān)控Springboot項目的組件,分為服務端和客戶端,兩端通過http進行通信。由于其輕量級的特性,所以特別適合中小項目使用。
其效果圖如下:
服務端配置
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,配置相關屬性。
server: port: 8080 servlet: context-path: /server spring: security: user: #admin Server端登錄時用的賬戶密碼 name: server123 password: 123456 boot: admin: instance-auth: #啟用header驗證 enabled: true #Server訪問client接口時會使用下面的配置生成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); } }
經過以上3步,服務端就可以啟動了。
訪問 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地址。
客戶端在啟動后會向配置的Server發(fā)起注冊申請,此時為了安全性還需要Server端的賬戶密碼進行校驗。
spring: boot: admin: client: #admin注冊地址 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,對暴露的接口進行權限校驗。
由于我們將監(jiān)控接口進行了暴露,所以必須對相關的接口進行權限校驗,否則就有可能泄露相關信息。
對接口進行權限過濾有很多種選擇,比如設置IP訪問的白名單,只允許admin Server所在的服務器訪問,也可以配置相關的token等等。
下面我們以一個簡單的接口過濾器實現對/actuator/**相關接口的權限校驗。
@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("權限不足"); return; } //其他接口直接放行 filterChain.doFilter(request, servletResponse); } }
在這個filter中,對actuator相關的接口進行了header參數的校驗,只有通過校驗才可以訪問暴露出的actuator接口。
當然,如果我們使用了SpringSecurity或者SaToken這樣的第三方權限框架,也可以去重寫相關的配置完成權限的判斷,原理都是一樣的。
下面我們看一下最終的監(jiān)控效果:
最后
除了通過普通http請求方式獲取監(jiān)控信息以外,Springboot admin還支持通過注冊中心的方式獲取相關信息,在其官方文檔大家也可以看到相關的配置。
官方文檔:codecentric.github.io/spring-boot…
以上就是Springboot輕量級的監(jiān)控組件SpringbootAdmin的詳細內容,更多關于Springboot監(jiān)控組件的資料請關注腳本之家其它相關文章!
相關文章
spring boot 自動更新靜態(tài)文件和后臺代碼的實例
下面小編就為大家分享一篇spring boot 自動更新靜態(tài)文件和后臺代碼的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12使用vue3.x+vite+element-ui+vue-router+vuex+axios搭建項目
因為vue3出了一段時間了,element也出了基于vue3.x版本的element-plus,這篇文章就拿他們搭建一個項目,希望能給你帶來幫助2021-08-08springboot 集成cas5.3 實現sso單點登錄詳細流程
SSO的定義是在多個應用系統(tǒng)中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統(tǒng)。單點登錄是目前比較流行的企業(yè)業(yè)務整合的解決方案之一,本文給大家介紹springboot 集成cas5.3 實現sso單點登錄功能,感興趣的朋友一起看看吧2021-10-10詳解使用Spring?Data?repository進行數據層的訪問問題
這篇文章主要介紹了使用Spring?Data?repository進行數據層的訪問,抽象出Spring Data repository是因為在開發(fā)過程中,常常會為了實現不同持久化存儲的數據訪問層而寫大量的大同小異的代碼,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-06-06Eclipse 開發(fā)java 出現Failed to create the Java Virtual Machine錯誤
這篇文章主要介紹了Eclipse 開發(fā)java 出現Failed to create the Java Virtual Machine錯誤解決辦法的相關資料,需要的朋友可以參考下2017-04-04