请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

RestTemplate配置问题

报错

2019-12-18 15:33:38.858 ERROR 8892 --- [           main] o.s.c.a.s.c.SentinelBeanPostProcessor    : fallback static method can not be found in bean[restTempLate]. The right method signature is com.huey.content.other.ExceptionUtil#fallback[HttpRequest, byte[], ClientHttpRequestExecution, BlockException], please check your class name, method name and arguments
2019-12-18 15:33:38.859  WARN 8892 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'restTempLate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTempLate' defined in class path resource [com/huey/content/config/RestTempLateConfig.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalArgumentException: fallback static method can not be found in bean[restTempLate]. The right method signature is com.huey.content.other.ExceptionUtil#fallback[HttpRequest, byte[], ClientHttpRequestExecution, BlockException], please check your class name, method name and arguments
2019-12-18 15:33:38.862  INFO 8892 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-12-18 15:33:38.865  WARN 8892 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [logback-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)

代码:
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.netflix.client.http.HttpRequest;
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * @Author: huey
 * @Desc:
 */
public class ExceptionUtil {

    /**
     * 限流后处理方法
     * @param request
     * @param body
     * @param execution
     * @param ex
     * @return
     */
    public static SentinelClientHttpResponse block(HttpRequest request,
                                                             byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        System.err.println("block: " + ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("custom block info");
    }

    /**
     * 熔断后处理的方法
     * @param request
     * @param body
     * @param execution
     * @param ex
     * @return
     */
    public static SentinelClientHttpResponse fallback(HttpRequest request,
                                                      byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        System.err.println("fallback: " + ex.getClass().getCanonicalName());
        return new SentinelClientHttpResponse("custom fallback info");
    }

}
代码二:
@Configuration
public class RestTempLateConfig {
    //blockHandler = "block",blockHandlerClass = ExceptionUtil.class
    @Bean
    @LoadBalanced  //配合负载均衡 ribbon
    @SentinelRestTemplate(fallbackClass = ExceptionUtil.class,fallback = "fallback") //加入熔断
    public RestTemplate restTempLate(){
        return new RestTemplate();
    }


}

问题: 看报错说静态限流的fallback方法找不到,有点奇怪

正在回答

插入代码

3回答

您好,异常里面说得很清楚了,它说您定义的方法签名不正确。

1
The right method signature is com.huey.content.other.ExceptionUtil#fallback[HttpRequest, byte[], ClientHttpRequestExecution, BlockException], please check your class name, method name and arguments


不过我看您贴出的代码,是符合上面日志里面的方法签名要求的。

建议按照如下思路排查一下:

  1. 检查IDE,确保执行的代码和你的代码一致。

  2. 检查导包是否正确。

此外,贴个示例代码,供您参考:

  1. RestTemplate定义:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java

  2. fallback类定义:https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ExceptionUtil.java

如果依然发现不了问题,可以将代码托管到GitHub或者Gitee,我来定位一下。

祝您学习愉快!

1 回复 有任何疑惑可以回复我~
  • 提问者 徐子与 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2019-12-18 16:41:08
  • 提问者 徐子与 #2
    问题是在其中一个入参导包大意了
    回复 有任何疑惑可以回复我~ 2019-12-18 16:56:48
qq_邪饿的小强_0 2020-06-19 18:34:12

nice.我和你同样的问题,导错包.

1 回复 有任何疑惑可以回复我~
提问者 徐子与 2019-12-18 16:41:53

我重新写了一边,是可以的,谢谢

0 回复 有任何疑惑可以回复我~

相似问题

登录后可查看更多问答,登录/注册

问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号