About java.io.IOException: Broken pipe solution.

About java.io.IOException: Broken pipe solution.

Let's first understand java.io.IOException: Broken pipe
pipe means pipe. The pipe is a data stream, usually data read from a file or network socket. When the pipeline is suddenly closed from the other end, a sudden interruption of data will occur , which is broken

Scenario: Our platform (referred to as A) calls other group platforms (referred to as B platform)

Calling method: httpclient post request

Problem: A calls the B platform interface, and the B platform interface is executed normally, but the return result is very fast and slow (maybe because of their interface). When it is slow, there may be no response time for ***10 minutes***.

Leading to the result: In the case of a large amount of concurrency, the interface of platform A has not received the return result for a long time, and then there are continuous requests coming in, which will cause the thread to block , and it will report java.io.IOException: Broken pipe, and finally It may affect the speed of other interfaces in the project,

The following is our error log
user/getUserDaBiaoScore. This interface calls the B platform interface to respond to slow errors.
Request method: [GET]
Request path: [user/getUserDaBiaoScore]
Request parameters: []
Exception information: [java.io.IOException: Broken pipe]
login/loginOut This interface is an exit interface. It should be affected by the above interface and cause an error.
Request method: [GET]
Request path: [/login/loginOut]
Request parameters: []
Exception information: [java.io.IOException: Broken pipe ]

Solution: Set a time. When the B platform interface does not return a result within seconds, the request will be directly discarded (because this interface is not very important to us, it is just a display of user compliance data).
At first I thought about setting a global timeout, but because the response time of some interfaces in our project is different (some businesses are more complicated and return more data), this method is definitely not feasible.
Because we call the B platform through the http method, we can do tricks in the http method.
Set the timeout time, the following is the code

public static String post(String url, Map < String, Object > paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(10000).setConnectionRequestTimeout(10000)
                    .setSocketTimeout(10000).build();
            //设置超时时间
            //setConnectTimeout:设置连接超时时间,单位毫秒。
            //setConnectionRequestTimeout设置从connect Manager获取Connection 超时时间,单位毫秒。
            //请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
            method.setConfig(requestConfig);
            if (paramsMap != null) {
                ArrayList<NameValuePair> paramList = new ArrayList <NameValuePair> ();
                for (Map.Entry < String, Object > param: paramsMap.entrySet()) {
                    NameValuePair pair=new BasicNameValuePair(param.getKey(),param.getValue().toString());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList,"UTF-8"));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                System.out.println("请求超过10s,抛弃此次请求");
                e.printStackTrace();
            }
        }
        return responseText;

    }

The version of httpclient I use is 4.5.2. The setting time of each version seems to be different, so I won’t explain them one by one here.

Guess you like

Origin blog.csdn.net/ChenLong_0317/article/details/103860269