Java NIO (Non-blocking I/O) cung cấp một mô hình lập trình hiệu quả hơn so với I/O truyền thống bằng cách sử dụng các thành phần như Channel, Buffer, và Selector để quản lý đa kết nối trên cùng một luồng thực thi.
Triển khai Server không chặn
Để xây dựng một máy chủ xử lý nhiều kết nối cùng lúc, chúng ta sử dụng Selector để theo dõi các sự kiện trên các kênh kết nối.
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class AsyncServer {
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.bind(new InetSocketAddress(8080));
serverChannel.configureBlocking(false);
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
if (selector.select() == 0) continue;
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
if (key.isAcceptable()) {
SocketChannel client = serverChannel.accept();
client.configureBlocking(false);
client.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(256));
} else if (key.isReadable()) {
SocketChannel client = (SocketChannel) key.channel();
ByteBuffer buffer = (ByteBuffer) key.attachment();
int bytesRead = client.read(buffer);
if (bytesRead > 0) {
System.out.println("Dữ liệu nhận được: " + new String(buffer.array(), 0, bytesRead));
}
}
keys.remove();
}
}
}
}
Triển khai Client kết nối
Phía client sử dụng SocketChannel để thiết lập kết nối và trao đổi dữ liệu với máy chủ một cách trực tiếp.
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class SimpleClient {
public static void main(String[] args) throws Exception {
SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8080));
String msg = "Hello NIO Server";
channel.write(ByteBuffer.wrap(msg.getBytes()));
ByteBuffer response = ByteBuffer.allocate(256);
channel.read(response);
System.out.println("Phản hồi: " + new String(response.array()).trim());
channel.close();
}
}
Thiết kế hệ thống Pipeline cho DevOps
Một hệ thống pipeline hiện đại cần tối ưu hóa quy trình tự động hóa dựa trên các nguyên lý sau:
- Lưu trữ dữ liệu: Sử dụng các cơ sở dữ liệu quan hệ (PostgreSQL, MySQL) để quản lý lịch sử build, kết quả test và metadata của từng phiên bản.
- Điều phối tác vụ: Sử dụng các workflow engine để quản lý trạng thái, phân chia công việc (task distribution) và xử lý lỗi tự động.
- Quản lý phiên bản: Tích hợp sâu với Git để hỗ trợ quy trình phát triển song song nhiều nhánh (multi-branch) và tích hợp liên tục (CI).
- Kiến trúc mở rộng: Áp dụng mô hình Microservices giúp tách biệt logic xử lý, tăng khả năng mở rộng khi khối lượng công việc tăng đột biến.
- Khả năng quan sát (Observability): Triển khai bộ công cụ tập trung log như ELK Stack (Elasticsearch, Logstash, Kibana) để theo dõi sức khỏe và chẩn đoán lỗi hệ thống kịp thời.