底层读写-NIO实战

阅读更多

     最近参与实现底层高并发处理的项目,接收socket客户端的连接请求,然后调用主机服务进行通讯。系统需要满足100TPS(吞吐量(tps)=活动的用户数/响应时间)的性能要求。
     通过查阅相关的文献及网上的资料,最终采用java NIO技术方案来实现。系统上线后各方面性能表现不错,也给我们技术团队更多的信心。以下分享一下Java NIO这块的程序,希望对各位有帮助。
Java NIO:提供多路(non-bloking) 非阻塞式的高伸缩性网络I/O 。
     Java NIO非堵塞技术实际是采取Reactor模式,或者说是Observer模式为我们监察I/O端口,如果有请求过来,会自动通知监听服务,不需要开启多个线程一直监听请求,从外界看,实现了流畅的I/O读写,不堵塞了。
     机制的实现是通过它的Selector当发现某个channel有数据时,会通过SelectorKey来告知我们,从而实现事件和handler的绑定。
     以下是代码部分,出于项目隐私,相关的业务处理已注释掉,但不影响运行。

服务器端:

Java代码   收藏代码
  1. import java.io.IOException;  
  2. import java.net.InetSocketAddress;  
  3. import java.net.ServerSocket;  
  4. import java.nio.ByteBuffer;  
  5. import java.nio.channels.SelectionKey;  
  6. import java.nio.channels.Selector;  
  7. import java.nio.channels.ServerSocketChannel;  
  8. import java.nio.channels.SocketChannel;  
  9. import java.nio.charset.Charset;  
  10. import java.util.Set;  
  11.   
  12.   
  13.   
  14.   
  15. /** 
  16.  * @author think 
  17.  * 
  18.  */  
  19. public class NIOServer implements Runnable{  
  20.     private int port = 7070;  
  21.     // 解码buffer  
  22.     private Charset cs = Charset.forName("gbk");  
  23.     private static Selector selector;  
  24.     public NIOServer() {  
  25.     }  
  26.   
  27.     /** 
  28.      * 启动服务器端,配置为非阻塞,绑定端口,注册accept事件ACCEPT事件:当服务端收到客户端连接请求时,触发该事件 
  29.      * @throws IOException 
  30.      */  
  31.     private void initchannel(){  
  32.         try{  
  33.             //初始化SOCKET通道  
  34.             ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();  
  35.             serverSocketChannel.configureBlocking(false);  
  36.             ServerSocket serverSocket = serverSocketChannel.socket();  
  37.             serverSocket.bind(new InetSocketAddress(port));  
  38.             selector = Selector.open();  
  39.             serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);  
  40.             System.out.println("Init NIOServer listening port:" + port);  
  41.         } catch (Exception e) {  
  42.             System.out.println("Init NIOServer failed! error:"+e.getMessage());  
  43.         }  
  44.           
  45.     }  
  46.   
  47.     /** 
  48.      * 服务器端轮询监听,select方法会一直阻塞直到有相关事件发生或超时 
  49.      */  
  50.     private void listen() {  
  51.         //初始化SOCKET通道  
  52.         initchannel();  
  53.         while (true) {  
  54.             try {  
  55.                 selector.select();// 返回值为本次触发的事件数  
  56.                 Set selectionKeys = selector.selectedKeys();  
  57.                 for (SelectionKey key : selectionKeys) {  
  58.                     //处理客户端请求  
  59.                     handle(key);  
  60.                 }  
  61.                 selectionKeys.clear();// 清除处理过的事件  
  62.             } catch (Exception e) {  
  63.                 System.out.println("Exit listen port:"+this.port+" error:"+e.getMessage());  
  64.             }  
  65.   
  66.         }  
  67.     }  
  68.   
  69.     /** 
  70.      * 处理不同的事件 
  71.      */  
  72.     private void handle(SelectionKey selectionKey) throws IOException {  
  73.         ServerSocketChannel server = null;  
  74.         SocketChannel client = null;  
  75.         if (selectionKey.isAcceptable()) {  
  76.             /* 
  77.              * 客户端请求连接事件 serversocket为该客户端建立socket连接,将此socket注册READ事件,监听客户端输入 
  78.              * READ事件:当客户端发来数据,并已被服务器控制线程正确读取时,触发该事件 
  79.              */  
  80.             server = (ServerSocketChannel) selectionKey.channel();  
  81.             client = server.accept();  
  82.             //System.out.println("Client IP:"+client.getRemoteAddress().toString());  
  83.             client.configureBlocking(false);  
  84.             client.register(selector, SelectionKey.OP_READ);  
  85.         } else if (selectionKey.isReadable()) {  
  86.             ByteBuffer sBuffer = ByteBuffer.allocate(1024);  
  87.             /* 
  88.              * READ事件,收到客户端发送数据,读取数据后继续注册监听客户端 
  89.              */  
  90.             client = (SocketChannel) selectionKey.channel();  
  91.             sBuffer.clear();  
  92.   
  93.             int n = -1;  
  94.             try {  
  95.                 n = client.read(sBuffer);  
  96.                 //log.info("输入字节数:" + n);  
  97.             } catch (Exception e) {  
  98.                 n=-1;  
  99.                 client.close();  
  100.                 selectionKey.cancel();  
  101.             }  
  102.             if (n > 0) {  
  103.                 sBuffer.flip();  
  104.                 String receiveText = String.valueOf(cs.decode(sBuffer).array());  
  105.                 //实际业务处理部分忽略  
  106.                 channelWriteBytes(client,"Server:" +receiveText);  
  107.             }  
  108.         }  
  109.     }  
  110.   
  111.     @Override  
  112.     public void run() {  
  113.         //线程开启SOCKET监听  
  114.         listen();  
  115.     }  
  116.     /** 
  117.      * 结果回写 
  118.      */  
  119.     public void channelWriteBytes(SocketChannel socketChannel,String result){  
  120.         ByteBuffer byBuffer = ByteBuffer.allocate(10240);  
  121.         byBuffer.put((result).getBytes(Charset.forName("gbk")));  
  122.         byBuffer.flip();  
  123.         // 输出到通道  
  124.         try {  
  125.             socketChannel.write(byBuffer);  
  126.         } catch (IOException e) {  
  127.             System.out.println("NIOServer write back failed:"+e.getMessage());  
  128.         }  
  129.     }  
  130.       
  131.     public static void main(String[] args) throws IOException {  
  132.         NIOServer server = new NIOServer();  
  133.         server.listen();  
  134.     }  
  135.   
  136. }  
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Set;




/**
 * @author think
 *
 */
public class NIOServer implements Runnable{
	private int port = 7070;
	// 解码buffer
	private Charset cs = Charset.forName("gbk");
	private static Selector selector;
	public NIOServer() {
	}

	/**
	 * 启动服务器端,配置为非阻塞,绑定端口,注册accept事件ACCEPT事件:当服务端收到客户端连接请求时,触发该事件
	 * @throws IOException
	 */
	private void initchannel(){
		try{
			//初始化SOCKET通道
			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.configureBlocking(false);
			ServerSocket serverSocket = serverSocketChannel.socket();
			serverSocket.bind(new InetSocketAddress(port));
			selector = Selector.open();
			serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
			System.out.println("Init NIOServer listening port:" + port);
		} catch (Exception e) {
			System.out.println("Init NIOServer failed! error:"+e.getMessage());
		}
		
	}

	/**
	 * 服务器端轮询监听,select方法会一直阻塞直到有相关事件发生或超时
	 */
	private void listen() {
		//初始化SOCKET通道
		initchannel();
		while (true) {
			try {
				selector.select();// 返回值为本次触发的事件数
				Set selectionKeys = selector.selectedKeys();
				for (SelectionKey key : selectionKeys) {
					//处理客户端请求
					handle(key);
				}
				selectionKeys.clear();// 清除处理过的事件
			} catch (Exception e) {
				System.out.println("Exit listen port:"+this.port+" error:"+e.getMessage());
			}

		}
	}

	/**
	 * 处理不同的事件
	 */
	private void handle(SelectionKey selectionKey) throws IOException {
		ServerSocketChannel server = null;
		SocketChannel client = null;
		if (selectionKey.isAcceptable()) {
			/*
			 * 客户端请求连接事件 serversocket为该客户端建立socket连接,将此socket注册READ事件,监听客户端输入
			 * READ事件:当客户端发来数据,并已被服务器控制线程正确读取时,触发该事件
			 */
			server = (ServerSocketChannel) selectionKey.channel();
			client = server.accept();
			//System.out.println("Client IP:"+client.getRemoteAddress().toString());
			client.configureBlocking(false);
			client.register(selector, SelectionKey.OP_READ);
		} else if (selectionKey.isReadable()) {
			ByteBuffer sBuffer = ByteBuffer.allocate(1024);
			/*
			 * READ事件,收到客户端发送数据,读取数据后继续注册监听客户端
			 */
			client = (SocketChannel) selectionKey.channel();
			sBuffer.clear();

			int n = -1;
			try {
				n = client.read(sBuffer);
				//log.info("输入字节数:" + n);
			} catch (Exception e) {
				n=-1;
				client.close();
				selectionKey.cancel();
			}
			if (n > 0) {
				sBuffer.flip();
				String receiveText = String.valueOf(cs.decode(sBuffer).array());
				//实际业务处理部分忽略
				channelWriteBytes(client,"Server:" +receiveText);
			}
		}
	}

	@Override
	public void run() {
		//线程开启SOCKET监听
		listen();
	}
	/**
     * 结果回写
     */
    public void channelWriteBytes(SocketChannel socketChannel,String result){
    	ByteBuffer byBuffer = ByteBuffer.allocate(10240);
    	byBuffer.put((result).getBytes(Charset.forName("gbk")));
		byBuffer.flip();
		// 输出到通道
		try {
			socketChannel.write(byBuffer);
		} catch (IOException e) {
			System.out.println("NIOServer write back failed:"+e.getMessage());
		}
    }
    
	public static void main(String[] args) throws IOException {
		NIOServer server = new NIOServer();
		server.listen();
	}

}


客户端进行调用即可:

Java代码   收藏代码
  1. import java.net.Socket;  
  2.   
  3. /** 
  4.  * @author think 
  5.  */  
  6.   
  7. public class Client{  
  8.      public static void main(String[] args) {  
  9.             try {  
  10.                 Socket socket = new Socket("127.0.0.1"7070);  
  11.                 String res = null;  
  12.                 try {  
  13.                     String request = "NIO Test...";  
  14.                     byte[] bts = request.getBytes();  
  15.                     socket.getOutputStream().write(bts);  
  16.                     socket.getOutputStream().flush();             
  17.                     byte[] resb = new byte[60000];  
  18.                     socket.getInputStream().read(resb);  
  19.                     res = new String(resb);  
  20.                     System.out.println(res);  
  21.                 } catch (Exception e) {  
  22.                     e.printStackTrace();  
  23.                 }  
  24.             } catch (Exception e) {  
  25.                 // TODO Auto-generated catch block  
  26.                 e.printStackTrace();  
  27.             }  
  28.         }  
  29. }  
import java.net.Socket;

/**
 * @author think
 */

public class Client{
	 public static void main(String[] args) {
			try {
				Socket socket = new Socket("127.0.0.1", 7070);
				String res = null;
				try {
					String request = "NIO Test...";
					byte[] bts = request.getBytes();
					socket.getOutputStream().write(bts);
					socket.getOutputStream().flush();			
					byte[] resb = new byte[60000];
					socket.getInputStream().read(resb);
					res = new String(resb);
					System.out.println(res);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}


      其实实现系统优异的高并发性能,还涉及到更多的方面去学习。例如并发锁的控制,及线程安全的问题。

你可能感兴趣的:(socket,java)