RestTemplate配置问题

来源:8-15 RestTemplate整合Sentinel

徐子与

2019-12-18

报错

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回答

大目

2019-12-18

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

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
2
徐子与
问题是在其中一个入参导包大意了
2019-12-18
共2条回复

qq_邪饿的小强_0

2020-06-19

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

1
0

徐子与

提问者

2019-12-18

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

0
0

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3085 学习 · 1324 问题

查看课程