javascript
SpringBoot笔记:SpringBoot集成SpringbootAdmin监控
文章目錄
- SpringBootAdmin是什么
- 接入配置
- server端配置
- client端配置
- 測試效果
SpringBootAdmin是什么
Spring Boot Admin 是一個管理和監控 Spring Boot 應用程序的開源軟件,它是在 Spring Boot Actuator 的基礎上提供簡潔的可視化 WEB UI。Spring Boot Actuator 是Springboot體系中非常好用且強大的監控能力節點,極大的方便了我們對springboot應用進行業務監控。但是,Spring Boot Actuator 監控接口返回的都是json數據,需要我們進一步開發自己的監控系統。Spring Boot Admin 就是這樣一個系統。
Spring Boot Admin并不是 Spring Boot 官方出品的開源軟件,但是其軟件質量和使用廣泛度都非常的高,并且 Spring Boot Admin 會及時隨著 Spring Boot 的更新而更新,當 Spring Boot 推出 2.X 版本時 Spring Boot Admin 也及時進行了更新。
它可以在列表中瀏覽所有被監控 spring-boot 項目的基本信息、詳細的 Health 信息、內存信息、JVM 信息、垃圾回收信息、各種配置信息(比如數據源、緩存列表和命中率)等,還可以直接修改 logger 的 level。
接入配置
server端配置
添加依賴,我本地使用的springboot版本為2.3.0.RELEASE
<!-- Spring Boot Admin 的server端 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId><version>2.2.2</version></dependency><!-- 引入springboot-security 登錄Spring oot Admin的時候進行安全認證 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>application.yml配置:
# springboot多環境配置 #端口,項目上下文 server:port: 8000servlet:context-path: /springboot-adminspring:security:user:name: 'admin'password: 'admin@2021'# 日志輸出配置 logging:level:root: INFOorg:springframework:security: WARNweb: ERRORfile:path: ./logsname: './logs/springboot-admin.log'pattern:file: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n'console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n'SecuritySecureConfig.java 配置
package com.demo.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.authentication.SavedRequestAwareAuthenticationSuccessHandler;@Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {/*** 定義上下文*/private final String adminContextPath;public SecuritySecureConfig(AdminServerProperties adminServerProperties) {this.adminContextPath = adminServerProperties.getContextPath();}@Overrideprotected void configure(HttpSecurity http) throws Exception {SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll().antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout().logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf().disable();} }最后啟動程序入口配置:
package com.demo;import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableAdminServer @SpringBootApplication public class SpringbootMonitorApplication {public static void main(String[] args) {SpringApplication.run(SpringbootMonitorApplication.class, args);} }client端配置
添加依賴
<!-- Spring Boot Admin 的client端 --><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>增加核心配置,其他省略了
# 默認啟動的是測試環境配置 spring:profiles:active: test# 客戶端client注冊到spring admin上server服務上application:name: springboot-params-demoboot:admin:client:# springboot admin server的注冊地址,多個以逗號隔開,并把localhost換成ipurl: http://hadoop-master:8000/springboot-adminusername: adminpassword: admin@2021 # 需要暴露監控端口給springboot admin server 訪問 management:endpoints:web:exposure:include: '*'這樣已經對接完成了,開始測試
測試效果
springboot Admin server登錄地址:http://hadoop-master:8000/springboot-admin
先啟動server端,然后再啟動client端
總結
以上是生活随笔為你收集整理的SpringBoot笔记:SpringBoot集成SpringbootAdmin监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot笔记:SpringB
- 下一篇: CentOS7安装笔记:minio分布式