简体   繁体   English

ZeroMQ 运行服务器 Java

[英]ZeroMQ running server Java

Im currently trying to develop a server that should basically get incoming data from unknown number of clients at any time.我目前正在尝试开发一个服务器,该服务器基本上可以随时从未知数量的客户端获取传入数据。 Ie the system will have multiple clients to send data and the servers job is basically to just collect the data and save it to a database.即系统将有多个客户端发送数据,服务器的工作基本上只是收集数据并将其保存到数据库中。

  1. Now I want the server to be running continuously.现在我希望服务器持续运行。 Right now the server just receive one client and shuts down.现在服务器只收到一个客户端并关闭。 If i say do an while(true)- loop surrounding the receive and print message part, It doesn't even do it once.如果我说围绕接收和打印消息部分执行 while(true)- 循环,它甚至不会执行一次。

  2. As i might understand REP REQ is not the best pattern for this, what pattern would be the best to just have the server getting incoming data?正如我可能理解的那样,REP REQ 不是最好的模式,让服务器获取传入数据的最佳模式是什么?

  3. Using Curve (aka Ironhouse pattern), the client needs to have the servers certificate, how should lets say a new client that wants to send data to the server, have access to the servers certificate?使用 Curve(又名 Ironhouse 模式),客户端需要拥有服务器证书,假设一个新客户端想要向服务器发送数据,如何访问服务器证书? All examples I've found from the ZMQ Guide or other sites just refer to a local, often same class, server-client application where the client just access the certificate since they're in the same class... But that's not a realistic case.我从 ZMQ 指南或其他站点找到的所有示例都只是指一个本地的、通常是同一个类的服务器-客户端应用程序,其中客户端只访问证书,因为它们在同一个类中......但这并不现实案件。

Appreciate all answers, tips and suggestions.感谢所有的答案,提示和建议。 Thanks.谢谢。

private class ServerTask extends Thread {

        public void run() {

            try(ZContext context = new ZContext()){ //create context
                ZAuth authenticator = new ZAuth(context); //create authenticator for incoming clients
                authenticator.setVerbose(true); //get indication of what the authenticator is deciding
                authenticator.allow("127.0.0.1"); //Whitelisting an adress, all other adresses will be rejected
                //authenticator.configureCurve(CERTIFICATE_FOLDER); //Tell authenticator to use the certificate store in .curve
                //ZCert server_cert = new ZCert(); //Create a server certificate
                //writeServerCertToFile(server_cert); //write the cert so client can use it

                //Create and bind server socket
                ZMQ.Socket server = context.createSocket(SocketType.REP);
                //server.setZAPDomain("global".getBytes());
                //server.setCurveServer(true);
                //server.setCurvePublicKey(server_cert.getPublicKey());
                //server.setCurveSecretKey(server_cert.getSecretKey());
                server.bind("tcp://*:9000");

                //recieve and print message

                String message = server.recvStr(0);
                System.out.println("received message: " + message);
                System.out.println("I should not be printed before the recieved message");



                try {
                    System.out.println("destroys the auth and context");
                    authenticator.close();
                    context.destroy();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
    }

I solved this months ago but if will help someone using ZeroMQ in the future, I ended up using the pull-push pattern with loadbalancing the data to a threadpool of workers.我在几个月前解决了这个问题,但是如果将来可以帮助使用 ZeroMQ 的人,我最终使用了拉推模式,将数据负载平衡到工作线程池。 Also using Inproc for frontend/backend communication.还使用 Inproc 进行前端/后端通信。 That seemed to be the best fit solution.这似乎是最合适的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM