1. Structure diagram
2. Message Server
The Messaging server (SNS) consists of Http Netty server (HNS) and WebSocket Netty server (WNS) . HNS is implemented using Netty http+xml protocol stack,WNS uses Netty Websocket+json Implementation.
Hns messagetomessagedecoder<t>, and messagetomessageencoder<t> These two codecs base classes and are used for parsing and processing predefined The class of the Data. hns Send a predefined set of messages to the client based on the received results httpxmlresponse type Data.
HNS can Create a link to the Business Server through Httpxmlclient and forward the business request through Httpxmlclienthandler . Httpxmlclienthandler inherits from Simplechannelinboundhandler, which can be implemented by HNS Asynchronous communication with the business Server.
CurrentlyWNSmainly used withWebthe client sideWebSocketcommunications,WNSthrough global variablesGLOBAL.WSCGmaintain the channel information byglobal.appusersMaintain client Connections. WNSdefines a message base classbasemsgthat describes the data information required by the client to initiate the Request. similarly,WNSalso defines aAppuserclass is used to store client information, One thing that must be stated is that the sameAppuserThere may be multiple channels, so theAppuserdefines aChannelidarray that maintains all the channels of the current userID. When a client initiates a connection request, the necessary data includesAppID,userId,cmd,AppIDis a unique identifier for a business server.
3. Business Server
The Business Server is an Http Netty serverthat is built by each application to interact with SNS , in other words, each application needs to start a HNS for SNS Interaction. similarly, The Business Server is also through httpxmlclient to SNS HNS initiate connection request, no longer repeat.
4. Httpxmlserver
Package Com.sns.protocol.http.xml.server;import Java.net.inetsocketaddress;import Com.zehin.sns.protocol.http.xml.codec.httpxmlrequest;import Com.zehin.sns.protocol.http.xml.codec.httpxmlrequestdecoder;import Com.zehin.sns.protocol.http.xml.codec.httpxmlresponseencoder;import Com.zehin.sns.protocol.http.xml.pojo.httprequestmessage;import Io.netty.bootstrap.serverbootstrap;import Io.netty.channel.adaptiverecvbytebufallocator;import Io.netty.channel.channelfuture;import Io.netty.channel.channelinitializer;import Io.netty.channel.channeloption;import Io.netty.channel.EventLoopGroup ; Import Io.netty.channel.simplechannelinboundhandler;import Io.netty.channel.nio.nioeventloopgroup;import Io.netty.channel.socket.socketchannel;import Io.netty.channel.socket.nio.nioserversocketchannel;import Io.netty.handler.codec.http.httpobjectaggregator;import Io.netty.handler.codec.http.httprequestdecoder;import Io.netty.handler.codec.http.httpresponseencoder;public class Httpxmlserver implements Runnable {Private Eventloopgroup Bossgroup = null;private Eventloopgroup workergroup = null;private Simplechannelinboundhandler <HttpXmlRequest> handler = null;private int port = 9999; @SuppressWarnings ("unused") private Httpxmlserver () {} Public httpxmlserver (int _port, simplechannelinboundhandler5, Httpxmlserverhandler
Package Com.sns.protocol.http.xml.server;import Static Io.netty.handler.codec.http.httpheaders.iskeepalive;import static Io.netty.handler.codec.http.httpheaders.names.content_type;import Static Io.netty.handler.codec.http.httpresponsestatus.internal_server_error;import Static Io.netty.handler.codec.http.httpversion.http_1_1;import com.zehin.sns.protocol.http.xml.codec.HttpXmlRequest; Import Com.zehin.sns.protocol.http.xml.codec.httpxmlresponse;import Com.zehin.sns.protocol.http.xml.pojo.httprequestmessage;import Com.zehin.sns.protocol.http.xml.pojo.httpresponsemessage;import Io.netty.buffer.unpooled;import Io.netty.channel.channelfuture;import Io.netty.channel.channelfuturelistener;import Io.netty.channel.channelhandler.sharable;import Io.netty.channel.channelhandlercontext;import Io.netty.channel.simplechannelinboundhandler;import Io.netty.handler.codec.http.defaultfullhttpresponse;import Io.netty.handler.codec.http.fullhttpresponse;import Io.netty.handler.codec.http.httprequest;import Io.netty.handler.codec.http.httpresponsestatus;import Io.netty.util.charsetutil;import Io.netty.util.concurrent.future;import io.netty.util.concurrent.GenericFutureListener; @Sharablepublic Final class Httpxmlserverhandler extends simplechannelinboundhandler6. Remarks
The main reference is "netty authoritative guide" and wrote a simple message forwarding.
Netty Implementing a message Forwarding service