我遇到一个问题,我有两个工作人员正在运行Node.js群集,而我正在使用for循环向工作人员发送大量请求。这些请求仅针对一个工作人员,因为负载不足以使操作系统需要使用第二个工作人员。但是,当我杀死目标worker(kill worker.pid)时,不是让第二个worker继续接收请求,所有请求都会停止,并且带有for循环的程序会挂起并最终失败/获得异常。
我不想在客户端更改代码,也无法弄清楚如何在服务器端处理此问题。
这是我的服务器端代码:
var cluster = require('cluster');
if(cluster.isMaster){
for(var j=0; j<2; j++)
cluster.fork();
cluster.on('online', function(worker){
console.log('worker ' + worker.process.pid + ' alive');
});
cluster.on('exit', function(worker, code, signal){
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
}
else {
/*Includes and global variables*/
var http = require('http');
http.createServer(function (req, res){
var headers = {};
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = true;
headers["Access-Control-Allow-Headers"] = "X-Requested-With, Access-Control-Allow-Origin, X-HTTP-Method-Override, Content-Type, Authorization, Accept";
res.writeHead(200, headers);
console.log("test");
res.end("Response sent");
}).listen(8070);
}
这是我的客户代码:
for(int i=0;i<2000;i++)
{
payLoad = "{\"otoo\":{\"keyName"+i+"\":\"valueName"+i+"\"}}";
System.out.println(payLoad);
StringEntity reqEntity = new StringEntity(payLoad);
reqEntity.setContentType("application/json");
httpPostRequest.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(httpPostRequest);
System.out.println("----------------------------------------");
System.out.println(httpResponse.getStatusLine());
System.out.println("----------------------------------------");
HttpEntity entity = httpResponse.getEntity();
byte[] buffer = new byte[1024];
if (entity != null) {
InputStream inputStream = entity.getContent();
try {
int bytesRead = 0;
BufferedInputStream bis = new BufferedInputStream(
inputStream);
while ((bytesRead = bis.read(buffer)) != -1) {
chunk = new String(buffer, 0, bytesRead);
System.out.println(chunk);
}
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (RuntimeException runtimeException) {
httpPostRequest.abort();
runtimeException.printStackTrace();
} finally {
try {
inputStream.close();
} catch (Exception ignore) {
}
}
}
}
以下是例外:
org.apache.http.NoHttpResponseException: 173.36.55.252:8070 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at CprWrite.main(CprWrite.java:48)