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

标签:SpringCloud,feign,注入接口
0
投稿

猜你喜欢

  • SpringBoot集成SpringSecurity和JWT做登陆鉴权的实现

    2023-01-29 09:34:57
  • Android自定义ActionProvider ToolBar实现Menu小红点

    2022-09-09 05:07:30
  • 工作中禁止使用Executors快捷创建线程池原理详解

    2021-11-24 20:55:48
  • C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题示例

    2021-10-05 16:28:14
  • Java实战员工绩效管理系统的实现流程

    2021-11-20 19:04:28
  • 老生常谈C/C++内存管理

    2022-05-07 02:17:10
  • Java多线程之条件对象Condition

    2021-09-25 15:15:30
  • 简述Mybatis增删改查实例代码

    2023-03-06 18:07:53
  • mybatis 集合嵌套查询和集合嵌套结果的区别说明

    2022-10-12 15:17:02
  • @PathVariable和@RequestParam传参为空问题及解决

    2023-01-06 02:27:00
  • JAVA的反射机制你了解多少

    2023-11-29 16:46:38
  • Java多线程 两阶段终止模式Two-Phase Termination Patter

    2023-11-29 04:47:04
  • C++实现softmax函数的面试经验

    2023-06-16 02:07:47
  • 在SpringBoot中通过jasypt进行加密解密的方法

    2023-11-15 21:29:23
  • java中同类对象之间的compareTo()和compare()方法对比分析

    2023-08-15 09:56:57
  • Java 继承与多态的深入理解

    2023-10-05 04:25:41
  • IntelliJ IDEA2020.1版本更新pom文件自动导包的方法

    2023-01-13 17:44:01
  • 解析Silverlight调用WCF/Rest异常的解决方法

    2021-08-19 07:41:37
  • java 指定某个jdk版本方法

    2023-10-20 08:07:28
  • SpringBoot中@ConditionalOnBean实现原理解读

    2023-04-25 14:24:54
  • asp之家 软件编程 m.aspxhome.com