SpringCloud feign无法注入接口的问题
作者:要争气 时间:2021-09-04 03:26:29
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调用常见问题
注: 本文基于Springcloud Edgware版本
Feign调用首次失败问题
1、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。
它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解,Feign 整合了Ribbon。
2、原因分析
Feign整合Hystrix组件后,Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入自定义的fallback代码,往往首次请求会比较慢(由于Ribbon是懒加载的,在首次请求时,才会开始初始化相关类),这个响应时间可能就大于1秒了,出现调用失败
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之后,当调用失败会走fallback逻辑,造成日志不显示,往往我们需要看日志分析原因,进行故障排查。
(1)在application中配置,开区Feign对Hystrix的支持
feign:
? hystrix:
? ? enabled: true
(2)编写Feigin的客户端以及回滚类
在客户端FeignClient注解配置相对应的回滚类,fallbackFactory = LogFallbackFactory.class,name属性为注册中心其他服务的名称
/**
?* @description:fegin调用客户端
?*
?* @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调用失败逻辑
?* @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;
? ? ? ? ? ? }
? ? ? ? };
? ? }
}
调用失败会打印异常信息
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调用时定义的fallback不生效
Springcloud Dalston之前得到版本,Feign默认已经开启了Hystrix熔断器,从Dalaton版本开始,默认关闭Hystrix支持,需手动开启,在application.yaml中添加如下配置
feign:
? hystrix:
? ? enabled: true
来源:https://lwyzq.blog.csdn.net/article/details/81747191
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot集成SpringSecurity和JWT做登陆鉴权的实现
Android自定义ActionProvider ToolBar实现Menu小红点
![](https://img.aspxhome.com/file/2023/9/98649_0s.gif)
工作中禁止使用Executors快捷创建线程池原理详解
![](https://img.aspxhome.com/file/2023/9/75439_0s.png)
C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题示例
![](https://img.aspxhome.com/file/2023/2/89842_0s.png)
Java实战员工绩效管理系统的实现流程
![](https://img.aspxhome.com/file/2023/1/81941_0s.jpg)
老生常谈C/C++内存管理
Java多线程之条件对象Condition
![](https://img.aspxhome.com/file/2023/7/79837_0s.png)
简述Mybatis增删改查实例代码
![](https://img.aspxhome.com/file/2023/1/65311_0s.png)
mybatis 集合嵌套查询和集合嵌套结果的区别说明
![](https://img.aspxhome.com/file/2023/8/71858_0s.png)
@PathVariable和@RequestParam传参为空问题及解决
![](https://img.aspxhome.com/file/2023/4/62674_0s.png)
JAVA的反射机制你了解多少
![](https://img.aspxhome.com/file/2023/9/60609_0s.jpg)
Java多线程 两阶段终止模式Two-Phase Termination Patter
C++实现softmax函数的面试经验
![](https://img.aspxhome.com/file/2023/1/66321_0s.png)
在SpringBoot中通过jasypt进行加密解密的方法
java中同类对象之间的compareTo()和compare()方法对比分析
Java 继承与多态的深入理解
IntelliJ IDEA2020.1版本更新pom文件自动导包的方法
![](https://img.aspxhome.com/file/2023/2/101972_0s.jpg)
解析Silverlight调用WCF/Rest异常的解决方法
![](https://img.aspxhome.com/file/2023/9/96419_0s.png)
java 指定某个jdk版本方法
SpringBoot中@ConditionalOnBean实现原理解读
![](https://img.aspxhome.com/file/2023/2/82432_0s.png)