• Spring Cloud OpenFeign - - - > 超时时间配置


    项目源码地址:https://download.csdn.net/download/weixin_42950079/87190349


    Spring Cloud OpenFeign 官方文档地址:https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/#timeout-handling



    OpenFeign超时处理

    OpenFeign 使用两个超时参数

    • connectTimeout 防止由于服务器处理时间过长而阻止调用方。

    • readTimeout 从建立连接时开始应用,并在返回响应时间过长时触发。


    java 方式配置
    import feign.Contract;
    import feign.Logger;
    import feign.Request;
    import org.springframework.context.annotation.Bean;
    
    /**
     * 全局配置:加了@Configuration注解表示全局配置,对所有服务起作用
     * 局部配置:不加@Configuration注解表示局部配置,只针对指定的一个服务起作用
     */
    public class OpenFeignConfig {
    
        // 日志级别配置
        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    
    
        // 契约配置
        @Bean
        public Contract feignContract(){
            return new Contract.Default();
        }
    
    
        // 超时时间配置
        @Bean
        public Request.Options options(){
            /**
             * connectTimeoutMillis: 连接超时时间,默认2s
             * readTimeoutMillis: 请求处理超时时间,默认5s
             */
            return new Request.Options(3000, 2000);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 在 OpenFeign 接口的@FeignClient注解中,通过configuration属性中指定 OpenFeign 的扩展配置类(配置类中可以配置日志级别契约配置超时时间等OpenFeign扩展内容)
    import com.cd.order8010.config.OpenFeignConfig;
    import feign.RequestLine;
    import org.springframework.cloud.openfeign.FeignClient;
    
    @FeignClient(name = "stock-service", path = "/stock", configuration = OpenFeignConfig.class)
    public interface StockFeignService {
    
    
        // 声明要调用的rest接口对应的方法
        @RequestMapping("/reduce")
        public String reduce();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    .yml方式配置
    feign:
        client:
            config:
              stock-service:  # 这里写default就是全局配置。如果是写服务名称(如:stock-service),则是针对某个微服务的配置,即局部配置
                  connect-timeout: 3000  #连接超时时间,默认2s
                  read-timeout: 2000   #请求处理超时时间,默认5s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    连接超时时间 和 请求处理时间 区别

    以服务A 调用 服务B为例:

    • 连接超时时间 (connect-timeout):是指服务A去请求服务B的网络连接时间。

    • 读取超时时间/请求处理超时时间(read-timeout):是指服务A连接上服务B后,服务B处理这个请求并做出响应的时间。


    readTimeout请求处理 超时时间 测试

    由于connectTimeout是网络连接,不方便进行测试。所以这里只测试readTimeout请求处理超时。

    由于readTimeout是请求处理并返回响应超时,也就是发生在服务提供方(服务消费方去调用服务提供方的接口,服务提供方响应的接口处理该请求并做出响应的限制时间)。

    由于上面设置的read-timeout是2秒,所以我们可以在服务提供方的相应接口中使用TimeUnit.SECONDS.sleep(3)让线程睡眠3秒来模拟请求处理超时。

    @RestController
    @RequestMapping("/stock")
    public class StockController {
    
        @Value("${server.port}")
        String port;
    
        @RequestMapping("/reduce")
        public String reduce() throws InterruptedException {
    		// 睡眠3秒
            TimeUnit.SECONDS.sleep(3);
    
            System.out.println("扣减库存");
            return "扣减库存" + port;
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    测试结果

    cd

    cd

  • 相关阅读:
    故障演练的关键要素及重要性
    406. 根据身高重建队列
    2022暑假牛客多校2(G/K/D)
    使用ORL人脸库,通过GRNN网络和HOG特征提取的人脸识别算法matlab仿真
    狄拉克函数及其性质
    ElasticSearch基本用法
    微信支付商户平台-配置密钥/API安全教程
    企业计算机服务器中了locked勒索病毒怎么办,勒索病毒解密恢复
    Tomcat多实例部署和动静分离
    第四代智能井盖传感器:智能井盖位移怎么进行监测
  • 原文地址:https://blog.csdn.net/weixin_42950079/article/details/128066599