Spring boot 3集成Micrometer
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
1.前沿
随着Spring Boot 3+版本的发布,可观察性(Observability)已完全支持Micrometer。
The upcoming Spring Boot 3.0.0-RC1 release will contain numerous autoconfigurations for improved metrics with Micrometer and new distributed tracing support with Micrometer Tracing (formerly Spring Cloud Sleuth). The most notable changes are that it will contain built-in support for log correlation, W3C context propagation will be the default propagation type, and we will support automatic propagation of metadata to be used by the tracing infrastructure (called “remote baggage”) that helps to label the observations.
从Spring Cloud 2022.0开始已完全移除对Sleuth的支持,将迁移至Micrometer。
This project has been removed from the release train. The core of this project has moved to Micrometer Tracing project and the instrumentations will be moved to Micrometer and all respective projects (no longer all instrumentations will be done in a single repository.
2.集成Micrometer、Zipkin
本文讲解Spring Boot 3快速集成Micrometer,并集成Zipkin展示应用健康看板。
引入依赖库
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
2.1.spring-boot-starter-actuator(监控监控)
actuator的依赖可以知道,actuator已完成转向支持Micrometer
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>3.1.3</version>
<name>spring-boot-starter-actuator</name>
<description>Starter for using Spring Boot's Actuator which provides production ready features to help you monitor and manage your application</description>
<url>https://spring.io/projects/spring-boot</url>
<organization>
<name>VMware, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Spring</name>
<email>ask@spring.io</email>
<organization>VMware, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>3.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
<version>3.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
<version>1.11.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.11.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
2.2.micrometer-tracing
Micrometer提供了跟踪的扩展接口ObservationHandler。
public interface ObservationHandler<T extends Observation.Context> {
/**
* Reacts to starting of an {@link Observation}.
* @param context an {@link Observation.Context}
*/
default void onStart(T context) {
}
/**
* Reacts to an error during an {@link Observation}.
* @param context an {@link Observation.Context}
*/
default void onError(T context) {
}
/**
* Reacts to arbitrary {@link Observation.Event}.
* @param event the {@link Observation.Event} that was signaled
* @param context an {@link Observation.Context}
*/
default void onEvent(Observation.Event event, T context) {
}
/**
* Reacts to opening of an {@link Observation.Scope}.
* @param context an {@link Observation.Context}
*/
default void onScopeOpened(T context) {
}
/**
* Reacts to closing of an {@link Observation.Scope}.
* @param context an {@link Observation.Context}
*/
default void onScopeClosed(T context) {
}
/**
* Reacts to resetting of scopes. If your handler uses a {@link ThreadLocal} value,
* this method should clear that {@link ThreadLocal} or any other scoped variable.
* @param context an {@link Observation.Context}
* @since 1.10.4
*/
default void onScopeReset(T context) {
}
/**
* Reacts to stopping of an {@link Observation}.
* @param context an {@link Observation.Context}
*/
default void onStop(T context) {
}
/**
* Tells the registry whether this handler should be applied for a given
* {@link Observation.Context}.
* @param context an {@link Observation.Context}
* @return {@code true} when this handler should be used
*/
boolean supportsContext(Observation.Context context);
}
2.3.bridge(桥接器)
Micrometer提供两种桥接实现,micrometer-tracing-bridge-brave和micrometer-tracing-bridge-otel,分别对接Brave和Otel。
3.实现
详情参考官网:Micrometer Application Observability
3.1.Brave模式
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
</dependency>
3.2.Otel模式
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
3.3.Zipkin安装
安装并启动Zipkin。
curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar
3.4.全局配置
全局配置
@Configuration
public class ObservedAspectConfiguration {
@Bean
public ObservedAspect observedAspect(ObservationRegistry observationRegistry) {
observationRegistry.observationConfig().observationHandler(new SimpleLoggingHandler());
return new ObservedAspect(observationRegistry);
}
}
定义Handler
public class SimpleLoggingHandler implements ObservationHandler<Observation.Context> {
@Override
public void onStart(Observation.Context context) {
ObservationHandler.super.onStart(context);
}
@Override
public void onError(Observation.Context context) {
ObservationHandler.super.onError(context);
}
@Override
public void onEvent(Observation.Event event, Observation.Context context) {
ObservationHandler.super.onEvent(event, context);
}
@Override
public void onScopeOpened(Observation.Context context) {
ObservationHandler.super.onScopeOpened(context);
}
@Override
public void onScopeClosed(Observation.Context context) {
ObservationHandler.super.onScopeClosed(context);
}
@Override
public void onScopeReset(Observation.Context context) {
ObservationHandler.super.onScopeReset(context);
}
@Override
public void onStop(Observation.Context context) {
ObservationHandler.super.onStop(context);
}
@Override
public boolean supportsContext(Observation.Context context) {
return true;
}
}
3.5.定义接口
@RestController
@RequestMapping("demo")
@Observed
public class DemoController {
@GetMapping("sayHello1")
public String sayHello() {
return "hello";
}
}
3.6.效果
访问地址:http://localhost:9411/
4.总结
通过集成Micrometer,用户可以快速、便捷的监控应用的各项指标
。