可观测性-Metrics-WebClient异步Http远程Call

代码示例

1.依赖导入

xml 复制代码
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.定义一个远程call客户端

java 复制代码
@Service
public class BaiduClient {
    @Bean
    public WebClient baidu(WebClient.Builder webClientBuilder) {
        return  webClientBuilder.baseUrl("https://xxxx.cn").build();
    }
}

3.定义一个指标注册器(输出到日志文件)

java 复制代码
@Configuration
public class MetricsRegistryConfiguration {

    @ConditionalOnProperty(name = "metrics.export", havingValue = "log", matchIfMissing = true)
    @Bean
    public MeterRegistry meterRegistry() {
        return new LoggingMeterRegistry(new LoggingRegistryConfig() {
            @Override
            public String get(String key) {
                return null;
            }

            @Override
            public Duration step() {
                return Duration.ofSeconds(60);
            }
        }, Clock.SYSTEM);
    }

}

4.日志配置

xml 复制代码
 <!-- 定义metrics的输出为日志 INFO -->
    <logger name="io.micrometer.core.instrument.logging.LoggingMeterRegistry" level="INFO"/>

5.调用示例

java 复制代码
@Controller
public class XXXXController {
    @Autowired
    WebClient baidu;

    @GetMapping("/test")
    @ResponseBody
    public Response captcha() {
        baidu.get()
                .uri("/echo")
                .retrieve().bodyToMono(String.class)
                .block(Duration.ofSeconds(10));
    }

6.示例结果日志

ini 复制代码
http.client.requests{client.name=xxx.cn,error=none,exception=none,method=GET,outcome=SUCCESS,status=200,uri=/echo} throughput=0.3/s mean=0.271909994s max=0.6102772s

高级

你还可以使用ObservationWebClientCustomizer定制。

默认情况下,生成的指标名称为http.client.requests。您可以通过设置属性来自定义名称

  • management.observations.http.client.requests.name(3.X版本)
  • management.metrics.web.client.requests-metric-name(2.X)

自定义标签,提供一个实现自org.springframework.web.reactive.function.client包中的 ClientRequestObservationConvention 的 @Bean

低基数键

名称 描述
method(必需) HTTP 请求方法的名称,如果无法创建请求则为 "none"。
uri(必需) 用于 HTTP 请求的 URI 模板,如果未提供则为 "none"。仅考虑 URI 的路径部分。
client.name(必需) 从请求 URI 主机派生的客户端名称。
status(必需) HTTP 响应的原始状态代码,或者在发生 IOException 时为 "IO_ERROR",或者如果未接收到响应则为 "CLIENT_ERROR"。
outcome(必需) HTTP 客户端交换的结果。
exception(必需) 在交换过程中抛出的异常的名称,如果没有发生异常则为 "none"。

高基数键

名称 描述
http.url(必需) HTTP 请求的 URI。

参考:

相关推荐
IpdataCloud28 分钟前
批量查IP的API接口怎么选?免费API配额瓶颈与离线库本地批量方案实测
网络·网络协议·tcp/ip
ITyunwei09872 小时前
售后夜间值守怎么落地:从告警到交接的工单系统配置笔记
网络
Smoothcloud润云3 小时前
GPU远程纳管安全吗?最小权限、网络边界与日志审计实战
服务器·网络·人工智能·安全·gpu算力·gpu
ai大模型-探索者3 小时前
Function Call与MCP(区别、联系与实战)
java·服务器·网络
GIoT80103 小时前
整段 C 段 IP 不能只看 Ping:一套实用的批量检测与分析思路
http
木子欢儿3 小时前
Debian 13 + vsftpd 搭建FTP服务
linux·服务器·前端·网络·debian
达~熊迪4 小时前
IP 分配前核查:把在线状态和使用登记一起看
服务器·网络·tcp/ip
西索斯coding5 小时前
Qwen3-Max 与 Qwen-Plus:60 个真实修复任务评测方案
大数据·网络·算法·ai
瓶瓶持续学习中5 小时前
数字孪生与空间智能技术架构解析:五类厂商路线及选型方法
大数据·网络·架构
伤心的梧桐叶5 小时前
商品大图断点续传后打不开,先看是不是把完整响应又接了一遍
http·https