简体   繁体   中英

how to close feign client request?

This is AS-IS code.

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);
        }
    }
 }

And I changed that code using feign client. like this.

**[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();
    }
}

Functionally, there is no problem.

But using feign call is not closed httpclient.

Response status is 200. But request is alived.

I checked tcp stream through wireshark program.

When messageClient.write called, after then I expect TCP [FIN, ACK] sequence.

But If using feign client, there is no connection close.

I want to close request connection.

Anyone help, please. Thank you!

This is a useful way, but I think it's not a good solution.

I figure out feign options.

[HttpClientFeignConfiguration.class]

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

Feign client has a schedule task about closing connections.

This task's period is 3000 seconds(this is default value. you can change period. ex. feign.httpclient.connectionTimerRepeat = 30)

And connectionManager call PoolingHttpClientConnectionManager.class, it checks pool expired.

[PoolingHttpClientConnectionManager.class]

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

Feign default timetoLive is 900 Seconds. So If you want to close immediately after feign call, then change the timetolive value and timeToLiveUnit.

feign:
  httpclient:
    timeToLive: 30
#    timeToLiveUnit : MILLISECONDS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM