前面的文章介绍过了spring家强大的服务监控管理系统spring boot actuator,但是好学的朋友会发现通过http restful api
的方式查看信息过于繁琐也不够直观,效率低下,运维人员看到JSON数据更是一脸懵逼,当服务过多的时候查看起来就过于操蛋了,每个服务都需要调用不同的接口来查看监控信息,各种困扰因素,这时spring-boot-admin就闪亮登场了。
SBA
SBA 全称 Spring Boot Admin
是一个管理和监控Spring Boot
应用程序的开源项目。分为admin-server
与admin-client
两个组件,admin-server
通过采集actuator
端点数据,显示在spring-boot-admin-ui
上,已知的端点几乎都有进行采集,通过spring-boot-admin
可以动态切换日志级别、导出日志、导出heapdump
、监控各项指标 等等….
Spring Boot Admin
在对单一应用服务监控的同时也提供了集群监控方案,支持通过eureka
、consul
、zookeeper、nacos
等注册中心的方式实现多服务监控与管理…
spring boot admin+nacos集成:
服务端整合:
新建boot项目引入下面依赖:
<!-- admin服务端依赖 https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.2.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.2.6.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!--配置中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--注册中心--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
属性配置:(安全起见引入security)
spring: #spring boot admin的登陆账号和密码配置 security: user: name: admin password: 123456 management: endpoints: web: exposure: include: '*' #日志相关配置 logging: level: org.springframework.security: DEBUG path: logs/ file: max-size: 1GB
bootstrap.yml配置
server: port: ${SERVER_PORT:8022} spring: application: name: admin cloud: nacos: discovery: server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848} config: server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848} file-extension: yml
主函数:
添加上@EnableAdminServer
注解即代表是Server
端。
配置文件:
package com.geostar.cloud.monitor.admin; 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 SecurityConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecurityConfig(AdminServerProperties adminServerProperties) { this.adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); http.authorizeRequests() .antMatchers(adminContextPath + "/assets/**").permitAll() .antMatchers(adminContextPath + "/actuator/**").permitAll() .antMatchers(adminContextPath + "/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminContextPath + "/login") .successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + "/logout") .and() .httpBasic().and() .csrf().disable(); // @formatter:on } }
至此服务端搭建完毕。
客户端:
只需要在你的cloud子项目模块引入actuator,并且注册到nacos即可,admin服务端就会去nacos服务中心拉取服务列表。
低版本可能还需要spring-boot-admin-starter-client
<!--自省和监控的集成功能--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
配置:
management: endpoints: web: exposure: include: '*' logging: level: com.geostar.cloud: debug java.sql.PreparedStatement: debug path: logs/ file: max-size: 1GB
然后把你的cloud项目全都启动访问admin服务中心:localhost:8022输入配置好的security用户名密码就可以愉快的玩耍了。👇
然后点击某一个应用进去就可以看到详细的监控信息。