Spring-Cloud Hystrix(未找到回退方法)

Spring-Cloud Hystrix (fallback method wasn't found)

我正在尝试使用 hyst,但是当调用保存方法时,使用 resttemplate 生成 post,出现以下异常:

com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException:未找到回退方法:breaker([class com.wnb.mastercard.domain.enroll.EnrollCommand])

有人可以帮助我吗?

@Component
public class EnrollRepositoryRest {

    @Autowired
    private RestTemplate template;

    @Value("${beblue-card-enroll.url}")
    private String url;

    public Enroll getEnrollByCardId(String cardId) {

        Enroll[] enroll = template.getForObject(url + "cardEnroll/enroll/" + cardId, Enroll[].class);

        return enroll[0];
    }

    @HystrixCommand(fallbackMethod = "breaker")
    public void save(EnrollCommand command) {
        template.postForObject(url + "/cardEnroll/enroll", command, EnrollCommand.class);
    }

    public String breaker() {
        System.out.println("HYSTRIX EXECUTADO");
        return "Hystrix is Ok";
    }
}

我认为异常清楚地告诉了你这个问题。方法:

public String breaker(EnrollCommand command) {
    System.out.println("HYSTRIX EXECUTADO");
    return "Hystrix is Ok";
}

不存在。 (注意签名中的参数)

当您使用该注释定义回退方法时,回退方法必须与您定义 Hystrix 命令的方法的相同参数相匹配。

@Ramon Rius

甚至 return 类型也必须与 'save' 方法相同,在这种情况下它是无效的。

否则我会收到此错误。

ERROR 10340 --- [nio-8060-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is 
com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: 
Incompatible return types. 
Command method: public long com.brownfield.pss.book.component.BookingComponent.book(com.brownfield.pss.book.entity.BookingRecord,java.lang.String);
Fallback method: public java.lang.String com.brownfield.pss.book.component.BookingComponent.fallByMethod(com.brownfield.pss.book.entity.BookingRecord,java.lang.String);
Hint: Fallback method 'public java.lang.String com.brownfield.pss.book.component.BookingComponent.fallByMethod(com.brownfield.pss.book.entity.BookingRecord,java.lang.String)' must return: long or its subclass] with root cause

fallbackMethod 和 main 方法的参数和 return 类型应该相同。 fallbackMethod 可能有一个 Throwable

类型的额外参数

后备方法必须与原始方法具有相同的定义

private void breaker(EnrollCommand command) {
    System.out.println("HYSTRIX EXECUTADO");
}