Spring Boot Admin的使用詳解(Actuator監(jiān)控接口)
第一部分 Spring Boot Admin 簡(jiǎn)介
- Spring Boot Admin用來管理和監(jiān)控Spring Boot應(yīng)用程序。
- 應(yīng)用程序向我們的Spring Boot Admin Client注冊(cè)(通過HTTP)或使用SpringCloud®(例如Eureka,Consul)發(fā)現(xiàn)。
- UI是Spring Boot Actuator端點(diǎn)上的Vue.js應(yīng)用程序。
Spring Boot Admin 是一個(gè)管理和監(jiān)控Spring Boot 應(yīng)用程序的開源軟件。每個(gè)應(yīng)用都認(rèn)為是一個(gè)客戶端,通過HTTP或者使用 Eureka注冊(cè)到admin server中進(jìn)行展示,Spring Boot Admin UI部分使用AngularJs將數(shù)據(jù)展示在前端。
Spring Boot Admin 是一個(gè)針對(duì)spring-boot的actuator接口進(jìn)行UI美化封裝的監(jiān)控工具。他可以:在列表中瀏覽所有被監(jiān)控spring-boot項(xiàng)目的基本信息,詳細(xì)的Health信息、內(nèi)存信息、JVM信息、垃圾回收信息、各種配置信息(比如數(shù)據(jù)源、緩存列表和命中率)等,還可以直接修改logger的level。
admin-server 服務(wù)端(admin-server)
服務(wù)端:是指Spring Boot Admin這個(gè)應(yīng)用(通常就是指監(jiān)控服務(wù)器),一個(gè)服務(wù)端可以監(jiān)控多個(gè)客戶端。
客戶端
客戶端是:被服務(wù)端監(jiān)控的對(duì)象(通常就是指你的業(yè)務(wù)系統(tǒng))。
第二部分 快速入門
本部分將為您展示SpringBoot ADMIN 的簡(jiǎn)單應(yīng)用。
服務(wù)端配置(admin-server)
步驟一:搭建springboot maven項(xiàng)目
搭建一個(gè)基于SpringBoot的項(xiàng)目。注意您所使用的SpringBoot版本。
步驟二:配置pom.xml文件
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency>
步驟三:application.properties中配置端口號(hào)
此端口號(hào)指的是你所搭建的服務(wù)器所使用的的版本號(hào),如果服務(wù)端和客戶端在同一臺(tái)機(jī)器上,注意端口號(hào)的設(shè)置,以防端口出現(xiàn)沖突的情況。
server.port=8099
步驟四:主啟動(dòng)類上加注解@EnableAdminServer
@SpringBootApplication @EnableAdminServer public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
步驟五:?jiǎn)?dòng)項(xiàng)目
訪問:http://127.0.0.1:8099/applications。監(jiān)控首頁顯示如下
客戶端配置(admin-client)
步驟一:在客戶端項(xiàng)目(也就是需要監(jiān)控的springboot項(xiàng)目)中添加jar包
加入Security安全框架的jar包,加入jar需注意版本的問題。有些springboot版本,可能會(huì)自動(dòng)引入失>敗。如圖:
出現(xiàn)這種情況需指定security的版本號(hào),找個(gè)適合你springboot版本的security。
具體如下:
<!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.4.1</version> </dependency>
引入 spring-boot-admin-starter-client
<!--admin server 監(jiān)控--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.4.1</version> </dependency>
步驟二:在啟動(dòng)配置文件中配置如下 application.properties
#開放端點(diǎn)用于SpringBoot Admin的監(jiān)控 management.endpoints.web.exposure.include=* # 給client應(yīng)用取個(gè)名字 spring.boot.admin.client.instance.name=zxfdemo #這里配置admin server 的地址 spring.boot.admin.client.url=http://localhost:8099 #這里配置admin client 的地址(客戶端應(yīng)用程序) spring.boot.admin.client.instance.service-url=http://localhost:8080
步驟四:測(cè)試效果
spring security 安全加固
SpringBoot Admin的管理后臺(tái)如果沒密碼就能訪問,那實(shí)在太不安全了,所以需要引入一個(gè)安全加固的jar包。spring-boot-starter-security
Spring Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架。提供了完善的認(rèn)證機(jī)制和方法級(jí)的授權(quán)功能。是一款非常優(yōu)秀的權(quán)限管理框架。它的核心是一組過濾器鏈,不同的功能經(jīng)由不同的過濾器。此處就是想通過一個(gè)小案例將Spring Security整合到SpringBoot中去。要實(shí)現(xiàn)的功能就是在認(rèn)證服務(wù)器上登錄,然后獲取Token,再訪問資源服務(wù)器中的資源。
服務(wù)端配置(admin-server)
服務(wù)端配置修改
1. 服務(wù)端添加Spring Security 相關(guān)依賴
添加Spring Security 相關(guān)依賴
<!-- security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.4.1</version> </dependency>
2. 服務(wù)端設(shè)置賬號(hào)密碼
spring.security.user.name=zxf spring.security.user.password=123456
3.添加一個(gè)Spring Security 配置類
package com.example.springadmintest.config; import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; /** * 配置security驗(yàn)證頁面指向SpringBootAdmin提供的UI界面 * * */ @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String contextPath; public SecuritySecureConfig(AdminServerProperties adminServerProperties) { this.contextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { // 跨域設(shè)置,SpringBootAdmin客戶端通過instances注冊(cè),見InstancesController http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers(contextPath + "/instances"); http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 靜態(tài)資源 http.authorizeRequests().anyRequest().authenticated(); // 所有請(qǐng)求必須通過認(rèn)證 // 整合spring-boot-admin-server-ui http.formLogin().loginPage("/login").permitAll(); http.logout().logoutUrl("/logout").logoutSuccessUrl("/login"); // 啟用basic認(rèn)證,SpringBootAdmin客戶端使用的是basic認(rèn)證 http.httpBasic(); } }
4.登錄頁面展示
再次訪問http://localhost:8099/ ,發(fā)現(xiàn)需要登錄
客戶端配置(admin-client)
客戶端配置
1.客戶端添加Spring Security 相關(guān)依賴
<!-- security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.4.1</version> </dependency>
2. 客戶端設(shè)置賬號(hào)密碼
# 配置 admin-client 地址 spring.boot.admin.client.instance.service-url=http://localhost:8080 #配置 admin-server地址 spring.boot.admin.client.url=http://localhost:8099 # 配置 admin-server的賬號(hào) spring.boot.admin.client.username=zxf # 配置 admin-server的密碼 spring.boot.admin.client.password=123456 #配置 admin-server的密碼 spring.security.user.name=zxf #配置 admin-client的密碼 spring.security.user.password=123456 #若在核心配置文件中未添加 management.security.enabled=false 配置, # 將會(huì)導(dǎo)致用戶在訪問部分監(jiān)控地址時(shí)訪問受限,報(bào)401未授權(quán)錯(cuò)誤。 management.security.enabled=false #監(jiān)控中心配置, 允許監(jiān)控所有接口 management.endpoints.web.exposure.include=*
3. 客戶端添加Spring Security 配置類
package com.cachedemo.controller; 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 public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().permitAll() .and().csrf().disable(); } }
所有配置完成測(cè)試結(jié)果
到此這篇關(guān)于Spring Boot Admin的使用詳解(Actuator監(jiān)控接口)的文章就介紹到這了,更多相關(guān)Spring Boot Admin的使用 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot Admin的簡(jiǎn)單使用的方法步驟
- Spring Boot集群管理工具KafkaAdminClient使用方法解析
- 詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序
- 使用spring-boot-admin對(duì)spring-boot服務(wù)進(jìn)行監(jiān)控的實(shí)現(xiàn)方法
- SpringBoot Admin 使用指南(推薦)
- 詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)
- Spring Boot Admin 的使用詳解
- Spring Boot Admin 環(huán)境搭建與基本使用詳解
相關(guān)文章
教你怎么用java一鍵自動(dòng)生成數(shù)據(jù)庫文檔
最近小編也在找這樣的插件,就是不想寫文檔了,浪費(fèi)時(shí)間和心情啊,果然我找到一款比較好用,操作簡(jiǎn)單不復(fù)雜.screw 是一個(gè)簡(jiǎn)潔好用的數(shù)據(jù)庫表結(jié)構(gòu)文檔的生成工具,支持 MySQL、Oracle、PostgreSQL 等主流的關(guān)系數(shù)據(jù)庫.需要的朋友可以參考下2021-05-05詳解Spring與Mybatis整合方法(基于IDEA中的Maven整合)
這篇文章主要介紹了Spring與Mybatis整合方法(基于IDEA中的Maven整合),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10解決Eclipse中java文件的圖標(biāo)變成空心J的問題
這篇文章主要介紹了解決Eclipse中java文件的圖標(biāo)變成空心J的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01解決redisTemplate向redis中插入String類型數(shù)據(jù)時(shí)出現(xiàn)亂碼問題
這篇文章主要介紹了解決redisTemplate向redis中插入String類型數(shù)據(jù)時(shí)出現(xiàn)亂碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12java樹結(jié)構(gòu)stream工具類的示例代碼詳解
Stream 作為 Java 8 的一大亮點(diǎn),它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念。今天通過本文重點(diǎn)給大家介紹java樹結(jié)構(gòu)stream工具類的示例代碼,感興趣的朋友一起看看吧2022-03-03如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)
這篇文章主要給大家介紹了關(guān)于如何利用Java輸出鏈表中倒數(shù)第k個(gè)結(jié)點(diǎn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-12-12springboot配置mybatis和事務(wù)管理方式
這篇文章主要介紹了springboot配置mybatis和事務(wù)管理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04