Spring Feign超时设置深入了解

作者:小乞丐程序员 时间:2022-01-17 10:39:06 

Feign其他功能-超时设置

  • Feign 底层依赖于 Ribbon 实现负载均衡和远程调用。

  • Ribbon默认1秒超时。

超时配置:

ribbon:

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

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

Spring Feign超时设置深入了解

在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)

Spring Feign超时设置深入了解

修改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);
}

来源:https://blog.csdn.net/qq_40432598/article/details/129484028

标签:Spring,Feign,超时设置
0
投稿

猜你喜欢

  • Studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解决办法

    2023-06-19 17:19:41
  • Java @Value("${xxx}")取properties时中文乱码的解决

    2023-08-14 06:25:50
  • Java JDK11基于嵌套的访问控制的实现

    2021-07-11 10:02:05
  • java判断字符串是否为数字的方法小结

    2023-11-25 05:54:52
  • 教你使用java实现去除各种空格

    2022-09-21 21:27:07
  • C# Winform实现进度条显示

    2023-09-14 15:47:51
  • Java Collections集合继承结构图_动力节点Java学院整理

    2022-07-10 03:44:53
  • Java Hibernate使用SessionFactory创建Session案例详解

    2022-03-04 06:21:28
  • Spring和Hibernate的整合操作示例

    2023-08-08 11:57:52
  • C#开发WinForm之DataGridView开发详解

    2023-06-25 06:31:35
  • java实现上传图片并压缩图片大小功能

    2023-06-14 22:32:35
  • 浅谈Java中Spring Boot的优势

    2022-12-25 17:36:52
  • MybatisPlus使用Wrapper实现条件查询功能

    2021-11-29 10:21:08
  • Spring Boot 读取静态资源文件的方法

    2023-08-25 02:53:07
  • Hadoop组件简介

    2023-08-20 14:07:00
  • Spring4整合Hibernate5详细步骤

    2022-03-15 16:53:07
  • 自定义log4j日志文件命名规则说明

    2021-11-21 16:55:51
  • Spring MVC全局异常实例详解

    2021-11-19 13:51:26
  • Java 注解学习笔记

    2022-12-25 02:40:54
  • c# 控件截图的简单实例

    2022-01-21 16:43:19
  • asp之家 软件编程 m.aspxhome.com