java - 如何使 HystrixCommand 适用于服务类中的所有功能
问题描述
在一个服务类中,有四个函数,所有这些函数都使用RestTemplate来远程调用第三个服务器。
以下是示例:
@Service
public class PaymentService
{
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
})
public String function1(@PathVariable("id") Integer id)
{
// some restTemplate action
}
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
})
public String function2(@PathVariable("id") Integer id)
{
// some restTemplate action
}
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
})
public String function3(@PathVariable("id") Integer id)
{
// some restTemplate action
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id)
{
return "Server [PaymentService][paymentCircuitBreaker_fallback] id=" + id;
}
}
但是三个函数使得@HystrixCommand 头部和属性都一样,所以有没有办法只写一次?
解决方案
推荐阅读
- java - 检查范围内的更多整数
- docker - 既然 docker 容器是独立运行的,那么基于同一个镜像的多个容器的代码段是否会在内存中重复呢?
- python - Detectron 2 有预测概率函数吗?我想将 Lime 与 Detectron 2 一起使用
- android - 启用 Dotfuscator 社区版后,适用于 Android 的 Xamarin 表单应用程序崩溃
- javascript - 如果在垫步进器之前打开任何文档,如何禁用/停止 HostListener?
- django - 如何获取 Django 模板中两个日期时间字段之间的秒数差异?
- google-cloud-platform - 使用 terraform 设置 iam 权限时,保护自己免于失去对存储桶的访问权限?
- linux - “/bin/exec -c --”是什么意思?
- android - 如何在初始登录时自动将用户添加到 Firebase 中的现有节点
- python - Django 信号没有收到任何信号(不起作用)