跟着尚硅谷的SpringCloud教程学的,然后在写代码的时候用了比较新的版本去替代视频里的教学版本。
使用SpringCloud的Finchley.SR1版本
使用SpringBoot的2.0.1.RELEASE版本
在学到hystrix断路器的时候
由于@EnableFeignClients和@ComponentScan注解的时候,由于包路径设置不正确,导致错误。
错误重现
这是microservicecloud-consumer-dept-feign中主启动类,这里包路径扫描到了com.atguigu.spring的下面一层
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.spring.microservicecloudconsumerdeptfeign"})
@ComponentScan("com.atguigu.spring.microservicecloudapi")
public class MicroservicecloudConsumerDeptFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroservicecloudConsumerDeptFeignApplication.class, args);
}
}
这是对应的目录结构
按地址访问后如下
日志不报错,于是开启debug级别的日志查看
2018-09-27 15:27:44.084 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/consumer/dept/list]
2018-09-27 15:27:44.084 DEBUG 2283 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /consumer/dept/list
2018-09-27 15:27:44.085 DEBUG 2283 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/consumer/dept/list]
2018-09-27 15:27:44.085 DEBUG 2283 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/consumer/dept/list] are [/**]
2018-09-27 15:27:44.085 DEBUG 2283 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/consumer/dept/list] are {}
2018-09-27 15:27:44.085 DEBUG 2283 --- [nio-8088-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/consumer/dept/list] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@771a7d53]]] and 1 interceptor
2018-09-27 15:27:44.085 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/consumer/dept/list] is: -1
2018-09-27 15:27:44.086 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-09-27 15:27:44.086 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-09-27 15:27:44.086 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-09-27 15:27:44.087 DEBUG 2283 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-09-27 15:27:44.088 DEBUG 2283 --- [nio-8088-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-09-27 15:27:44.089 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2018-09-27 15:27:44.092 DEBUG 2283 --- [nio-8088-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2018-09-27 15:27:44.093 DEBUG 2283 --- [nio-8088-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@6221cbbf] based on requested media type 'text/html'
2018-09-27 15:27:44.093 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView@6221cbbf] in DispatcherServlet with name 'dispatcherServlet'
2018-09-27 15:27:44.093 DEBUG 2283 --- [nio-8088-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-09-27 15:27:44.248 DEBUG 2283 --- [nio-8088-exec-5] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/favicon.ico]
2018-09-27 15:27:44.248 DEBUG 2283 --- [nio-8088-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/favicon.ico] are [/**/favicon.ico]
2018-09-27 15:27:44.248 DEBUG 2283 --- [nio-8088-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/favicon.ico] are {}
2018-09-27 15:27:44.248 DEBUG 2283 --- [nio-8088-exec-5] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/favicon.ico] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@60e06f7d]]] and 1 interceptor
2018-09-27 15:27:44.248 DEBUG 2283 --- [nio-8088-exec-5] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/favicon.ico] is: -1
2018-09-27 15:27:44.253 DEBUG 2283 --- [nio-8088-exec-5] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-09-27 15:27:44.253 DEBUG 2283 --- [nio-8088-exec-5] o.s.web.servlet.DispatcherServlet : Successfully completed request
报错关键是
Did not find handler method for [/consumer/dept/list]
于是搜了一下这个报错
找到了这样的解释
链接地址https://stackoverflow.com/questions/43747742/springboot-cannot-find-handler-method
分析应该是那两个注解在进行扫描的时候,没有扫描到controller层,导致根据dispatcherServlet根据uri找不到对应的handler method,然后返回handler method for path /error,于是就能看到上面的This application has no explicit mapping for /error, so you are seeing this as a fallback.
修改扫描路径后,microservicecloud-consumer-dept-feign中主启动类如下
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.spring"})
@ComponentScan("com.atguigu.spring")
public class MicroservicecloudConsumerDeptFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroservicecloudConsumerDeptFeignApplication.class, args);
}
}
访问正常
另外记录
在使用feign中的hystrix功能时,需要在application.yml开启
feign:
hystrix:
enabled: true
IDEA会提示Cannot resolve configuration property 'feign.hystrix.enabled
但实际使用时还是会生效
生效如下