javascript
SpringBoot笔记:SpringBoot集成SpringbootAdmin监控
文章目錄
- SpringBootAdmin是什么
- 接入配置
- server端配置
- client端配置
- 測(cè)試效果
SpringBootAdmin是什么
Spring Boot Admin 是一個(gè)管理和監(jiān)控 Spring Boot 應(yīng)用程序的開(kāi)源軟件,它是在 Spring Boot Actuator 的基礎(chǔ)上提供簡(jiǎn)潔的可視化 WEB UI。Spring Boot Actuator 是Springboot體系中非常好用且強(qiáng)大的監(jiān)控能力節(jié)點(diǎn),極大的方便了我們對(duì)springboot應(yīng)用進(jìn)行業(yè)務(wù)監(jiān)控。但是,Spring Boot Actuator 監(jiān)控接口返回的都是json數(shù)據(jù),需要我們進(jìn)一步開(kāi)發(fā)自己的監(jiān)控系統(tǒng)。Spring Boot Admin 就是這樣一個(gè)系統(tǒng)。
Spring Boot Admin并不是 Spring Boot 官方出品的開(kāi)源軟件,但是其軟件質(zhì)量和使用廣泛度都非常的高,并且 Spring Boot Admin 會(huì)及時(shí)隨著 Spring Boot 的更新而更新,當(dāng) Spring Boot 推出 2.X 版本時(shí) Spring Boot Admin 也及時(shí)進(jìn)行了更新。
它可以在列表中瀏覽所有被監(jiān)控 spring-boot 項(xiàng)目的基本信息、詳細(xì)的 Health 信息、內(nèi)存信息、JVM 信息、垃圾回收信息、各種配置信息(比如數(shù)據(jù)源、緩存列表和命中率)等,還可以直接修改 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的時(shí)候進(jìn)行安全認(rèn)證 --><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多環(huán)境配置 #端口,項(xiàng)目上下文 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();} }最后啟動(dòng)程序入口配置:
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>增加核心配置,其他省略了
# 默認(rèn)啟動(dòng)的是測(cè)試環(huán)境配置 spring:profiles:active: test# 客戶端client注冊(cè)到spring admin上server服務(wù)上application:name: springboot-params-demoboot:admin:client:# springboot admin server的注冊(cè)地址,多個(gè)以逗號(hào)隔開(kāi),并把localhost換成ipurl: http://hadoop-master:8000/springboot-adminusername: adminpassword: admin@2021 # 需要暴露監(jiān)控端口給springboot admin server 訪問(wèn) management:endpoints:web:exposure:include: '*'這樣已經(jīng)對(duì)接完成了,開(kāi)始測(cè)試
測(cè)試效果
springboot Admin server登錄地址:http://hadoop-master:8000/springboot-admin
先啟動(dòng)server端,然后再啟動(dòng)client端
總結(jié)
以上是生活随笔為你收集整理的SpringBoot笔记:SpringBoot集成SpringbootAdmin监控的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot笔记:SpringB
- 下一篇: CentOS7安装笔记:minio分布式