SpringCloud feign無法注入接口的問題
SpringCloud feign無法注入接口
接口:
package cn.mn.app.service; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name="sc-provider") public interface IGoodsClient { @RequestMapping(value = "/goods/query",method = RequestMethod.GET) String query(); }
controller
package cn.mn.app.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import cn.mn.app.service.IGoodsClient; @RestController @RequestMapping("/goods") public class GoodsController { @Autowired private IGoodsClient goodsClient; @GetMapping("/q") public String query(){ return goodsClient.query(); } }
這里把接口直接注入。
啟動類:
package cn.mn.app.application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication(scanBasePackages = { "cn.mn.app.controller" }) @EnableDiscoveryClient @EnableFeignClients public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } // @LoadBalanced // @Bean // RestTemplate restTemplate(){ // return new RestTemplate(); // } }
啟動報錯,無法注入IGoodsClient。
原因是沒有使用@EnableFeignClients掃描指定包。
修改啟動類
package cn.mn.app.application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication(scanBasePackages = { "cn.mn.app.controller" }) @EnableDiscoveryClient @EnableFeignClients("cn.mn.app.service") public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args); } // @LoadBalanced // @Bean // RestTemplate restTemplate(){ // return new RestTemplate(); // } }
正常。
springcloud中feign調(diào)用常見問題
注: 本文基于Springcloud Edgware版本
Feign調(diào)用首次失敗問題
1、Feign簡介
Feign是一個聲明式的偽Http客戶端,它使得寫Http客戶端變得更簡單。使用Feign,只需要創(chuàng)建一個接口并注解。
它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解,F(xiàn)eign 整合了Ribbon。
2、原因分析
Feign整合Hystrix組件后,Hystrix默認的超時時間是1秒,如果超過這個時間尚未響應(yīng),將會進入自定義的fallback代碼,往往首次請求會比較慢(由于Ribbon是懶加載的,在首次請求時,才會開始初始化相關(guān)類),這個響應(yīng)時間可能就大于1秒了,出現(xiàn)調(diào)用失敗
3、解決方案
(1)增加Hystrix的超時時間,默認為1s
hystrix: ? command: ? ? default: ? ? ? execution: ? ? ? ? isolation: ? ? ? ? ? thread: ? ? ? ? ? ? timeoutInMilliseconds: ?10000
(2)配置餓加載(推薦使用)
ribbon: ? eager-load: ? ? clients: ?project1,project2 ? ? enabled: ?true
(3)禁用Hystrix超時(不推薦使用)
hystrix: ? command: ? ? default: ? ? ? execution: ? ? ? ? timeout: ? ? ? ? ? enabled: ?false
(4)為fegin全局禁用hystrix(此種方式較為極端,不建議使用)
feign: ?? ?hystrix: ?? ??? ?enabled: false
Feign整合Hystrix之后日志顯示問題
1、解決方案
Feign整合Hystrix之后,當(dāng)調(diào)用失敗會走fallback邏輯,造成日志不顯示,往往我們需要看日志分析原因,進行故障排查。
(1)在application中配置,開區(qū)Feign對Hystrix的支持
feign: ? hystrix: ? ? enabled: true
(2)編寫Feigin的客戶端以及回滾類
在客戶端FeignClient注解配置相對應(yīng)的回滾類,fallbackFactory = LogFallbackFactory.class,name屬性為注冊中心其他服務(wù)的名稱
/** ?* @description:fegin調(diào)用客戶端 ?* ?* @author: LUOYUAN ?* @date: 2019-08-07-10:33 ?* @function: ?*/ @FeignClient(name = "eureka-log",path = "/api/log",fallbackFactory = LogFallbackFactory.class) public interface LogFeignClient { ? ? @RequestMapping(value = "list", method = RequestMethod.GET) ? ? public String logList(); }
/** ?* @description:feign調(diào)用失敗邏輯 ?* @author: LUOYUAN ?* @date: 2019-08-07-10:34 ?* @function: ?*/ @Slf4j public class LogFallbackFactory implements FallbackFactory<LogFeignClient> { ? ? @Override ? ? public LogFeignClient create(Throwable throwable) { ? ? ? ? return new LogFeignClient() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public String logList() { ? ? ? ? ? ? ? ? log.info("query log fallback reason was:",throwable); ? ? ? ? ? ? ? ? return null; ? ? ? ? ? ? } ? ? ? ? }; ? ? } }
調(diào)用失敗會打印異常信息
query log fallback reason was:
feign.RetryableException: Connection refused: connect executing GET http://eureka-log/api/log/list
at feign.FeignException.errorExecuting(FeignException.java:132)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78)
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:109)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
Feign調(diào)用時定義的fallback不生效
Springcloud Dalston之前得到版本,F(xiàn)eign默認已經(jīng)開啟了Hystrix熔斷器,從Dalaton版本開始,默認關(guān)閉Hystrix支持,需手動開啟,在application.yaml中添加如下配置
feign: ? hystrix: ? ? enabled: true
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用JavaConfig代替xml實現(xiàn)Spring配置操作
這篇文章主要介紹了使用JavaConfig代替xml實現(xiàn)Spring配置操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09java實現(xiàn)無符號數(shù)轉(zhuǎn)換、字符串補齊、md5、uuid、隨機數(shù)示例
這篇文章主要介紹了java實現(xiàn)無符號數(shù)轉(zhuǎn)換、字符串補齊、md5、uuid、隨機數(shù)示例,需要的朋友可以參考下2014-04-04Java實現(xiàn)各種文件類型轉(zhuǎn)換方式(收藏)
這篇文章主要介紹了Java?各種文件類型轉(zhuǎn)換的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03ProtoStuff不支持BigDecimal序列化及反序列化詳解
這篇文章主要為大家介紹了ProtoStuff不支持BigDecimal序列化/反序列化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08Spring項目中使用Junit單元測試并配置數(shù)據(jù)源的操作
這篇文章主要介紹了Spring項目中使用Junit單元測試并配置數(shù)據(jù)源的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09