Feign中使用hystrix功能和@EnableFeignClients和@ComponentScan註解的一個坑
阿新 • • 發佈:2018-12-12
跟著尚矽谷的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=[ [email protected]1a7d53]]] 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.aut [email protected]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.aut[email protected]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=[[email protected]e06f7d]]] 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]
於是搜了一下這個報錯
找到了這樣的解釋
分析應該是那兩個註解在進行掃描的時候,沒有掃描到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
但實際使用時還是會生效
生效如下