SpringBoot05-监听与监控

SpringBoot-监听机制

Java 监听机制

SpringBoot 的监听机制,其实是对Java提供的事件监听机制的封装。

​ Java中的事件监听机制定义了以下几个角色:

​ ① 事件:Event,继承 java.util.EventObject 类的对象

​ ② 事件源:Source ,任意对象Object

​ ③ 监听器:Listener,实现 java.util.EventListener 接口 的对象

​ SpringBoot 在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成一些操作。

​ ApplicationContextInitializer:SpringBoot 初始化时触发

​ SpringApplicationRunListener:SpringBoot整个生命周期

​ CommandLineRunner(无需注册):SpringBoot启动后触发

​ ApplicationRunner(无需注册):SpringBoot启动后触发

定义各种监听器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("MyApplicationRunner .. ");
}
}

@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner ...");
}
}


public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("MyApplicationContextInitializer ...");
}
}


public class MySpringApplicationRunListener implements SpringApplicationRunListener{

@Override
public void starting(ConfigurableBootstrapContext bootstrapContext) {
System.out.println("MySpringApplicationRunListener ...starting");
}

@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
System.out.println("MySpringApplicationRunListener ...environmentPrepared");
}


@Override
public void contextPrepared(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener ...contextPrepared");
}

@Override
public void contextLoaded(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener ...contextLoaded");
}

@Override
public void started(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener ...started");
}

@Override
public void running(ConfigurableApplicationContext context) {
System.out.println("MySpringApplicationRunListener ...running");
}

@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
System.out.println("MySpringApplicationRunListener ...failed");
}

public MySpringApplicationRunListener(SpringApplication application, String[] args) {
}
}

META-INF/spring.factories:注册SpringApplicationRunListener和ApplicationContextInitializer

1
2
3
4
org.springframework.boot.SpringApplicationRunListener=\
com.study.springboot01simple.listenerr.MySpringApplicationRunListener
org.springframework.context.ApplicationContextInitializer=\
com.study.springboot01simple.listenerr.MyApplicationContextInitializer

启动过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
MySpringApplicationRunListener ...starting
MySpringApplicationRunListener ...environmentPrepared

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.3)

MyApplicationContextInitializer ...
MySpringApplicationRunListener ...contextPrepared
2021-08-20 22:49:22.105 INFO 18552 --- [ main] c.s.s.Springboot01SimpleApplication : Starting Springboot01SimpleApplication using Java 1.8.0_171 on LAPTOP-87U6TGC3 with PID 18552 (D:\git\code\study\springboot-itheima-2021\springboot-01-simple\target\classes started by 81566 in D:\git\code\study\springboot-itheima-2021)
2021-08-20 22:49:22.117 INFO 18552 --- [ main] c.s.s.Springboot01SimpleApplication : No active profile set, falling back to default profiles: default
MySpringApplicationRunListener ...contextLoaded
2021-08-20 22:49:25.313 INFO 18552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-08-20 22:49:25.336 INFO 18552 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-20 22:49:25.336 INFO 18552 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-20 22:49:25.578 INFO 18552 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-08-20 22:49:25.578 INFO 18552 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3255 ms
2021-08-20 22:49:26.463 INFO 18552 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-20 22:49:26.494 INFO 18552 --- [ main] c.s.s.Springboot01SimpleApplication : Started Springboot01SimpleApplication in 5.848 seconds (JVM running for 8.709)
MySpringApplicationRunListener ...started
MyApplicationRunner ..
MyCommandLineRunner ...
MySpringApplicationRunListener ...running
com.study.springboot01simple.Springboot01SimpleApplication

SpringBoot监控-actuator

使用步骤

① 导入依赖坐标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

</dependencies>

②application.properties

1
2
3
4
5
6
7
8
9
10
#开放健康信息检测
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*
#配置Spring Boot Admin 服务端
spring.boot.admin.client.url=http://localhost:9000
spring.boot.admin.client.instance.name=client-01
server.address=localhost
info.username=小帅哥
info.age = 24
server.port=9001

③ 访问http://127.0.0.1:9001/acruator

/beans 描述应用程序上下文里全部的Bean,以及它们的关系

/env 获取全部环境属性

/env/{name} 根据名称获取特定的环境属性值

/health 报告应用程序的健康指标,这些值由HealthIndicator的实现类提供

/info 获取应用程序的定制信息,这些信息由info打头的属性提供

/mappings 描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系

/metrics 报告各种应用程序度量信息,比如内存用量和HTTP请求计数

/metrics/{name} 报告指定名称的应用程序度量值

/trace 提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)

④利用spring boot admin展示客户端监控信息

​ Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。

​ Spring Boot Admin 有两个角色,客户端(Client)和服务端(Server)。

​ 应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册

​ Spring Boot Admin Server 的UI界面将Spring Boot Admin Client的Actuator Endpoint上的一些监控信息。

admin-server:

① 创建 admin-server 模块

② 导入依赖坐标 admin-starter-server

③ 在引导类上启用监控功能**@EnableAdminServer**

admin-client:

① 创建 admin-client 模块

② 导入依赖坐标 admin-starter-client

③ 配置相关信息:server地址等

⑤ 启动server和client服务,访问server

http://localhost:9000/applications

image-20210822172511981