hystrix报错找不到相应的fallbackMethod
- 问题出现:
当消费者客户端调用提供者的服务时,会出现以下错误,调用方出现以下错误
2020-11-27 17:03:51.996 ERROR 11536 --- [p-nio-80-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [{"timestamp":"2020-11-27T09:03:51.952+00:00","status":500,"error":"Internal Server Error","trace":"com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't fo... (8286 bytes)]] with root cause
org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [{"timestamp":"2020-11-27T09:03:51.952+00:00","status":500,"error":"Internal Server Error","trace":"com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't fo... (8286 bytes)]
at org.springframework.web.client.HttpServerErrorException.create(HttpServerErrorException.java:100) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
而提供接口服务的一方报的错误如下:
2020-11-27 17:03:51.937 ERROR 376 --- [nio-8001-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: fallback method wasn't found: processHystrixGetDept([long])] with root cause
com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: fallback method wasn't found: processHystrixGetDept([long])
从上面的报错信息可以看出来 fallback method wasn't found: processHystrixGetDept([long])
,说明是fallback method
方法有问题,后来发现是Long和long的问题,代码如下:
@GetMapping("/dept/get/{id}")
@HystrixCommand(fallbackMethod = "processHystrixGetDept")
public Dept dept(@PathVariable("id") long id) {
Dept dept = deptService.queryById(id);
if (dept == null) {
throw new RuntimeException("该id" + id + "没有对应的信息");
}
return dept;
}
public Dept processHystrixGetDept(Long id) {
return new Dept().setDeptno(id)
.setDname("该id:" + id + "没有对应的信息,null@HystrixCommand")
.setDb_source("没有该数据库信息");
}
- 问题解决,定义的备用方法中是Long包装类,但是原始方法中是long基本类型,这就是报错的源头,我将其统一之后就没有报错了
保持对优秀的热情