gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:45 24 4
gpt4 key购买 nike

我想在我的 Web 应用程序中使用 MP FaultTolerance 功能中的熔断功能。现在我不知道如何知道这个函数是否在我的应用程序中运行。我想自动跟踪 MP 容错添加的 MP 指标的值,如 https://download.eclipse.org/microprofile/microprofile-fault-tolerance-2.0/microprofile-fault-tolerance-spec.html#fallback 中所述。

我的应用程序在 WAS Liberty 配置文件 19.0.0.6 上运行。我尝试使用/metrics 获取所有指标,但仅返回基本指标。返回结果如下

# 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.total , ft.<name>.circuitbreaker.callsSucceeded.total等等。
  2. 当我的应用程序运行时,如何知道注释 CircuitBreaker 正在工作?

最佳答案

看来您所做的一切都是正确的,因此有几种可能会导致您看不到指标:

  1. 只有调用该方法一次后,指标才会显示

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

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

  • 带注释的方法是在 CDI bean 中吗?
  • 容错是使用拦截器实现的。为了进行拦截,该方法需要位于 CDI bean 上,您需要使用 @Inject 将您的 bean 注入(inject)某处,然后需要在注入(inject)的实例上调用该方法。

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

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

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

    关于java - 如何在 Liberty 配置文件中使用 MicroProfile FaultTolerance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57471610/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com