美文网首页
OpenFegin超时控制

OpenFegin超时控制

作者: CodeYang | 来源:发表于2021-08-28 17:49 被阅读0次

OpenFegin超时控制

消费者去调用微服务提供方,肯定会出现超时

测试超时

一、修改服务提供者 Controller ,睡眠一段时间

@RestController
public class ProviderController {
    /**
     * 测试超时
     * @param name
     * @return
     */
    @RequestMapping("/provider/timeout/{name}")
    public String testTimeOut(@PathVariable("name") String name){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return name+" Welcome  - ProviderController "+port;
    }
}

二、消费者新增超时接口,新增超时 Controller 方法

  1. 新增超时接口
@Component
@FeignClient(value = "CLOUD-PROVIDER") //服务名
public interface OpenFeginService {
   @GetMapping(value = "/provider/timeout/{name}")
    public String testTimeOut(@PathVariable("name") String name);
}
  1. 新增超时 Controller 方法
@RestController
public class OpenFeginController {

    @Resource
    private OpenFeginService service;

    @GetMapping("/consumer/timeout/{name}")
    public String testTimeOut(@PathVariable("name") String name){
        return service.testTimeOut(name)+"| OpenFegin";
    }
}

三、测试消费方调用服务提供方。

OpenFegin 默认超时时间为 1 秒钟,但是服务提供者处理需要超过一秒钟,导致Fegin 客户端不想等待了,这时就直接返回会报错。

测试结果.png

四、避免这种情况的产生,我们可以通过 yml 文件修改。

# feign配置
feign:
  client:
    config:
      service-provider-exception:   # 提供方的服务名
        connect-timeout: 5000       # 连接超时时间,默认2s,设置单位为毫秒
        read-timeout: 6000          # 请求处理超时时间,默认5s,设置单位为毫秒。

五、再次测试,发现正常访问。

测试结果.png

相关文章

网友评论

      本文标题:OpenFegin超时控制

      本文链接:https://www.haomeiwen.com/subject/uzyhiltx.html