Actuator Endpoints(端点)
Endpoints是Actuator的核心部分
SpringBoot Actuator提供的原生端点有三类:
应用配置类:自动配置信息、Spring Bean 信息、yml文件信息、环境信息等等
度量指标类:主要是运行期间的动态信息,例如堆栈、健康指标、metrics信息等等
操作控制类:主要是指dhutdown,用户可以发送一个请求将应用的监控功能关闭
创建Admin子项目
pom文件示例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e-commerce-springcloud</artifactId>
<groupId>com.taluohui.ecommerce</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>e-commerce-admin</artifactId>
<version>1.0.SNAPSHOT</version>
<packaging>jar</packaging>
<!--模块名及描述信息-->
<name>e-commerce-admin</name>
<description>监控服务器</description>
<dependencies>
<!--spring cloud alibaba nacos discovery 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--SpringBoot Admin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.taluohui.ecommerce</groupId>
<artifactId>e-commerce-mvc-config</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<!--
SpringBoot的Maven插件, 能够以Maven的方式为应用提供SpringBoot的支持,可以将
SpringBoot应用打包为可执行的jar或war文件, 然后以通常的方式运行SpringBoot应用
-->
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
创建启动类
/**
* <h1>监控中心服务器启动入口</h1>
*/
@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
配置文件示例
server:
port: 7001
servlet:
context-path: /e-commerce-admin
spring:
application:
name: e-commerce-admin # 应用名称也是构成 Nacos 配置管理 dataId 字段的一部分 (当 config.prefix 为空时)
cloud:
nacos:
# 服务注册发现
discovery:
enabled: true # 如果不想使用 Nacos 进行服务注册和发现, 设置为 false 即可
server-addr: 127.0.0.1:8848
# server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 # Nacos 服务器地址(集群方式)
namespace: 22d40198-8462-499d-a7fe-dbb2da958648
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
thymeleaf:
check-template: false
check-template-location: false
# 暴露端点
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
之后就可以在浏览器打开查看了,选择应用墙可以查看详情。
应用注册到SpringBoot Admin Server
方式一:被监控和管理的应用程序,使用SpringBoot Admin Client库, 通过HTTP调用注册到SpringBoot Admin Server上。(单体SpringBoot项目)
方式二:首先,被监控和管理的应用程序,注册到SpringCloud集成的注册中心;然后SpringBoot Admin Server通过注册中心获取被监控和管理的应用程序。(微服务项目)
只需要在配置文件中加入
spring:
cloud:
nacos:
discovery:
metadata:
management:
context-path: ${server.servlet.context-path}/actuator
就可以实现方式二。
配置安全认证
由于我们的安全中心会发布到公网上,所以我们必须配置安全认证,设置用户名和密码。
在pom中添加
<!-- 开启登录认证功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在bootstrap.yml中,通过配置以下配置,完成用户名和密码的设置
spring:
security:
user:
name: 用户名
password: 密码
cloud:
nacos:
# 服务注册发现
discovery:
metadata:
user.name: 用户名
user.password: 密码
添加包conf,并添加SecuritySecureConfig。用以开放登录登出界面以及服务注册等。
/**
* <h1>配置安全认证, 以便其他的微服务可以注册</h1>
* 参考 Spring Security 官方
* */
@Configuration
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 {
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/**"
);
}
}
自定义监控报警
我们的服务监控在监听到服务无法访问等问题时,实现自定义的报警等功能
添加包conf,并添加TaluohuiNotifier,用以自定义报警,我现在只实现了日志的打印,后面可以添加邮箱报警等方式。
/**
* <h1>自定义告警</h1>
* */
@Slf4j
@Component
@SuppressWarnings("all")
public class TaluohuiNotifier extends AbstractEventNotifier {
protected TaluohuiNotifier(InstanceRepository repository) {
super(repository);
}
/**
* <h2>实现对事件的通知</h2>
* */
@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());
}
});
}
}