可观测性-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。

参考

相关推荐
Jerry2505091 天前
微信小程序必要要安装SSL证书吗?小程序SSL详解
网络·网络协议·网络安全·微信小程序·小程序·https·ssl
大G的笔记本1 天前
gRPC vs RPC 高频面试题
网络·网络协议·rpc
无敌最俊朗@1 天前
7层/4层模型讲解
网络
驯狼小羊羔1 天前
学习随笔-http和https有何区别
前端·javascript·学习·http·https
草明1 天前
Chrome HSTS(HTTP Strict Transport Security)
前端·chrome·http
代码程序猿RIP1 天前
【Linux面经】OSI七层模型和TCP/IP四层体系结构
linux·网络·面试·面经
zt1985q1 天前
外网访问项目研发管理软件 codes
运维·服务器·windows·网络协议·tcp/ip
njnu@liyong1 天前
HTTP-发展史
网络·网络协议·http
_星辰大海乀1 天前
UDP / TCP 协议
网络·网络协议·tcp/ip·udp·tcp·三次握手·四次挥手
liebe1*11 天前
第十三章 网络服务和应用
网络协议·tcp/ip