Spring-Cloud Hystrix(找不到后备方法)

时间:2017-02-01 11:06:43

标签: hystrix spring-cloud-netflix

我正在尝试使用hyst,但是当调用save方法时,会使用resttemplate发布一个帖子,但会出现以下异常:

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";
    }
}

3 个答案:

答案 0 :(得分:10)

我认为这个例外很清楚地告诉你这个问题。方法:

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

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

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

答案 1 :(得分:0)

@Ramon Rius

即使返回类型必须与'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

Thing是fallbackMethod的参数和返回类型,main方法应该相同。 fallbackMethod可能有一个类型为Throwable

的额外参数

答案 2 :(得分:0)

回退方法必须与原始方法具有相同的定义

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