SpringBoot+actuator和admin-UI實現(xiàn)監(jiān)控中心方式
使用SpringBoot很久了,但是很少使用到SpringBoot的查看和監(jiān)控,將來八成也不會用到,萬一有機會用到呢?
所以記錄一下以前學(xué)習(xí)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>
導(dǎo)入對應(yīng)的包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.0.5.RELEASE</version> </dependency> <!-- security 一旦導(dǎo)入就會生效 --> <!-- <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)錯誤,只有運行的時候才報錯
當(dāng)訪問
http://localhost:8081/sys/actuator/health
使用adminUI的方式 client客戶端導(dǎo)包和配置
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ù)端導(dǎo)包和配置
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 Web監(jiān)聽器如何實現(xiàn)定時發(fā)送郵件
這篇文章主要介紹了Java Web監(jiān)聽器如何實現(xiàn)定時發(fā)送郵件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12Spring Boot環(huán)境屬性占位符解析及類型轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot環(huán)境屬性占位符解析及類型轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Java中的線程安全集合CopyOnWriteArrayList解析
這篇文章主要介紹了Java中的線程安全CopyOnWriteArrayList解析,CopyOnWriteArrayList是ArrayList的線程安全版本,從他的名字可以推測,CopyOnWriteArrayList是在有寫操作的時候會copy一份數(shù)據(jù),然后寫完再設(shè)置成新的數(shù)據(jù),需要的朋友可以參考下2023-12-12Java中使用jaxp進行sax解析_動力節(jié)點Java學(xué)院整理
使用SAX的優(yōu)勢在于其解析速度較快,相對于DOM而言占用內(nèi)存較少。這篇文章主要介紹了Java中使用jaxp進行sax解析,需要的朋友可以參考下2017-08-08