解决openFeign超时的异常

1.异常信息

使用openFeign远程调用时出现如下异常

feign.RetryableException: Read timed out executing GET http://service-user/...

2.原因

因为默认时间太短,我们可以配置连接时间

3.解决方法

编写配置类:

package com.gh.config;

import feign.Request;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author Eric
 * @Date 2021/6/27 17:27
 * @Version 1.0
 */
@Configuration
public class ServiceFeignConfiguration {
    @Value("${service.feign.connectTimeout:60000}")
    private int connectTimeout;

    @Value("${service.feign.readTimeOut:60000}")
    private int readTimeout;

    @Bean
    public Request.Options options() {
        return new Request.Options(connectTimeout, readTimeout);
    }
}

在远程调用时使用配置类:

例如:

package com.gh.client;

import com.gh.config.ServiceFeignConfiguration;
import com.gh.model.user.Patient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Author Eric
 * @Date 2021/6/26 16:32
 * @Version 1.0
 */
@Component
@FeignClient(value="service-user",configuration = ServiceFeignConfiguration.class)
public interface PatientFeignClient {

    @GetMapping("/api/user/patient/inner/get/{id}")
    public Patient getPatientById(@PathVariable("id") Long id);
}

 

解决openFeign超时的异常

上一篇:Sleuth简介


下一篇:离线数仓(十)