Spring Feign超时设置深入了解

Spring Cloud中Feign客户端是默认开启支持Ribbon的,最重要的两个超时就是连接超时ConnectTimeout和读超时ReadTimeout,在默认情况下,也就是没有任何配置下,Feign的超时时间会被Ribbon覆盖,两个超时时间都是1秒

Feign其他功能-超时设置

  • Feign 底层依赖于 Ribbon 实现负载均衡和远程调用。
  • Ribbon默认1秒超时。

超时配置:

ribbon:

ConnectTimeout: 1000 #连接超时时间,毫秒

ReadTimeout: 1000 #逻辑处理超时时间,毫秒

在feign-consumer中设置超时时间(具体代码看上 Feign的快速入门)

server:
  port: 9000

eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
spring:
  application:
    name: feign-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径

#设置Ribbon的超时时间
ribbon:
  ConnectTimeout: 1000 #链接超时时间,默认1s
  ReadTimeout: 3000 #逻辑处理的超时时间 默认1s

provider超时2s测试

Goods goods = goodsservice.findOne(id); //当前现场睡眠2秒 try { Thread.sleep( millis: 2000); }catch (InterruptedException e) { e.printStackTrace(); //java.net.SocketTimeoutException: Read timed out } goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置

Feign其他功能-日志记录

Feign 只能记录 debug 级别的日志信息。

logging: 
     level:   
          com.itheima: debug //包名

定义Feign日志级别Bean

@Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } 

启用该Bean:

@FeignClient(configuration = XxxConfig.class)

修改consumer

#设置当前的日志级别 debug,feign 只支持记录debug级别的日志
logging:
  level:
    com.itheima: debug

package com.itheima.consumer.config; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FeignLogConfig { /** * NONE, 不记录 * BASIC, 记录基本的请求行,响应状态码数据 * HEADERS, 记录基本的请求行,响应状态码数据,记录响应头信息 * FULL 记录完整的请求,响应数据 * @return */ @Bean public Logger.Level level(){ return Logger.Level.FULL; } } 
package com.itheima.consumer.feign; import com.itheima.consumer.config.FeignLogConfig; import com.itheima.consumer.domain.Goods; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; /** *  feign声明式接口 发钱远程调用的 *   String url = "http://FEIGN-PROVIDER/goods/findOne/"+id; *         // 3. 调用方法 *         Goods goods = restTemplate.getForObject(url, Goods.class); * *  1 定义接口 *  2 接口上添加注解 @FeignClient 设置value属性为服务提供的 应用名称 *  3 编写调用接口,接口的声明规则和提供方接口保持一致 *  4 注入该接口对象,调用接口方法完成远程调用 */ @FeignClient(value = "FEIGN-PROVIDER",configuration = FeignLogConfig.class) public interface GoodsFeignClient { @GetMapping("/goods/findOne/{id}") public Goods findGoodsById(@PathVariable("id") int id); } 

到此这篇关于Spring Feign超时设置深入了解的文章就介绍到这了,更多相关Spring Feign超时设置内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Spring Feign超时设置深入了解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java