Socket.D v2.5.12

监听多个协议架构

</> markdown

多协议监听。我们一般采用多服务启动并复用一个监听器,所以业务封装在监听器里:

//定义复用和监听器实例
Listener brokerListener = new BrokerListenerFolkmq()
                .addAccessAll(getAccessMap());
                
//启动 tcp 协议监听服务          
Server brokerServerTcp = SocketD.createServer("sd:tcp")
                .config(c -> c.port(Solon.cfg().serverPort() + 10000)
                        .coreThreads(2)
                        .maxThreads(4)
                        .fragmentHandler(brokerFragmentHandler))
                .listen(brokerListener)
                .start();

//启动 ws 协议监听服务
Server brokerServerWs = SocketD.createServer("sd:ws")
                    .config(c -> c.port(Solon.cfg().serverPort() + 10001)
                            .coreThreads(2)
                            .maxThreads(4)
                            .channelExecutor(brokerServerTcp.getConfig().getChannelExecutor()) //复用通用执行器
                            .fragmentHandler(brokerFragmentHandler))
                    .listen(brokerListener)
                    .start();

此代码抄之 FolkMQ:

https://gitee.com/noear/folkmq/blob/main/folkmq-broker/src/main/java/org/noear/folkmq/broker/mq/BrokerLifecycleBean.java