我们需要有关使用Java NIO的服务器软件实现的建议

Eri*_*rik 6 java sql multithreading nio

我正在尝试计算我必须构建的服务器上的负载.

我需要创建一个服务器,在SQL数据库中注册了一百万用户.在一周内,每个用户将大约连接3-4次.每次用户启动并下载1-30 MB数据时,可能需要1-2分钟.

上传完成后,将在几分钟内删除.(在计算中更新文本删除错误)

我知道如何制作和查询SQL数据库,但在这种情况下要考虑什么?

Hou*_*ana 5

你想要的是Netty.它是用NIO编写的API,提供了另一个事件驱动模型,而不是经典的线程模型.它不会为每个请求使用一个线程,但它会将请求放入队列中.使用此工具,您每秒最多可以处理250,000个请求.


Far*_*ker 4

我正在使用 Netty 来实现类似的场景。它只是在工作!

这是使用 netty 的起点:

public class TCPListener {
    private static ServerBootstrap bootstrap;

    public static void run(){
        bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                TCPListnerHandler handler = new MyHandler();
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("handler", handler);

                return pipeline;
            }
        });

        bootstrap.bind(new InetSocketAddress(9999));  //port number is 9999
    }

    public static void main(String[] args) throws Exception {
        run();
    }
}
Run Code Online (Sandbox Code Playgroud)

和 MyHandler 类:

public class MyHandler extends SimpleChannelUpstreamHandler {
    @Override
    public void messageReceived(
        ChannelHandlerContext ctx, MessageEvent e) {


        try {
            String remoteAddress = e.getRemoteAddress().toString();
            ChannelBuffer buffer= (ChannelBuffer) e.getMessage();
            //Now the buffer contains byte stream from client.

        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }

        byte[] output; //suppose output is a filled byte array
        ChannelBuffer writebuffer = ChannelBuffers.buffer(output.length);
        for (int i = 0; i < output.length; i++) {
            writebuffer.writeByte(output[i]);
        }


        e.getChannel().write(writebuffer);
    }

    @Override
    public void exceptionCaught(
            ChannelHandlerContext ctx, ExceptionEvent e) {
        // Close the connection when an exception is raised.
        e.getChannel().close();
    }
}
Run Code Online (Sandbox Code Playgroud)