Springboot设置接口超时配置

Springboot设置服务超时配置方式

  • 第一种方式 : application.yml or bootstarp.yml 设置 : spring.mvc.async.request-timeout=5000 单位: ms 超时时间 5s
  • 第二种方式 : 重写WebMvcConfigurerAdapter 的configureAsyncSupport方法
@Configuration
public class WebMvcAdapter extends WebMvcConfigurationSupport {
    @Override
    public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(5000);
        configurer.registerCallableInterceptors(timeoutInterceptor());
    }
    @Bean
    public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
        return new TimeoutCallableProcessingInterceptor();
    }
}
  • 第三种: 使用RestTemplate超时,设置配置HttpComponentsClientHttpRequestFactory中的RequestConfig属性
@Configuration
public class BeanConfig {

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(5000);
        requestFactory.setReadTimeout(1000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}
  • 测试结果 : 【需要配置生效,接口结果返回Callable类型】
    1.
    @GetMapping("/jk")
    @ApiOperation("接口测试")
    public Result jk() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        }
        return Result.okWithMsg("测试接口触发操作");
    }
    测试没有效果的,超时没有作用到 
    返回结果 :  {"code":0,"msg":"测试接口触发操作","data":null,"failed":false,"succeed":true}

    2.
    @GetMapping("/jk-Callable")
    @ApiOperation("接口Callable测试")
    public Callable<String> jkCallable() {
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(10000); //this will cause a timeout
                return "接口Callable测试";
            }
        };
    }
    测试是生效的,结论 : 需要配置生效,接口结果返回Callable类型
    返回结果 : {"code":-1,"msg":"服务器异常","data":null,"failed":true,"succeed":false}

微服务接口超时配置

  • 在微服务中,服务与服务调用是用feign调用,我们可以设置相应 ribbon 超时时间以熔断器抛异常接口信息,配置例如: 在这里插入图片描述

服务入口Nginx超时配置

  • Nginx 设置接口请求超时
    在这里插入图片描述

参考文章:

公众号

  • 这个是我的公众号,可以关注下,一起学习,一起提升~
    在这里插入图片描述
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot设置接口超时时间可以通过以下方式实现: 1. 使用RestTemplate发送请求时设置超时时间 RestTemplate是Spring提供的一个用于访问Rest服务的客户端,它支持设置超时时间。可以在发送请求时设置连接超时时间和读取超时时间,如下所示: ```java RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(5000); // 连接超时时间 requestFactory.setReadTimeout(5000); // 读取超时时间 restTemplate.setRequestFactory(requestFactory); ``` 2. 在配置文件中设置全局超时时间 Spring Boot提供了一个配置属性`server.connection-timeout`,可以用于设置全局的连接超时时间。该属性指定的时间单位是毫秒,默认值为30秒。可以在`application.properties`或`application.yml`中进行配置,如下所示: ```yaml server: connection-timeout: 5000 ``` 3. 使用@HystrixCommand注解设置超时时间 如果你在Spring Boot应用中使用了Hystrix断路器,那么可以使用@HystrixCommand注解来设置超时时间。可以在注解中设置`commandProperties`属性,指定超时时间,如下所示: ```java @HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") }) public String hello() { // ... } ``` 以上是几种在Spring Boot设置接口超时时间的方法,你可以根据自己的需求选择适合的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值