SpringBoot+actuator和admin-UI實現(xiàn)監(jiān)控中心方式
使用SpringBoot很久了,但是很少使用到SpringBoot的查看和監(jiān)控,將來八成也不會用到,萬一有機會用到呢?
所以記錄一下以前學習SpringBoot+actuator和adminUI實現(xiàn)監(jiān)控中心的方式
Springboot的版本2.0.x
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
導入對應(yīng)的包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.5.RELEASE</version> </dependency> <!-- security 一旦導入就會生效 --> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
application.properties
server.port=7080 # 配置用戶名和密碼 #spring.security.user.name=admin #spring.security.user.password=123456 # 端點信息配置 management.server.port=8081 management.server.servlet.context-path=/sys # 默認 never always可以顯示硬盤使用情況和線程情況 management.endpoint.health.show-details=always # 端點暴露的內(nèi)容,默認["health","info"],設(shè)置"*"代表暴露所有可訪問的端點 management.endpoints.web.exposure.include=* # actuator 信息 info.actuator.name=test
啟動之后

訪問
http://localhost:8081/sys/actuator

在這里使用的Actuator是spring boot的一個附加功能,可幫助你在應(yīng)用程序生產(chǎn)環(huán)境時監(jiān)視和管理應(yīng)用程序。
可以使用HTTP的各種請求來監(jiān)管,審計,收集應(yīng)用的運行情況.特別對于微服務(wù)管理十分有意義.
缺點:沒有可視化界面
使用場景,針對微服務(wù)的服務(wù)狀態(tài)監(jiān)控,服務(wù)器的內(nèi)存變化(堆內(nèi)存,線程,日志管理等),檢測服務(wù)配置連接地址是否可用(模擬訪問,懶加載),統(tǒng)計現(xiàn)在有多少個bean(Spring容器中的bean) 統(tǒng)計接口數(shù)量,
應(yīng)用場景:生產(chǎn)環(huán)境
- /actuator/beans 顯示應(yīng)用程序中所有Spring bean的完整列表
- /actuator/configprops 顯示所有配置信息
- /actuator/env 陳列所有的環(huán)境變量
- /actuator/mappings 顯示所有@RequestMapping的url整理列表
- /actuator/health 顯示應(yīng)用程序運行狀況信息 up表示成功 down失敗,對于懶加載沒報錯的可以看到控制臺報錯了
- /actuator/info 查看自定義應(yīng)用信息
懶加載有個缺點,例如mysql的配置,啟動的時候不會發(fā)現(xiàn)錯誤,只有運行的時候才報錯
當訪問
http://localhost:8081/sys/actuator/health

使用adminUI的方式 client客戶端導包和配置
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.5.RELEASE</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.0.5</version> </dependency>
application.properties
server.port=8071 spring.application.name=boot-example-admin-client spring.boot.admin.client.url=http://127.0.0.1:8050/myw-admin spring.boot.admin.client.username=admin spring.boot.admin.client.password=123456 spring.boot.admin.client.instance.service-url=http://127.0.0.1:8071 #management.server.port=8081 #management.server.servlet.context-path=/sys # 默認 never always可以顯示硬盤使用情況和線程情況 management.endpoint.health.show-details=always # 端點暴露的內(nèi)容,默認["health","info"],設(shè)置"*"代表暴露所有可訪問的端點 management.endpoints.web.exposure.include=*
使用admin-ui的方式 server服務(wù)端導包和配置
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.5</version> </dependency>
application.properties
server.port=8050 server.servlet.context-path=/myw-admin spring.application.name=boot-example-admin-server spring.security.user.name=admin spring.security.user.password=123456
WebSecurityConfig.java
package boot.example.admin.server;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
/**
* SpringBootAdmin 登錄鑒權(quán)使用
*
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final String contextPath;
public WebSecurityConfig(AdminServerProperties adminServerProperties) {
this.contextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// 跨域設(shè)置,SpringBootAdmin客戶端通過instances注冊,見InstancesController
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(contextPath + "/instances");
http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 靜態(tài)資源
http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身監(jiān)控
http.authorizeRequests().anyRequest().authenticated(); // 所有請求必須通過認證
// 整合spring-boot-admin-server-ui
http.formLogin().loginPage("/login").permitAll();
http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
// 啟用basic認證,SpringBootAdmin客戶端使用的是basic認證
http.httpBasic();
}
}
啟動客戶端和服務(wù)端的監(jiān)控中心
http://localhost:8050/myw-admin/login#/applications



記錄一下在SpringBoot2.6.6版本使用
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent>
client的pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.6.6</version> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.5.6</version> </dependency>
application.properties 1
server.port=8071 spring.application.name=boot-example-admin-client1 spring.boot.admin.client.url=http://localhost:8050 spring.boot.admin.client.username=admin spring.boot.admin.client.password=123456 management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
application.properties 2
server.port=8072 spring.application.name=boot-example-admin-client2 spring.boot.admin.client.url=http://localhost:8050 spring.boot.admin.client.username=admin spring.boot.admin.client.password=123456 management.server.port=8082 management.server.base-path = /sys management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
application.properties 3
server.port=8073 spring.application.name=boot-example-admin-client3 spring.boot.admin.client.url=http://localhost:8050 spring.boot.admin.client.username=admin spring.boot.admin.client.password=123456 #spring.security.user.name=admin #spring.security.user.password=123456 management.server.port=8083 management.server.base-path = /sys management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
服務(wù)端的配置
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.5.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
application.properties
server.port=8050 spring.application.name=boot-example-admin-server spring.security.user.name=admin spring.security.user.password=123456
總結(jié)
備注記錄到這里 將來會不會用到再說了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot應(yīng)用監(jiān)控Actuator使用隱患及解決方案
- SpringBoot Actuator未授權(quán)訪問漏洞的排查和解決方法
- Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
- SpringBoot中的Actuator詳解
- SpringBoot Actuator未授權(quán)訪問漏洞解決方案
- 關(guān)于SpringBoot Actuator漏洞補救方案
- SpringBoot監(jiān)控模塊Actuator的用法詳解
- SpringBoot Actuator未授權(quán)訪問漏洞修復(fù)詳解
- Spring Boot Actuator入門指南
相關(guān)文章
詳解Java阻塞隊列(BlockingQueue)的實現(xiàn)原理
這篇文章主要介紹了詳解Java阻塞隊列(BlockingQueue)的實現(xiàn)原理,阻塞隊列是Java util.concurrent包下重要的數(shù)據(jù)結(jié)構(gòu),有興趣的可以了解一下2017-06-06
SpringBoot后端接收多個文件多種實現(xiàn)方法
SpringBoot提供了簡單的方式來實現(xiàn)文件接收功能,可使用MultipartFile來接收上傳的文件,并將其存儲在服務(wù)器的指定位置,在SpringBoot中接收多個文件有多種方式,本文將詳細介紹各種方法及其實現(xiàn),需要的朋友可以參考下2025-09-09
SpringBoot返回對象時,如何將Long類型轉(zhuǎn)換為String
這篇文章主要介紹了SpringBoot返回對象時,實現(xiàn)將Long類型轉(zhuǎn)換為String,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
基于Spring Boot的Environment源碼理解實現(xiàn)分散配置詳解
這篇文章主要給大家介紹了基于Spring Boot的Environment源碼理解實現(xiàn)分散配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08
sentinel整合ribbon與fallback流程分步講解
這篇文章主要介紹了sentinel整合ribbon與fallback分步流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
Java畢業(yè)設(shè)計實戰(zhàn)項目之寵物商城系統(tǒng)的實現(xiàn)流程
這是一個使用了java+Springboot+Maven+mybatis+Vue+mysql開發(fā)的寵物商城系統(tǒng),是一個畢業(yè)設(shè)計的實戰(zhàn)練習,具有寵物商城該有的所有功能,感興趣的朋友快來看看吧2022-01-01
詳解Java?ThreadPoolExecutor的拒絕策略
這篇文章主要介紹了Java?ThreadPoolExecutor的拒絕策略,本文對于線程的池的幾種策略進行詳細的講解,在實際的生產(chǎn)中需要集合相關(guān)的場景來選擇合適的拒絕策略,需要的朋友可以參考下2022-08-08
spring boot hutool整合email的詳細過程
這篇文章主要介紹了spring boot hutool整合email的相關(guān)知識,本文介紹兩種方式發(fā)送email文件,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-03-03

