如何在 Liberty 配置文件中使用 MicroProfile FaultTolerance

How to use MicroProfile FaultTolerance in Liberty profile

我想在我的网络应用程序中使用 MP FaultTolerance 功能中的断路功能。现在我不知道如何知道这个功能是否一直在我的应用程序中工作。我想跟踪 MP Fault Tolerance 自动添加的 MP Metrics 的值,如 https://download.eclipse.org/microprofile/microprofile-fault-tolerance-2.0/microprofile-fault-tolerance-spec.html#fallback

中所述

我的应用程序在 WAS Liberty 配置文件 19.0.0.6 上运行。我尝试使用 /metrics 获取所有指标,但只有基本指标被 returned。 return如下

# TYPE base:classloader_total_loaded_class_count counter
# HELP base:classloader_total_loaded_class_count Displays the total number of classes that have been loaded since the Java virtual machine has started execution.
base:classloader_total_loaded_class_count 8853

我导入了包 org.eclipse.microprofile.faulttolerance。注释 CircuitBreaker 到我的 java 代码中并在这样的方法前面添加注释:

@CircuitBreaker(successThreshold = 2, requestVolumeThreshold = 3, failureRatio = 0.5, delay = 1000)
public void handle() throws ApiRequesterException{

    ..........
}

我在 server.xml 中添加了以下功能

<featureManager>

     <feature>mpFaultTolerance-1.1</feature>

     <feature>mpMetrics-1.1</feature>

</featureManager>
  1. 如何获取Fault Tolerance添加的Metrics的值,如ft.<name>.circuitbreaker.callsSucceeded.totalft.<name>.circuitbreaker.callsSucceeded.total
  2. 当我的应用程序处于 运行 时,如何知道注释 CircuitBreaker 正在工作?

看起来您做对了所有事情,所以您看不到指标的原因有以下几种:

  1. 在方法被调用一次之前指标不会出现

  2. 您是否安装了所需的功能?

如果您没有同时安装 mpFaultTolerance-1.1 和 mpMetrics-1.1,服务器仍会启动,但您会在 messages.log 顶部收到一条警告,说明哪些功能无法启动开始或不在场。

  1. 注解方法是在 CDI bean 中吗?

Fault Tolerance 是使用拦截器实现的。要进行拦截,该方法需要在 CDI bean 上,您需要使用 @Inject 将 bean 注入某处,然后您需要在注入的实例上调用该方法。

特别是,在以下情况下不会发生拦截:

  • 您使用 new
  • 创建 class 的实例
  • 您从同一个 class
  • 中调用带注释的方法

就测试断路器是否正常工作而言,最简单的方法通常是调用该方法并检查指标是否出现。除此之外,您需要使您的方法失败几次并检查您是否开始获得 CircuitBreakerOpenException.