将 Hystrix 迁移到resilience4j

问题描述 投票:0回答:1

我有这个 Hystrix 代码,我想将其迁移到resilience4j:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix:2.2.10.RELEASE
.....

import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionHandlerAdvice {
  
  @ExceptionHandler(HystrixRuntimeException.class)
  public ResponseEntity<?> handleHystrixException(HystrixRuntimeException hystrixException) {

    if (HystrixRuntimeException.FailureType.TIMEOUT == hystrixException.getFailureType()) {
      System.out.println("Hystrix timeout exception: ", hystrixException);
      return new InternalTimeoutException());
    }
    return null;
  }
}

在resilience4j中替换

HystrixRuntimeException
的正确方法是什么?

java spring spring-boot hystrix resilience4j
1个回答
0
投票

Hystrix 使用命令模式,而 Resilience4j 使用装饰器和函数组合模式。

因此,要迁移,您必须更改方法并使用适当的注释,例如 CircuitBreaker、RateLimiter、Retry、Bulkhead 和 TimeLimiter。然后将它们组合在一起以实现类似的功能。

以下是更改方法的方法。

@Override
@Retry(name = "userDataRetry")
@Bulkhead(name = "userDataBulkhead")
public String getUserData(String userId) {
    // Simulate an external service call
    if (Math.random() < 0.7) { // Simulate a failure 70% of the time
        throw new RuntimeException("Failed to fetch user data for userId: " + userId);
    }
    return "UserData for userId: " + userId;
}

然后我们可以更改入口点来捕获特定的异常,如下所示。

@GetMapping("/user/{userId}")
public ResponseEntity<String> getUserData(@PathVariable String userId) {
    String userData;
    try {
        userData = externalService.getUserData(userId);
        return ResponseEntity.ok(userData);
    } catch (BulkheadFullException | RetryException ex) {
        // Handle resilience4j exceptions
        return ResponseEntity.status(503).body("Service unavailable, please try again later.");
    } catch (RuntimeException ex) {
        // Handle other unexpected exceptions
        return ResponseEntity.status(500).body("Internal server error");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.