简体   繁体   中英

Netty-TCP-HTTP-MQTT design

I am trying to write a server-side java application that can accept tcp, http and mqtt communication (receive and send/ MongoDB as storage). From research, we decided that it could be a jar application based on Netty and paho for mqtt. We have 3 project using three of these protocols, therefore I am trying to unify the connection module. They each have different protocol style, for example:

-tcp: 0102330123456700

-http: HTTP POST /URL/count {"id":"02","count":"01234567"}

-mqtt: topic /02/count {"count":"01234567"}

Since we are a bit short of time, i am running them three in a silly but quick way---3 different thread listening to 3 different ports.

public class ServerLauncher {
public static void main(String[] args) {
    NettyRestServer nettyRestServer = new NettyRestServer();
    MqttServer mqttServer = new MqttServer();
    EchoServer echoServer =  new EchoServer();

    new Thread(nettyRestServer,"HTTP Listening").start();
    new Thread(mqttServer,"Mqtt Listening").start();
    new Thread(echoServer,"socket Listening").start();
}

}

My questions are:

  1. Since they are all based on tcp, is there a good way to manage them all together without wasting thread resource? maybe running just one thread for listening one port. I only find examples of single protocol.
  2. For data storage, is it an okey design to push all the incoming messages to a concurrentHashMap across all thread/channels. Finally with a another thread running scheduled task, storage this concurrentHashMap into MongoDB and reset. or maybe use queue instead of concurrentHashMap

如果将Netty用于所有这些,则可以为所有服务器共享相同的EventLoopGroup ,这意味着所有服务器都将共享相同的Threads

You don't have to use three threads to start Server. You can do all of these in only one ServerBootstrap. And put the logic in ChannelHandler.

Netty's ChannelPipeline can dynamicly change ChannelHandler when getting the connecttion.

  ctx.pipeline().addBefore(...)
  ctx.pipeline().addAfter(...)
  ctx.pipeline().remove(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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