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. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 04:30:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 04:30:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 04:30:02       82 阅读
  4. Python语言-面向对象

    2024-03-16 04:30:02       91 阅读

热门阅读

  1. 力扣131分隔回文串

    2024-03-16 04:30:02       43 阅读
  2. Docker部署ruoyi前后端分离项目 补充

    2024-03-16 04:30:02       50 阅读
  3. asan 使用

    2024-03-16 04:30:02       42 阅读
  4. 电脑上同时安装多个版本的cuda

    2024-03-16 04:30:02       46 阅读
  5. js计算百分比

    2024-03-16 04:30:02       42 阅读
  6. Spring: SpringBoot MybatisPlus框架动态数据源

    2024-03-16 04:30:02       50 阅读
  7. LLM(大语言模型)常用评测指标-MAP

    2024-03-16 04:30:02       45 阅读
  8. 自然语言处理(NLP)技术

    2024-03-16 04:30:02       44 阅读