生产上对 Web
应用 的监控是十分必要的。咱们能够近乎实时来对应用的健康、性能等其余指标进行监控来及时应对一些突发状况。避免一些故障的发生。对于 Spring Boot 应用来讲咱们能够经过一个轻量级的监控工具 Spring Boot Admin (SBA) 来进行监控。html
Spring Boot Admin是由德国软件工程师 Johannes Edmeier 开源的用于管理和监控 Spring Boot 应用程序。已经被收归入Spring Initializr 截至发文时间的最新正式版本为 2.1.6 ,快照为2.2.0-SNAPSHOT。 C/S 架构风格 。 应用程序做为 Spring Boot Admin Client 向 Spring Boot Admin Server 注册(经过HTTP
)或使用 Spring Cloud注册中心(如 Eureka,Consul)发现。SERVER程序采用了 响应式Web框架 Spring Webflux 。 展现UI采用了 Vue.js,展现Spring Boot Admin Client 经过 Spring Boot Actuator 端点上的一些监控。常见的功能或者监控以下:java
heapdump
jvm
系统和环境属性JMX-beans
交互http
跟踪auditevents
http-endpoints
Flyway
/Liquibase
数据库迁移接下来让咱们来在 Spring Boot 项目中集成 Spring Boot Admin 。注意版本的兼容性,可经过Spring Initializr 来验证。web
Spring Boot Admin Server 通常推荐独立做为一个 Spring Boot jar
应用运行。 只须要将下列依赖添加到你的 pom.xml
中:spring
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.0-SNAPSHOT</version> </dependency> <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>
而后经过添加 @EnableAdminServer
到配置中来引入 Spring Boot Admin Server 配置:数据库
@EnableAdminServer @SpringBootApplication public class SpringBootAdminApplication { public static void main(String[] args) { SpringApplication.run(SpringBootAdminApplication.class, args); } }
每一个要注册的应用程序都必须包括 Spring Boot Admin Client。为了保护端点,你还应该添加安全依赖 spring-boot-starter-security
。缓存
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
而后在客户端应用程序的 application.yml
中增长如下配置:安全
spring: boot: admin: client: # Spring Boot Admin Server 地址 http://localhost:8080 可自定义 url: http://localhost:8080 # 默认状况下,大多数端点都不经过http公开,咱们公开了全部端点。对于生产,您应该仔细选择要公开的端点。 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS
分别启动 SBA
服务端和客户端 。打开服务端页面 http://localhost:8080
将进入如下监控界面:springboot
进而也能够获取 admin-client
的具体监控指标:服务器
若是您已经将 Spring Cloud Discovery (eureka
、consul
等)用于您的应用程序,则不须要 Spring Boot Admin 客户端。只需将 DiscoveryClient 添加到 Spring Boot Admin Server ,其他的事情经过自动配置完成,可经过官方示例来查看。架构
应用的监控指标都是极其敏感的数据。因此生产上必须增长安全访问控制以免发生泄漏事件。你可使用你擅长的安全框架来作访问控制。这里咱们采用 Spring Security 来保护咱们的 Spring Boot Admin 。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
spring: security: user: name: SBA_admin password: SBA_password roles: SBA_ADMIN
package cn.felord.admin.server.configuer; import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; 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; import org.springframework.security.web.csrf.CookieCsrfTokenRepository; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import java.util.UUID; /** * The type Security secure config. * * @author Felordcn * @since 2019 /10/19 23:33 */ @Configuration public class AdminServerSecurityConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; /** * Instantiates a new Security secure config. * * @param adminServer the admin server */ public AdminServerSecurityConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity http) throws Exception { // @formatter:off SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); final String adminServerContextPath = this.adminServer.getContextPath(); successHandler.setDefaultTargetUrl(adminServerContextPath+"/"); http.authorizeRequests() .antMatchers(adminServerContextPath + "/assets/**").permitAll() // <1> .antMatchers(adminServerContextPath + "/login").permitAll() .anyRequest().authenticated() // <2> .and() .formLogin().loginPage(adminServerContextPath + "/login").successHandler(successHandler).and() // <3> .logout().logoutUrl(adminServerContextPath + "/logout").and() .httpBasic().and() // <4> .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5> .ignoringRequestMatchers( new AntPathRequestMatcher(adminServerContextPath + "/instances", HttpMethod.POST.toString()), // <6> new AntPathRequestMatcher(adminServerContextPath + "/instances/*", HttpMethod.DELETE.toString()), // <6> new AntPathRequestMatcher(adminServerContextPath + "/actuator/**") // <7> ) .and() .rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600); } }
而后启动 SBA Server 服务器 http://localhost:8237
会进入登陆页面,输入你配置的帐密便可:
服务端端点被访问控制后,客户端注册须要权限,同时客户端的一些 Actuator 端点也必须被保护。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
spring: security: user: name: SBA_admin password: SBA_password roles: SBA_ADMIN
将咱们在Spring Boot Admin服务端配置配置的安全帐户配置到如下属性中:
boot: admin: client: # Spring Boot Admin Server 管理帐户 username: SBA_admin password: SBA_password
当使用HTTP Basic
身份验证保护执行器端点时,SBA Server 须要凭据才能访问它们。因此咱们经过如下来配置以受权服务端访问 Actuator 端点:
spring: boot: admin: client: instance: metadata: # 这里是咱们在 client 设置安全帐户信息 步骤中设置的帐密 user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}
启动客户端应用就能够了。
请注意:若是你改变了 HTTP BASIC 方式访问端点,上面的配置会失效,你可能会须要定制 HttpHeadersProvider
来知足你的须要。
Spring Boot Admin 还提供了一些咱们经常使用的功能。
默认状况下,日志文件没法经过执行器端点访问,所以在 Spring Boot Admin 中不可见。为了启用日志文件执行器端点,您须要经过设置logging.path
或 logging.file
。
Spring Boot Admin 将检测全部看起来像URL的内容,并将其呈现为超连接。还支持ANSI
颜色转义。您须要设置一个自定义文件日志模式,由于Spring Boot的默认模式不使用颜色。
以 logging.file
为例, 咱们在客户端 application.yml
增长如下配置:
logging: file: /application.log pattern: file: '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx'
而后便可在 SBA
控制台显示:
Tags
是咱们区别同一应用的不一样实例的有效方法。好比咱们同时使用 SBA
监控了 spring.application.name=admin-client
应用的三个实例,分别是开发(DEV
)、测试(TEST
)、生产(PROD
)。咱们能够经过(以开发为例):
使用信息端点/info
:
info: tags: environment: DEV
或者配置 SBA
元数据:
spring: boot: admin: client: instance: metadata: tags: environment: DEV
而后咱们就能够经过详情界面查看到具体的信息:
Spring Boot Admin 支持配置邮件来发送邮件通知,以便于咱们及时处置系统警报。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
# spring boot mail 配置 spring: mail: host: smtp.qq.com username: username@xx.com password: password properties: mail: smtp: auth: true starttls: enable: true required: true
# SBA 邮件配置 boot: admin: notify: mail: from: from_user@xxx.com to: to_admin@xxx.com
这样就能够接收邮件告警了。国内也可使用钉钉机器人通知功能。
还有其它一些功能,能够经过官方文档进行学习。
今天咱们学习了使用 Spring Boot Admin 对 Spring Boot 应用进行监控。也学习了如何对 Spring Boot Admin 进行安全访问控制,还有一些有用的进阶操做。 这里须要说明的是对一些小型应用 Spring Boot Admin 能够彻底胜任监控功能,也很是简单好用。 可是对于大型分布式集群应用来讲我我的不建议使用 Spring Boot Admin ,须要其它更加专业的 APM
监控,好比开源的 Apache Skywalking
、Prometheus + Grafana
等等。
相关 SBA
实战完整代码可关注公众号:Felordcn
回复 admin
获取
关注公众号:Felordcn 获取更多资讯