【问题标题】:how to close feign client request?如何关闭假装客户端请求?
【发布时间】:2021-08-10 09:09:33
【问题描述】:

这是原样代码。

public void send() throws IOException {
   CloseableHttpResponse httpResponse = null;
   CloseableHttpClient httpClient = null;

   try {

   HttpPost post = new HttpPost(url);
   httpResponse = httpClient.execute(post);

   } finally
   {
      closeapi(httpClient);
      closeapi(httpResponse);
   }
}

public static void closeapi(Closeable obj)
{
    if (obj != null)
    {
        try
        {
           obj.close();
        } catch (IOException e)
        {
           logger.error(LP.EXCEPTION, e);
        }
    }
 }

我使用 feign 客户端更改了该代码。像这样。

**[MessageFeignClient.class]**

@FeignClient(
    contextId = "messageClient",
    url = "${url}",
    name = "messageclient",
    configuration = {FeignConfiguration.class, FeignRetryConfiguration.class},
    primary = false
)
public interface MessageClient {
    @PostMapping("write")
    PushMessageResultRdo write(
        @RequestHeader("Key1") String key1,
        @RequestHeader("Key2") String key2,
        @RequestBody PushMessage pushMessage);

    @PostMapping("end")
    PushMessageResultRdo end(
        @RequestHeader("Key1") String key1,
        @RequestHeader("Key2") String key2,
        @RequestBody PushMessage pushMessage);
}

**[MessageService.java]**

@Slf4j
@Component
@RequiredArgsConstructor
public class MessageService {
    private final MessageClient messageClient;

    @Override
    public PushResultRdo apply(PushMessage pushMessage) {
        try {
            return messageClient.write(pushMessage.getKey1(), pushMessage.getKey2(), pushMessage);
        } catch (HystrixRuntimeException e) {
            log.error(e);
        }
        return PushResultRdo.defaultError();
    }
}

功能上没有问题。

但是使用feign调用是没有关闭httpclient的。

响应状态为 200。但请求处于活动状态。

我通过wireshark程序检查了tcp流。

当 messageClient.write 被调用时,之后我期望 TCP [FIN, ACK] 序列。

但是如果使用feign客户端,就没有关闭连接。

我想关闭请求连接。

请大家帮忙。 谢谢!

【问题讨论】:

标签: java spring feign openfeign


【解决方案1】:

这是一个有用的方法,但我认为这不是一个好的解决方案。

我想出了假装选项。

[HttpClientFeignConfiguration.class]

this.connectionManagerTimer.schedule(new TimerTask() {
    public void run() {
       connectionManager.closeExpiredConnections();
    }
}, 30000L, (long)httpClientProperties.getConnectionTimerRepeat());

Feign 客户端有一个关于关闭连接的调度任务。

此任务的周期为 3000 秒(这是默认值。您可以更改周期。例如 feign.httpclient.connectionTimerRepeat = 30)

并且connectionManager调用PoolingHttpClientConnectionManager.class,它检查池是否过期。

[PoolingHttpClientConnectionManager.class]

if (timeToLive > 0L) {
    long deadline = this.created + timeUnit.toMillis(timeToLive);
    this.validityDeadline = deadline > 0L ? deadline : 9223372036854775807L;
} else {
    this.validityDeadline = 9223372036854775807L;
}

Feign 默认的生存时间是 900 秒。所以如果你想在feign调用后立即关闭,那么改变timetolive值和timeToLiveUnit。

feign:
  httpclient:
    timeToLive: 30
#    timeToLiveUnit : MILLISECONDS

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2020-10-07
    • 2023-03-14
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多