Disclosure of Invention
The embodiment of the application provides an information publishing method and device based on an MQTT protocol, which can be used for publishing high-performance processing information and saving labor cost and time cost in related products and development processes used by an MQTT protocol-based client.
The embodiment of the application provides an information issuing method based on an MQTT protocol, which is applied to a middleware at a server side, wherein the middleware is driven by a golang language, and the method comprises the following steps:
acquiring an information issuing instruction issued by an issuing terminal;
determining a shared instruction protocol of all topics, wherein the shared instruction protocol indicates an instruction composition specification which is met by instructions issued under each topic;
judging whether the information issuing instruction meets the instruction composition specification;
and if the information issuing instruction meets the instruction composition specification, issuing the information issuing instruction under a target subject to which the information issuing instruction belongs.
In the information issuing method based on MQTT protocol according to the embodiment of the present application, the determining whether the information issuing instruction satisfies the instruction composition specification includes:
analyzing the information issuing instruction, and judging whether the information issuing instruction consists of a theme and a load;
and if the information issuing instruction consists of the theme and the load, judging that the information issuing instruction meets the instruction composition specification.
In the information issuing method based on the MQTT protocol according to the embodiment of the present application, if the information issuing instruction is composed of the topic and the load, it is determined that the information issuing instruction satisfies the instruction composition specification, where the method includes:
and if the information issuing instruction consists of the theme and the load, and the theme consists of a project identifier, a terminal identifier and a service identifier, judging that the information issuing instruction meets the instruction composition specification.
In the MQTT protocol-based information issuing method according to the embodiment of the present application, if the information issuing instruction satisfies the instruction composition specification, issuing the information issuing instruction on a target topic to which the information issuing instruction belongs includes:
and if the information issuing instruction consists of the theme and the load, and the theme consists of the project identifier, the terminal identifier and the service identifier, issuing the information issuing instruction under a target theme to which the information issuing instruction belongs.
In the information issuing method based on MQTT protocol according to the embodiment of the present application, after determining whether the information issuing instruction satisfies the instruction composition specification, the method further includes:
if the information issuing instruction does not meet the instruction composition specification, the information issuing instruction is not issued under the target subject to which the information issuing instruction belongs;
and sending the sharing instruction protocol to the issuing terminal, and triggering the issuing terminal to update the instruction protocol used by the information issuing instruction into the sharing instruction protocol.
In the MQTT protocol-based information issuing method according to the embodiment of the present application, sending the shared instruction protocol to the issuing terminal, and triggering the issuing terminal to update the instruction protocol used by the information issuing instruction to the shared instruction protocol further includes:
acquiring first data information of a subscription terminal, and storing the first data information in a redis database of the golang language for later use based on a cache long connection function of the golang language.
In the information publishing method based on the MQTT protocol according to the embodiment of the present application, the obtaining first data information of the subscriber terminal, and storing the first data information in a redis database of the golang language for use based on a cache length connection function of the golang language, includes:
and acquiring first data information of the subscription terminal, and storing the first data information in a redis database of the golang language for standby use based on a translport protocol cached in a net dependency library of the golang language in advance.
In the information issuing method based on MQTT protocol according to the embodiment of the application, the obtaining of the information issuing instruction issued by the issuing terminal includes
And starting an interception port through the middleware, and acquiring the information issuing instruction through the interception port.
In the information issuing method based on MQTT protocol according to the embodiment of the present application, before issuing the information issuing instruction on the target topic to which the information issuing instruction belongs, the method further includes:
and reloading the information release instruction so that the reloaded information release instruction can be identified by the subscriber terminal.
The embodiment of the present application also provides an information issuing device based on MQTT protocol, the device includes:
the acquisition module is used for acquiring an information issuing instruction issued by the issuing terminal;
the system comprises a determining module, a judging module and a judging module, wherein the determining module is used for determining a sharing instruction protocol of all topics, and the sharing instruction protocol indicates an instruction composition specification which is met by instructions issued under all topics;
the judging module is used for judging whether the information issuing instruction meets the instruction composition specification;
and the issuing module is used for issuing the information issuing instruction under a target subject to which the information issuing instruction belongs if the information issuing instruction meets the instruction composition specification.
According to the embodiment of the application, the golang language drive middleware is adopted, the natural advantages of the golang language are utilized, the issuing of the messages can be processed in a high-performance mode, the long connection is stable and efficient due to the cache long connection function of the golang language, and the disaster situation that a large amount of go routines are concurrent and a CPU is full due to special reasons (such as useless data) is avoided. In addition, through unified issuing of instruction composition specifications of instructions under various topics, labor cost and time cost can be saved in related products and development processes used by the client based on the mqtt protocol.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application. It is to be understood that the embodiments described are only a few embodiments of the present application and not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Referring to fig. 1, fig. 1 is a schematic flowchart illustrating an information publishing method based on MQTT protocol according to an embodiment of the present application. The method may comprise the steps of:
step 101, obtaining an information issuing instruction issued by an issuing terminal.
Among them, MQTT (Message Queuing Telemetry Transport) is a "lightweight" communication protocol based on publish/subscribe (publish/subscribe) mode, and for this purpose, it needs a Message middleware, which is built on TCP/IP protocol and published by IBM in 1999. The MQTT has the greatest advantage that a real-time reliable message service can be provided for connecting remote devices with few codes and limited bandwidth. As an instant messaging protocol with low cost and low bandwidth occupation, the method has wide application in the aspects of Internet of things, small-sized equipment, mobile application and the like.
MQTT is a client-server based message publish/subscribe transport protocol. The MQTT protocol is lightweight, simple, open, and easy to implement, which makes it very versatile. In many cases, including in restricted environments, such as: machine to machine (M2M) communication and internet of things (IoT). It has found widespread use in communication sensors via satellite links, occasionally dial-up medical devices, smart homes, and some miniaturized devices.
The MQTT protocol is realized by communication between a client and a server, and in the communication process, the MQTT protocol has three identities: publisher (Publish), Broker (Broker) (server), subscriber (Subscribe). The message publisher and the message subscriber are clients, the message broker is a server, and the message publisher can be a subscriber at the same time.
The MQTT server, also known as a "message Broker" (Broker), may be an application or a device. It is located between the message publisher and the subscriber, and it can:
(1) accepting a network connection from a client;
(2) receiving application information issued by a client;
(3) processing subscription and unsubscription requests from the client;
(4) the application message is forwarded to the subscribed client.
It should be noted that the publishing terminal in the present application is a publisher in the MQTT, and the subscribing terminal in the present application is a subscriber in the MQTT.
The Gorang language is from Google company, has the advantages of simple grammar, high performance and fast compilation, and has concurrent and asynchronous programming due to two godets of coroutine and Channel, high support for concurrency, a programming language which is used for big data, micro service and concurrency, strong compiling inspection, strict coding specification and complete software life cycle, and strong stability, because the Gorang language provides tools for all links of the software life cycle (development, test, deployment, maintenance and the like), such as go tool, gofmt, go test and the like.
The invention can process the release of the message with high performance by adopting the golang language to drive the middleware and utilizing the natural advantages of the golang language.
In some embodiments, the obtaining of the information issue instruction issued by the issue terminal includes:
and starting an interception port through the middleware, and acquiring the information issuing instruction through the interception port.
For example, when the issuing terminal issues an information issuing instruction a, the server opens the listening port through the middleware, and acquires the information issuing instruction a through the listening port.
Step 102, determining a shared instruction protocol of all topics, wherein the shared instruction protocol indicates instruction composition specifications which are met by instructions issued under all topics.
The issuing terminal sends a plurality of information issuing instructions to the server, and the server issues the information issuing instructions under a target theme to which the information issuing instructions belong, but in the prior art, because the composition of the instructions is not standardized and unified, maintenance development cost, labor cost, time cost and the like are increased along with the time in related products and development processes used by the client based on the MQTT protocol due to the lack of strict instruction standards. The invention sets the sharing protocol instructions of all the topics, and uniformly issues the instruction composition specifications of the instructions under all the topics, thereby saving the labor cost and the time cost in related products and development processes used by the client based on the mqtt protocol, achieving the specifications of development and use, being stable and easy to maintain and expand.
Step 103, judging whether the information issuing instruction meets the instruction composition specification.
Because the instruction composition specifications which are commonly met by the instructions issued under each topic are set, when the information issuing instruction issued by the issuing terminal comes in, the server side judges whether the information issuing instruction issued by the issuing terminal meets the instruction composition specifications.
In some embodiments, the determining whether the information issue instruction satisfies the instruction composition specification includes:
analyzing the information issuing instruction, and judging whether the information issuing instruction consists of a theme and a load;
and if the information issuing instruction consists of the theme and the load, judging that the information issuing instruction meets the instruction composition specification.
Where a topic is a tag that is connected to an application message that matches the subscription of the server. The server will send a message to each client subscribing to the matched tag. The payload is the content specifically received by the message subscriber (subscriber terminal).
In some embodiments, if the information issue instruction consists of the topic and the payload, determining that the information issue instruction meets the instruction composition specification includes:
and if the information issuing instruction consists of the theme and the load, and the theme consists of a project identifier, a terminal identifier and a service identifier, judging that the information issuing instruction meets the instruction composition specification.
The item identifier is the name of the item, what the name of the item is called, and what the item identifier is. For example, the item name is a dining item, the item identifier is a dining item, the item name is C, the item identifier is C, the item name is ordering, and the item identifier is ordering.
The terminal number is the terminal number of the publisher. For example, if the terminal number of the publisher is a, the terminal number is a; the terminal number of the publisher is 12, the terminal number is 12.
The service identifier is the name of the service, what the name of the service is called, and what the service identifier is. For example, the service name is meal ordering, and the service identifier is meal ordering; the service name is meal delivery, and the service identifier is meal delivery.
The information issuing instruction satisfies the instruction composition specification only when the information issuing instruction is composed of a topic and a payload, and the topic is composed of a project identifier, a terminal identifier and a service identifier.
And S104, if the information issuing instruction meets the instruction composition specification, issuing the information issuing instruction under a target subject to which the information issuing instruction belongs.
That is, when the information issuing instruction issued by the issuing terminal meets the instruction composition specification, the middleware of the server side issues the information issuing instruction under the target subject to which the information issuing instruction belongs
In some embodiments, after determining whether the information issue instruction satisfies the instruction composition specification, the method further includes:
if the information issuing instruction does not meet the instruction composition specification, the information issuing instruction is not issued under the target subject to which the information issuing instruction belongs;
and sending the sharing instruction protocol to the issuing terminal, and triggering the issuing terminal to update the instruction protocol used by the information issuing instruction into the sharing instruction protocol.
When the information issuing instruction does not meet the instruction composition specification, the server side does not issue the information issuing instruction under the target subject to which the information issuing instruction belongs, and sends the shared instruction protocol to the issuing terminal so as to trigger the issuing terminal to update the instruction protocol used by the information issuing instruction to the shared instruction protocol, so that the situation that the issuing terminal continues to send the information issuing instruction which does not meet the instruction composition specification next time can be avoided.
In some embodiments, the sending the shared instruction protocol to the issuing terminal, and triggering the issuing terminal to update the instruction protocol used by the information issuing instruction to the shared instruction protocol, further includes:
acquiring first data information of a subscription terminal, and storing the first data information in a redis database of the golang language for later use based on a cache long connection function of the golang language.
The first data information may be a heartbeat of the subscriber terminal, an online duration of the subscriber terminal, a broadcast of the subscriber terminal, and the like. The long connection is a connection between the client and the server, so that the purpose of data transmission between the client and the server is achieved, the long connection is a loss connection, resource consumption is needed when the long connection is made every time, and the purpose of caching the long connection is to achieve the effect of recycling, resources are not lost and reconnected, and resources are saved. And storing the first data information in a redis database of the golang language for later use through a cache length connection function of the golang language.
In some embodiments, the obtaining first data information of the subscriber terminal, and storing the first data information in a redis database of the golang language for use based on a cache long connection function of the golang language, includes:
and acquiring first data information of the subscription terminal, and storing the first data information in a redis database of the golang language for standby use based on a translport protocol cached in a net dependency library of the golang language in advance.
The heartbeat and the data broadcast by the high-frequency transmission need a stable system, a prestored transpport protocol in a net dependency library of the golang language is used, so that the cost can be saved, the stability of the system is greatly helped, the heartbeat can be guaranteed, and the data broadcast by the high-frequency transmission has stability.
In some embodiments, said issuing said information issue instruction before said information issue instruction is under a target topic to which said information issue instruction belongs, further comprises:
and reloading the information release instruction so that the reloaded information release instruction can be identified by the subscriber terminal.
The information issuing instruction is reassembled, and the information issuing instruction is mainly combined into an instruction which can be identified by the terminal, so that the information issuing instruction needs to be reassembled before the information issuing instruction is issued under a target subject to which the information issuing instruction belongs, so that the reassembled information issuing instruction can be identified by the subscription terminal.
All the above optional technical solutions may be combined arbitrarily to form optional embodiments of the present application, and are not described herein again.
In particular implementation, the present application is not limited by the execution sequence of the described steps, and some steps may be performed in other sequences or simultaneously without conflict.
As can be seen from the above, the information issuing method based on MQTT protocol provided by the embodiment of the present application obtains the information issuing instruction issued by the issuing terminal; determining a shared instruction protocol of all topics, wherein the shared instruction protocol indicates an instruction composition specification which is met by instructions issued under each topic; judging whether the information issuing instruction meets the instruction composition specification; and if the information issuing instruction meets the instruction composition specification, issuing the information issuing instruction under a target subject to which the information issuing instruction belongs. In other words, in the embodiment of the present application, by using the golang language-driven middleware, the distribution of messages can be processed with high performance by using the natural advantages of the golang language, and the long connection is stable and efficient by adding the cache long connection function of the golang language, so that the disaster situation that a large number of go routines are concurrent and the CPU is full due to special reasons (for example, useless data) can be avoided. In addition, through unified issuing of instruction composition specifications of instructions under various topics, labor cost and time cost can be saved in related products and development processes used by the client based on the mqtt protocol.
Please refer to fig. 2, and fig. 2 is a schematic structural diagram of the MQTT protocol-based information publishing device according to the embodiment of the present application.
The MQTT protocol-basedinformation distribution device 30 may include:
an obtainingmodule 31, configured to obtain an information issuing instruction issued by an issuing terminal;
a determiningmodule 32, configured to determine a shared instruction protocol of all topics, where the shared instruction protocol indicates an instruction composition specification that is satisfied by instructions issued under each topic;
a judgingmodule 33, configured to judge whether the information issue instruction meets the instruction composition specification;
theissuing module 34 is configured to issue the information issuing instruction under a target topic to which the information issuing instruction belongs if the information issuing instruction meets the instruction composition specification.
In some embodiments, the obtainingmodule 31 is configured to obtain first data information of a subscriber terminal, and store the first data information in a redis database of the golang language for use based on a cache length connection function of the golang language.
In some embodiments, the obtainingmodule 31 is configured to obtain the first data information of the subscriber terminal, and store the first data information in a redis database of the golang language for use based on a transferport protocol cached in a net dependency library of the golang language in advance.
In some embodiments, the obtainingmodule 31 is configured to open a listening port through the middleware, and obtain the information issue instruction through the listening port.
In some embodiments, the determiningmodule 33 is configured to parse the information issuing instruction, and determine whether the information issuing instruction consists of a topic and a load; and if the information issuing instruction consists of the theme and the load, judging that the information issuing instruction meets the instruction composition specification.
In some embodiments, the determiningmodule 33 is configured to determine that the information issue instruction meets the instruction composition specification if the information issue instruction consists of the topic and the load, and the topic consists of an item identifier, a terminal identifier, and a service identifier.
In some embodiments, the determiningmodule 33 is configured to issue the information issue instruction under a target topic to which the information issue instruction belongs if the information issue instruction consists of the topic and the load, and the topic consists of a project identifier, a terminal identifier, and a service identifier.
In some embodiments, the issuingmodule 34 is configured to, if the information issuing instruction does not meet the instruction composition specification, not issue the information issuing instruction under a target topic to which the information issuing instruction belongs; and sending the sharing instruction protocol to the issuing terminal, and triggering the issuing terminal to update the instruction protocol used by the information issuing instruction into the sharing instruction protocol.
As can be seen from the above, theinformation issuing device 30 based on MQTT protocol provided in the embodiment of the present application obtains, through the obtainingmodule 31, the information issuing instruction issued by the issuing terminal; the determiningmodule 32 determines a shared instruction protocol of all topics, wherein the shared instruction protocol indicates an instruction composition specification which is met by instructions issued under each topic; the judgingmodule 33 judges whether the information issuing instruction meets the instruction composition specification; theissuing module 34 is configured to issue the information issuing instruction under a target topic to which the information issuing instruction belongs if the information issuing instruction meets the instruction composition specification. According to the embodiment of the application, the golang language drive middleware is adopted, the natural advantages of the golang language are utilized, the issuing of the messages can be processed in a high-performance mode, the long connection is stable and efficient due to the cache long connection function of the golang language, and the disaster situation that a large amount of go routines are concurrent and a CPU is full due to special reasons (such as useless data) is avoided. In addition, through unified issuing of instruction composition specifications of instructions under various topics, labor cost and time cost can be saved in related products and development processes used by the client based on the mqtt protocol.
In the foregoing embodiments, the descriptions of the respective embodiments have respective emphasis, and for parts that are not described in detail in a certain embodiment, reference may be made to related descriptions of other embodiments.
The information publishing method and device based on the MQTT protocol provided by the embodiment of the present application are introduced in detail, a specific example is applied in the present application to explain the principle and the implementation manner of the present application, and the description of the above embodiment is only used to help understand the technical scheme and the core idea of the present application; those of ordinary skill in the art will understand that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications or substitutions do not depart from the spirit and scope of the present disclosure as defined by the appended claims.