hystrix報錯找不到相應的fallbackMethod


  1. 問題出現:

當消費者客戶端調用提供者的服務時,會出現以下錯誤,調用方出現以下錯誤

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("沒有該數據庫信息");
    }
  1. 問題解決,定義的備用方法中是Long包裝類,但是原始方法中是long基本類型,這就是報錯的源頭,我將其統一之后就沒有報錯了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM