BaseIO基于 Java NIO 的異步 IO 框架
BaseIO是基于java nio開發(fā)的一款可快速構(gòu)建網(wǎng)絡(luò)通訊項(xiàng)目的異步IO框架,其以簡(jiǎn)單易用的API和優(yōu)良的性能深受開發(fā)者喜愛。
項(xiàng)目特色
支持協(xié)議擴(kuò)展,已知的擴(kuò)展協(xié)議有:
Redis協(xié)議(僅作測(cè)試),示例:詳見 {baseio-test}
LineBased協(xié)議(基于換行符的消息分割),示例:詳見 {baseio-test}
FixedLength協(xié)議(固定長(zhǎng)度報(bào)文頭),支持傳輸文本和二進(jìn)制數(shù)據(jù)
HTTP1.1協(xié)議(lite),示例: https://www.generallycloud.com/
WebSocket協(xié)議,示例: https://www.generallycloud.com/web-socket/chat/index.html
Protobase(自定義協(xié)議),支持傳輸文本和二進(jìn)制數(shù)據(jù)及混合數(shù)據(jù)
輕松實(shí)現(xiàn)斷線重連(輕松實(shí)現(xiàn)心跳機(jī)制)
支持SSL(jdkssl,openssl)
壓力測(cè)試
超過200W QPS的處理速度(Http1.1,I7-4790,16.04.1-Ubuntu) wrk壓測(cè)
快速入門
Maven引用:
<dependency> <groupId>com.generallycloud</groupId> <artifactId>baseio-all</artifactId> <version>3.2.6.RELEASE</version> </dependency>
Simple Server:
public static void main(String[] args) throws Exception {
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(NioSocketChannel channel, Frame frame) throws Exception {
FixedLengthFrame f = (FixedLengthFrame) frame;
frame.write("yes server already accept your message:", channel.getCharset());
frame.write(f.getReadText(), channel.getCharset());
channel.flush(frame);
}
};
ChannelContext context = new ChannelContext(8300);
ChannelAcceptor acceptor = new ChannelAcceptor(context);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setIoEventHandle(eventHandle);
context.setProtocolCodec(new FixedLengthCodec());
acceptor.bind();
}
Simple Client:
public static void main(String[] args) throws Exception {
IoEventHandle eventHandle = new IoEventHandle() {
@Override
public void accept(NioSocketChannel channel, Frame frame) throws Exception {
FixedLengthFrame f = (FixedLengthFrame) frame;
System.out.println();
System.out.println("____________________" + f.getReadText());
System.out.println();
}
};
ChannelContext context = new ChannelContext(8300);
ChannelConnector connector = new ChannelConnector(context);
context.setIoEventHandle(eventHandle);
context.addChannelEventListener(new LoggerChannelOpenListener());
context.setProtocolCodec(new FixedLengthCodec());
NioSocketChannel channel = connector.connect();
FixedLengthFrame frame = new FixedLengthFrame();
frame.write("hello server!", channel);
channel.flush(frame);
ThreadUtil.sleep(100);
CloseUtil.close(connector);
}
更多樣例詳見 {baseio-test}
評(píng)論
圖片
表情
