spring-cloud-starter-openfeign 4.0.4配置超时时间和默认重试机制,测试超时场景

配置超时时间application.yml

spring:
  cloud:
    openfeign:
      client:
        config:
          testClient:
            connectTimeout: 1000
            readTimeout: 10000
            loggerLevel: basic
            retryer: feign.Retryer.Default

配置openfeignclient日志打印

logging:
  level:
    root: DEBUG

openfeignclient服务

@Controller
public class TestController {
    @Autowired
    private TestClient testClient;

    @GetMapping("/delay/test")
    public ResponseEntity<String> testdelay() {
        testClient.delay();
        return ResponseEntity.ok("test  Delayed response");
    }
}
@FeignClient(url = "http://localhost:8081/", name="testClient")
public interface TestClient {
    @GetMapping(value = "/delay", headers = {"Content-Type=application/json;charset=UTF-8"})
    void delay();
}
@SpringBootApplication
@EnableAuroraFeignClients
public class OpenFeignCilentApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenFeignCilentApplication.class, args);
    }

}

mock dealy服务

@RestController
public class DelayController {

    @GetMapping("/delay")
    public ResponseEntity<String> delayEndpoint() throws InterruptedException {
        Thread.sleep(50000);
        return ResponseEntity.ok("Delayed response");
    }
}
@SpringBootApplication
public class MockDelayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MockDelayApplication.class, args);
    }

}

测试Connect timed out

配置@FeignClient的url中的ip地址为不可路由的IP地址,如192.168.8.8,openfeignclient配置connectTimeout为10秒,

测试结果

"http://192.168.8.8:8080/  尝试连接到不可路由的IP地址,例如192.168.8.8。 Connect timed out
2024-03-14T19:08:24.397+08:00  INFO 30452 --- [io-18009-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-03-14T19:08:24.397+08:00  INFO 30452 --- [io-18009-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
2024-03-14T19:08:24.413+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://192.168.8.8:8080/delay HTTP/1.1
2024-03-14T19:08:25.423+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Connect timed out (1009ms)
2024-03-14T19:08:25.580+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING
2024-03-14T19:08:25.580+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://192.168.8.8:8080/delay HTTP/1.1
2024-03-14T19:08:26.595+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Connect timed out (1015ms)
2024-03-14T19:08:26.821+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING
2024-03-14T19:08:26.821+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://192.168.8.8:8080/delay HTTP/1.1
2024-03-14T19:08:27.826+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Connect timed out (1005ms)
2024-03-14T19:08:28.166+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING
2024-03-14T19:08:28.166+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://192.168.8.8:8080/delay HTTP/1.1
2024-03-14T19:08:29.167+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Connect timed out (1000ms)
2024-03-14T19:08:29.675+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING
2024-03-14T19:08:29.675+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://192.168.8.8:8080/delay HTTP/1.1
2024-03-14T19:08:30.680+08:00 DEBUG 30452 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Connect timed out (1004ms)
2024-03-14T19:08:30.685+08:00 ERROR 30452 --- [io-18009-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: feign.RetryableException: Connect timed out executing GET http://192.168.8.8:8080/delay] with root cause

java.net.SocketTimeoutException: Connect timed out

测试Read timed out

配置@FeignClient的url中的ip地址和端口都为可访问,接口设置延迟,mock dealy服务设置延迟50秒,openfeignclient配置readTimeout为10秒,

测试结果

2024-03-14T19:01:43.090+08:00  INFO 32740 --- [io-18009-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'

2024-03-14T19:01:43.090+08:00  INFO 32740 --- [io-18009-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'

2024-03-14T19:01:43.090+08:00  INFO 32740 --- [io-18009-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms

2024-03-14T19:01:43.106+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8080/delay HTTP/1.1

2024-03-14T19:01:53.110+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Read timed out (10004ms)

2024-03-14T19:01:53.269+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:01:53.269+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8080/delay HTTP/1.1

2024-03-14T19:02:03.279+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Read timed out (10009ms)

2024-03-14T19:02:03.505+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:02:03.505+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8080/delay HTTP/1.1

2024-03-14T19:02:13.512+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Read timed out (10006ms)

2024-03-14T19:02:13.850+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:02:13.850+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8080/delay HTTP/1.1

2024-03-14T19:02:23.859+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Read timed out (10008ms)

2024-03-14T19:02:24.368+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:02:24.368+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8080/delay HTTP/1.1

2024-03-14T19:02:34.374+08:00 DEBUG 32740 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR SocketTimeoutException: Read timed out (10005ms)

2024-03-14T19:02:34.378+08:00 ERROR 32740 --- [io-18009-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: feign.RetryableException: Read timed out executing GET http://localhost:8080/delay] with root cause



java.net.SocketTimeoutException: Read timed out

测试Connection refused

配置@FeignClient的url中的ip地址为可访问,端口不可访问,

测试结果

2024-03-14T19:10:36.085+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8081/delay HTTP/1.1

2024-03-14T19:10:36.091+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR ConnectException: Connection refused: no further information (5ms)

2024-03-14T19:10:36.258+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:10:36.258+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8081/delay HTTP/1.1

2024-03-14T19:10:36.259+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR ConnectException: Connection refused: no further information (0ms)

2024-03-14T19:10:36.486+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:10:36.486+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8081/delay HTTP/1.1

2024-03-14T19:10:36.487+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR ConnectException: Connection refused: no further information (0ms)

2024-03-14T19:10:36.825+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:10:36.825+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8081/delay HTTP/1.1

2024-03-14T19:10:36.826+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR ConnectException: Connection refused: no further information (0ms)

2024-03-14T19:10:37.333+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> RETRYING

2024-03-14T19:10:37.333+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] ---> GET http://localhost:8081/delay HTTP/1.1

2024-03-14T19:10:37.357+08:00 DEBUG 28072 --- [io-18009-exec-1] c.e.openfeigncilent.client.TestClient    : [TestClient#delay] <--- ERROR ConnectException: Connection refused: no further information (23ms)

2024-03-14T19:10:37.362+08:00 ERROR 28072 --- [io-18009-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: feign.RetryableException: Connection refused: no further information executing GET http://localhost:8081/delay] with root cause



java.net.ConnectException: Connection refused: no further information
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值