1.TimeServer.java
import com.ghq.netty.bio.TimeServerHandler;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author ${user}
* @Description:
* @Date: Created in 13:58 2018/6/28
* @Modified By:
*/
public class TimeServer {
public static void main(String[] args) throws IOException{
int port = 8888;
try{
if(args != null && args.length>0){
port = Integer.valueOf(args[0]);
}
}catch (NumberFormatException e){
e.printStackTrace();
}
ServerSocket server = null;
try{
server = new ServerSocket(port);
System.out.println("this time server is start in port : "+port);
Socket socket = null;
TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(50,10000);
while (true){
socket = server.accept();
singleExecutor.execute(new TimeServerHandler(socket));
}
}finally {
if(server != null){
System.out.println("the time server close");
server.close();
server = null;
}
}
}
}
2.TimeServerHandlerExecutePool.java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池,避免每次都新的创建线程。
*
* 由于线程池和消息队列都是有界的,因此无论客户端并发链接数多大,
* 它都不会导致线程个数过于膨胀或者内存溢出,相比于传统的一链接一线程,是一种改良
* @author
* @Description:
* @Date: Created in 14:02 2018/6/28
* @Modified By:
*/
public class TimeServerHandlerExecutePool {
private ExecutorService executor;
public TimeServerHandlerExecutePool(int maxPoolSize, int queueSize) {
executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
maxPoolSize, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(queueSize));
}
public void execute(java.lang.Runnable task){
System.out.println(Thread.currentThread().getName());
executor.execute(task);
}
}
其他的两个类参考
分析问题:
先看socket输入流:
/**
* Reads some number of bytes from the input stream and stores them into
* the buffer array <code>b</code>. The number of bytes actually read is
* returned as an integer. This method blocks until input data is
* available, end of file is detected, or an exception is thrown.
*
* <p> If the length of <code>b</code> is zero, then no bytes are read and
* <code>0</code> is returned; otherwise, there is an attempt to read at
* least one byte. If no byte is available because the stream is at the
* end of the file, the value <code>-1</code> is returned; otherwise, at
* least one byte is read and stored into <code>b</code>.
*
* <p> The first byte read is stored into element <code>b[0]</code>, the
* next one into <code>b[1]</code>, and so on. The number of bytes read is,
* at most, equal to the length of <code>b</code>. Let <i>k</i> be the
* number of bytes actually read; these bytes will be stored in elements
* <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
* leaving elements <code>b[</code><i>k</i><code>]</code> through
* <code>b[b.length-1]</code> unaffected.
*
* <p> The <code>read(b)</code> method for class <code>InputStream</code>
* has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
*
* @param b the buffer into which the data is read.
* @return the total number of bytes read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached.
* @exception IOException If the first byte cannot be read for any reason
* other than the end of the file, if the input stream has been closed, or
* if some other I/O error occurs.
* @exception NullPointerException if <code>b</code> is <code>null</code>.
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
This method blocks until input data is available, end of file is detected, or an exception is thrown 当socket的输入流进行读取操作的时候,它会一直阻塞下去,知道发生如下三种事件:
1.有数据可读
2.可用数据已经读完
3.发生空指针或者IO异常
这意味着当对方发送请求或者应答消息比较缓慢,或者网络传输较慢时,读取输入流一方的通信线程将被长时间阻塞,如果对方要60s下能够将数据发送完成,读取一方的IO线程也将会被阻塞60s,在此期间,其他接入消息只能在消息队列中排队。
再看socket输出流:
/**
* Writes <code>b.length</code> bytes from the specified byte array
* to this output stream. The general contract for <code>write(b)</code>
* is that it should have exactly the same effect as the call
* <code>write(b, 0, b.length)</code>.
*
* @param b the data.
* @exception IOException if an I/O error occurs.
* @see java.io.OutputStream#write(byte[], int, int)
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
问题同输入流。
伪异步IO的问题:
如果通信对方返回应答时间过长会引起的级联故障。
1. 服务器处理缓慢,返回应答消息耗费60s,平时只需要10ms
2. 采用伪异步IO的线程正在读取故障服务节点的响应,由于读取输入流是阻塞的,它将会被同步阻塞60s
3. 假设所有的可用线程都被故障服务器阻塞,那后续所有的IO消息都将在队列中排队。
4. 由于线程池采用阻塞对象实现,当队列积满之后,后续入队列的操作将被阻塞。
5. 由于前端只有一个acceptor线程接收客户端接入,它被阻塞在线程池的同步阻塞队列之后,新的客户端请求消息将被拒绝,客户端会发生大量链接超时。
6. 由于几乎所有的链接都超时,调用者会认为系统已经崩溃,无法接收新的请求消息。
如何解决这个难题??采用NIO