当前位置: 代码网 > it编程>编程语言>Java > Feign Client超时时间设置不生效的解决方法

Feign Client超时时间设置不生效的解决方法

2025年04月08日 Java 我要评论
在使用feign client时,可以通过两种方式来设置超时时间:1.针对整个feign client设置超时时间可以在feign client的配置类中通过修改request.options对象来设

在使用feign client时,可以通过两种方式来设置超时时间:

1.针对整个feign client设置超时时间

可以在feign client的配置类中通过修改request.options对象来设置超时时间。request.options对象有两个属性,connecttimeoutmillis用于设置连接超时时间,readtimeoutmillis用于设置读取超时时间。

下面是一个示例:

@configuration
public class feignclientconfig {

    @bean
    public request.options requestoptions() {
        return new request.options(5000, 5000);
    }

}

在上面的示例中,连接超时和读取超时时间都设置为5000毫秒。

针对单个feign接口方法设置超时时间

可以在feign接口方法上使用@feignclient注解的configuration属性来指定一个配置类,然后在配置类中通过修改request.options对象来设置超时时间。

下面是一个示例:

@feignclient(name = "example-client")
public interface exampleclient {

    @getmapping("/example")
    @headers("content-type: application/json")
    @requestline("get /example")
    void getexample(request.options options);

}

//自定义接口超时时间(20秒)
request.options options =new request.options(20, timeunit.seconds,20,timeunit.seconds,true);
// 执行调用
exampleclient .getexample(options );

在上面的示例中,exampleclient接口中的getexample()方法的超时时间被配置为20秒。

需要注意的是,feign client的超时时间设置只对请求的连接和读取阶段有效,对于响应的处理时间是无效的。如果需要设置整个请求-响应的超时时间,可以通过使用hystrix或其他方式来实现。

不生效可能得原因

搜索一下项目里有没有对options 进行重写,如下所示:

 @bean
    public options options() {
        return new options();
    }

options 类

public static class options {
        private final int connecttimeoutmillis;
        private final int readtimeoutmillis;

        public options(int connecttimeoutmillis, int readtimeoutmillis) {
            this.connecttimeoutmillis = connecttimeoutmillis;
            this.readtimeoutmillis = readtimeoutmillis;
        }

        public options() {
            this(10000, 60000);
        }

        public int connecttimeoutmillis() {
            return this.connecttimeoutmillis;
        }

        public int readtimeoutmillis() {
            return this.readtimeoutmillis;
        }
    }

可以看到options 类的默认构造函数里connecttimeout为10000ms,readtimeout为60000ms。你的配置文件里面的设置如果不生效可能就是被覆盖了。

feign:
  client:
    config:
      default:
        connecttimeout: 5000
        readtimeout: 150000

1.我们需要强制重写它才会生效

  @primary
    @bean
    public request.options requestoptions(configurableenvironment env) {
        string connecttime = env.getproperty("feign.client.config.default.connecttimeout");
        string readtime = env.getproperty("feign.client.config.default.readtimeout");
        if (connecttime != null && readtime != null) {
            integer connecttimeout = integer.valueof(connecttime);
            integer readtimeout = integer.valueof(readtime);
            return new request.options(connecttimeout, readtimeout);
        }
        return new request.options();
    }

2.单独针对某个方法设置超时时间。

@feignclient(name = "example-client")
public interface exampleclient {

    @getmapping("/example")
    @headers("content-type: application/json")
    @requestline("get /example")
    void getexample(request.options options,string params);

}

//自定义接口超时时间(20秒)
request.options options =new request.options(20, timeunit.seconds,20,timeunit.seconds,true);
// 执行调用
exampleclient .getexample(options );

到此这篇关于feign client超时时间设置不生效的解决方法的文章就介绍到这了,更多相关feign client超时设置不生效内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com