SpringbootAdmin微服务应用监控(SBA)
- Springboot Actuator模块提供了生产级别的功能,比如健康检查,审计,指标收集,HTTP跟踪等,帮助我们监控和管理Springboot应用.这个模块是一个采集应用内部信息暴露给外部的模块。上述的功能都可以通过HTTP和JMX访问。
Actuator Endpoints(端点)
-
Endpoints是Actuator的核心部分,他用来监视应用程序及交互;Springboot Actuator内置了很多Endpoints,并支持扩展(其实就相当于一堆HttpRestful的接口)
-
Springboot Actuator提供的原生端点有三类:
1> 应用配置类:自动配置信息,springBean信息,yml文件信息,环境信息等等
2> 度量指标类:主要是运行期间的动态信息。例如堆栈、健康指标、metrics信息等等
3> 操作控制类:主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭
搭建Springboot Admin监控服务器
步骤:
1.添加SpringbootAdminstart自动配置依赖。(实现对Springboot Admin Server的自动化配置,包含Spring-boot-admin-server[Server端],spring-boot-admin-server-ui[UI界面],spring-boot-admin-server-cloud[对Spring Cloud的介入])
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version>
</dependency>
2.添加启动注解: @EnableAdminServer
3.添加bootstrap.yml文件
server:
port: 7001
servlet:
context-path: /yue-admin
spring:
application:
name: yue-admin
# security:
# user:
# name: mry
# password: 88888888
cloud:
nacos:
discovery:
enabled: true
server-addr: 127.0.0.1:8848
namespace: c31ddf18-a5ee-4bb9-b1ea-c5fc106ebb54
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
# user.name: mry
# user.password: 88888888
thymeleaf:
check-template: false
check-template-location: false
# 暴露端点
management:
endpoints:
web:
exposure:
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 *, 可以开放所有端点
endpoint:
health:
show-details: always
4.应用注册到Springboot Admin Sever
被监控和管理的应用(微服务),注册到Admin Server有两种方式(一般都采用第二种方式):
1>方式一:被监控和管理的应用程序,使用Springboot AdminClient库,通过Http调用注册到Springboot Admin Server上。
2>方式二:受限,被监控和管理的应用程序。注册到SpringCloud集成的注册中心;然后Springboot Admin Server通过注册中心获取被监控和管理的应用程序
步骤:
1.添加Springboot Actuator依赖
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.暴露SpringBoot Actuator Endpoints
management:
endpoints:
web:
exposure:
include: '*' #需要开放的断点,默认值只打开health和info两个端点,通过设置*,可以开放所有端点.
endpoint:
health:
show-details: always
3.指定Springboot Actuator Endpoints,这个是指定了context-path之后,需要配置的,需要告诉程序更改的路径。
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
自定义监控告警
定义新的类继承AbstractEventNotifier,重写doNotify方法。可以对具体状态进行定制开发。
AbstractEventNotifier为SpringbootAdmin的一个事件通知抽象基类
@Component
@Slf4j
public class YueNotifier extends AbstractEventNotifier {
protected YueNotifier(InstanceRepository repository) {
super(repository);
}
/***
* @return reactor.core.publisher.Mono<java.lang.Void>
* @Description 实现对事件的通知
* @Param [event, instance]
* @Author create by ayue 2023/1/3 22:05
*/
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
return Mono.fromRunnable( ()->{
if (event instanceof InstanceStatusChangedEvent) {
log.info("Instance Status Change: [{}], [{}], [{}]",
instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
} else {
log.info("Instance Info: [{}], [{}], [{}]",
instance.getRegistration().getName(), event.getInstance(),
event.getType());
}
});
}
}
添加邮件报警功能
被监控的应用状态变更为 DOWN、OFFLINE、UNKNOWN 时, 会自动发出告警: 实例的状态、原因、实例地址等信息 需要在 pom.xml 文件中添加 spring-boot-starter-mail 依赖 配置发送告警的邮箱服务器 但是, 这个要能连接上, 否则会报错
- 添加邮箱POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置收、发邮箱信息
spring:
mail:
# 配置邮箱 smtp 地址
host: smtp.sina.com
# 配置邮箱的账户名
username: xxxx@sina.com
# 配置邮箱授权码(此处为授权码,而非密码)
password: xxxxxx
default-encoding: UTF-8
# 监控告警通知
boot:
admin:
notify:
mail:
# 配置发送邮箱
from: ${spring.mail.username}
# 配置接收邮箱
to: 1255525465@qq.com
cc: 1255525465@qq.com
- 开启 SMTP 服务
发送的邮箱必要要开启 SMTP 服务,新浪邮箱找到 客户端pop/imap/smtp,然后在设置中找到IMAP/SMTP 服务并开启它.开启的同时会提供一个授权码(其他邮箱可以手动生成授权码).
- 注意事项
如果配置多个接收邮箱,用逗号分隔配置就行。 发送邮件的邮箱必须开启 SMTP 服务。发送邮箱无需设置密码,只需要为配置项“spring.mail.password”设置邮箱授权码即可。发送邮箱和接收邮箱可以是同一个邮箱地址。SBA 邮箱报警提醒功能无需添加任何代码,只需要添加相应的框架支持,然后再配置上正确的收、发邮箱即可。
访问权限控制
SpringbootAdmin(SBA)默认是没有权限控制的,生产环境上需要添加一定的权限验证。所以可以通过添加SpringSecurity框架来实现权限拦截。
步骤:
1.添加springsecurity相关依赖
<!-- 开启登录认证功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 配置文件中设置登录账户信息
spring:
security:
user:
name: mry
password: 88888888
cloud:
nacos:
discovery:
metadata:
user.name: mry
user.password: 88888888
3、添加配置类,配置对应信息
package com.yue.config;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
/**
* @Author: ayue
* @Description: 配置安全认证,以便其他的微服务可以注册,参考SpringSecurity官方文档
* @Date Created in 2023-01-03-16:52
*/
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
/* 应用上下文路径 */
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties){
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//认证成功后对当前request对象进行缓存的handler
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath+"/");
http.authorizeRequests()
// 1.配置所有静态资源和登录页可以公开访问
.antMatchers(adminContextPath+"/assets/**").permitAll()
.antMatchers(adminContextPath+"/login").permitAll()
//2.其他请求,必须要经过认证
.anyRequest().authenticated()
.and()
//3.配置登录和登出路径
.formLogin().loginPage(adminContextPath +"/login")
.successHandler(successHandler)
.and()
.logout().logoutUrl(adminContextPath+"/logout")
.and()
//4.开启http basic支出,其他的服务模块注册的时候需要使用
.httpBasic()
.and()
//5.开启基于cookie的csrf保护
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
//6.忽略这些路径的csrf保护以便其他的模块可以实现注册
.ignoringAntMatchers(
adminContextPath+"/instances",
adminContextPath+"/actuator/**"
);
}
}