I/O 处理对 {}->http://IP:80 的请求时发生异常:打开的文件过多

I/O exception caught when processing request to {}->http://IP:80: Too many open files

我有这个架构,3 个系统:

  1. 后端
  2. 中间件
  3. 前端(客户端应用程序)。

我在后端和中间件之间有 rest web 服务,在前端和中间件之间还有另一个不同的 rest 服务(如下图所示)

如上图所示,客户端调用中间件,中间件调用后端rest服务

现在当我 运行 几个请求时一切正常,但是当我从客户端开始压力测试时(我使用 JMeter 进行此测试并调用中间件服务),我开始收到此异常

我正在使用此代码调用后端服务:

public static String makeHttpPostCall(String url2, String param1, String type,
            String username, String password) throws IOException {

        String response1 = "";


        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials(username, password));

        CloseableHttpClient client = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .build();


            HttpPost post = new HttpPost(url2);

            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
            urlParameters.add(new BasicNameValuePair("param1", param1));
            urlParameters.add(new BasicNameValuePair("type", type));

            post.setEntity(new UrlEncodedFormEntity(urlParameters));

            HttpResponse response = client.execute(post);



            System.out.println("Response Code : "+ response.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));


            String line;
            while ((line = rd.readLine()) != null) {
                response1 += line;
            }
            System.out.println("response1: "+ response1);
            return response1;
    }

谁能告诉我中间件或后端的问题在哪里,为什么会发生?

您没有关闭客户端 http 连接。尝试:

...
 }
            System.out.println("response1: "+ response1);

            client.close();

            return response1;
...

异常说明是尝试打开新的http连接时出现错误。请注意,虽然它是一个网络连接,但对于底层 OS 它只是一个打开的文件描述符。 如果你只打了几个电话,连接可能会被 gc 丢弃,一切似乎都很好。进行压力测试将尽可能多地打开 "files",然后抛出此异常。