Method for java microservice iot distributed message gateway
By building a message subscription tree before the microservice module starts, utilizing a Redis cache center and a Nacos registry center, and combining it with the RabbitMQ message middleware server, the problem of low efficiency in IoT message processing is solved, achieving efficient message processing and system load optimization.
Patent Information
- Application Number
- CN202310717228.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-16
- Publication Date
- 2026-02-13
- Estimated Expiration
- 2043-06-16
AI Technical Summary
Existing technologies for IoT message processing suffer from long response times, system complexity, and high pressure, while microservice modules have low message processing efficiency.
By adopting a distributed message gateway approach, a message subscription tree is built before the microservice module starts. This approach utilizes a Redis cache center and a Nacos registry center, combined with the message middleware server RabbitMQ, to achieve efficient lookup and asynchronous processing of message topics, thus simplifying message processing development.
It improves message processing efficiency, reduces response time and system load, achieves decoupling and unified management of microservice functions, and has a wide range of applications.
Smart Images

Figure CN116800824B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of Internet of Things, and particularly relates to a method for a java micro-service Internet of Things distributed message gateway. BACKGROUND
[0002] The Internet of Things connects "all things", and an Internet of Things platform continuously receives a large amount of reporting data from various Internet of Things sensing devices. The information contained in the data needs to be sent to different micro-service modules for further analysis and processing, decision management, for example, sending messages to subscribers. At present, the message processing of the micro-service module adopts a hash table structure, and the response time is long, the system is complex and the pressure is large. Therefore, it is necessary to provide a method for a java micro-service Internet of Things distributed message gateway, which can solve the problem of long response time, complex system and large pressure of Internet of Things message processing in the prior art. SUMMARY
[0003] The present application relates to the technical field of Internet of Things, and particularly relates to a method for a java micro-service Internet of Things distributed message gateway.
[0004] The present application is implemented in the following manner:
[0005] A method for a java micro-service Internet of Things distributed message gateway, comprising the following steps:
[0006] Step 1: starting a micro-service module;
[0007] Step 2: creating a message queue and a message listener;
[0008] Step 3: a message sending end sends a message to a server of a message middleware;
[0009] Step 4: a consumer consumes the message.
[0010] The step 1 comprises the following sub-steps:
[0011] Step 1.1: starting the micro-service module through springboot;
[0012] Step 1.2: when initializing and registering a java bean in the micro-service module, all class files under a specified package path are scanned to find a message subscriber method in the class file which has a custom annotation @Subscribe added on a method;
[0013] Step 1.3: constructing a message topic topic of all message subscriber methods into a message subscription tree, and storing the message subscription tree into a redis cache center;
[0014] Step 1.4: Register the service instance of the microservice module in the nacos registry center;
[0015] Step 1.5: The microservice module is successfully started and runs.
[0016] The step 1.3 includes the following sub-steps:
[0017] Step 1.3.1: Scan the message subscriber method one by one, and parse the attribute information of the custom annotation @Subscribe on the message subscriber method to obtain the message topic of the message subscriber method;
[0018] Step 1.3.2: Split the message topic topic into a topic array by " / ", and start matching from the root node of the message subscription tree, with the initial search depth depth = 0;
[0019] Step 1.3.3: Determine whether the search depth depth is equal to the length of the topic array, if yes, execute step 1.3.4, if no, execute step 1.3.5;
[0020] Step 1.3.4: Add the current message subscriber method to the subscription method list of the current node, and go to step 1.3.9;
[0021] Step 1.3.5: Traverse the list of next level nodes of the current node, and determine whether there is a next level node whose content is equal to the message topic search depth value topic[depth], if yes, execute step 1.3.6, if no, execute step 1.3.7;
[0022] Step 1.3.6: Take the found next level node as the current node, and go to step 1.3.8;
[0023] Step 1.3.7: Create a new node, and take the current message topic search depth value topic[depth] as the content of the new node;
[0024] Step 1.3.8: Add the new node to the subscription method list of the next level nodes of the current node, and take the new node as the current node;
[0025] Step 1.3.9: Search depth depth+1, continue the next round of matching search;
[0026] Step 1.3.10: Repeat steps 1.3.3 to 1.3.9 until the addition of the message topic topic of all message subscriber methods is completed, and the message subscription tree is formed.
[0027] The step 2 includes the following sub-steps:
[0028] The step 2 includes the following sub-steps:
[0029] Step 2.1: The microservice module introduces the dependency of the message middleware.
[0030] Step 2.2: When the microservice module starts, a message queue is automatically created on the server of the message middleware with the service instance name of the microservice module, an exchange is created on the server of the message middleware with the message topic, and the message queue is bound to the exchange.
[0031] Preferably, the server of the message middleware can adopt the prior art message middleware server RabbitMQ.
[0032] Step 2.3: A custom message listener class is defined, and the interface of the message listener is implemented. When the microservice module starts, a message listener container instance is created, and the message queue and the custom message listener class are registered in the message listener container instance, so as to complete the creation of the message listener and the binding relationship between the message queue and the message listener.
[0033] During the running of the microservice module, a message subscriber can be manually registered by executing the code, the message topic of the message subscriber method is added to the message subscription tree, the redis cache center is updated, and the corresponding message queue is automatically created on the server of the message middleware.
[0034] The step 3 includes the following sub-steps:
[0035] Step 3.1: All registered microservice modules are obtained from the nacos registration center.
[0036] Step 3.2: The microservice modules are traversed, and the message subscription tree corresponding to the microservice module is pulled from the redis cache center in real time.
[0037] Step 3.3: According to the message topic of the sent message, the corresponding subscription method list is quickly matched through the message subscription tree, the message content is serialized and assembled into a ByteBuf by traversing the subscription method list.
[0038] Step 3.4: The ByteBuf of the message is sent to the exchange bound to each message topic topic, and is sent to the queue of the message bound to the exchange exchange, and is pushed to the message listener of the corresponding microservice module, and waits for the consumption of the consumer.
[0039] The step 4 comprises the following sub-steps:
[0040] Step 4.1: The message listener of the microservice module of the consumer receives the message pushed by the server of the message middleware.
[0041] Step 4.2: The message topic of the message is found, and the matching subscription method list is found from the message subscription tree, and the method information of each message subscriber method is obtained by traversing the message method list.
[0042] Step 4.3: The obtained method information is deserialized into the corresponding method parameter type, the subscription method is called through the reflection technology of java, and the consumption processing of the message is completed.
[0043] Compared with the prior art, the present application has the following beneficial effects:
[0044] 1、The present application can automatically build the message subscription tree before the microservice module is started, uses the redis cache center and the nacos registration center, realizes the distributed message gateway, and the message subscription tree is read more and written less, the data structure of the subscription tree is used, the search efficiency of the message topic topic is much higher than that of the hash table structure of the prior art, not only simplifies the development of message processing, but also realizes the decoupling of the microservice function, asynchronous processing, reduces the response time of message processing, reduces the system load pressure, and is beneficial to the unified management of the message processing of the internet of things device.
[0045] 2、The present application can create a message subscriber by adding and analyzing a custom annotation @Subscribe, or create a message subscriber by manually registering, which simplifies the difficulty of distributed message development, and the business only needs to focus on its own business logic, without implementing complex message processing.
[0046] 3、The present application can adapt to the servers of multiple message middleware, without modifying the program, only by introducing the corresponding dependency, further simplifying the difficulty of distributed message development, and having a wide range of applications. DETAILED DESCRIPTION
[0047] Figure 1 is a flowchart of the method for the java microservice internet of things distributed message gateway of the present application;
[0048] Figure 2is a flow chart for constructing a message subscription tree in the method for java microservice internet of things distributed message gateway of the present application;
[0049] Figure 3 is a flow chart for sending end sending message in the method for java microservice internet of things distributed message gateway of the present application;
[0050] Figure 4 is a flow chart for consumer consuming message in the method for java microservice internet of things distributed message gateway of the present application. DETAILED DESCRIPTION
[0051] The present application will be further described below in combination with the drawings and specific embodiments.
[0052] Please refer to the drawings Figure 1 A method for java microservice internet of things distributed message gateway, comprising the following steps:
[0053] Step 1: start the microservice module.
[0054] The step 1 comprises the following sub-steps:
[0055] Step 1.1: start the microservice module through springboot.
[0056] Step 1.2: when the microservice module initializes to register java bean, find the message subscriber method with custom annotation @Subscribe added on the method in the class file by scanning all class files under the specified package path, as shown in the following code:
[0057] @Subscribe(value=TopicConstant.TOPIC_RULE_ENGINE_INSTANCE_START,features={Subscription.Feature.broker})
[0058] public void start(String id){
[0059] ruleInstanceService.start(id);
[0060] }
[0061] Step 1.3: construct the message topic of all message subscriber methods into a message subscription tree, and store the message subscription tree into the redis cache center.
[0062] Please refer to the drawings Figure 2, the step 1.3 comprises the following sub-steps:
[0063] Step 1.3.1: Scan the message subscriber method one by one, and parse the attribute information of the custom annotation @Subscribe on the message subscriber method to obtain the message topic of the message subscriber method.
[0064] Preferably, a tree structure can be used to define the topic, such as: / device / product-1 / device-1 / property / report.
[0065] Step 1.3.2: Split the message topic into a topic array through " / ", and start matching from the root node of the message subscription tree, with the initial search depth depth = 0.
[0066] Step 1.3.3: Determine whether the search depth depth is equal to the length of the topic array, if yes, execute step 1.3.4, if not, execute step 1.3.5.
[0067] Step 1.3.4: Add the current message subscriber method to the subscription method list of the current node, and go to step 1.3.9.
[0068] Step 1.3.5: Traverse the list of next level nodes of the current node, and determine whether there is a next level node whose content is equal to the message topic search depth value topic[depth], if yes, execute step 1.3.6, if not, execute step 1.3.7.
[0069] Step 1.3.6: Take the found next level node as the current node, and go to step 1.3.8.
[0070] Step 1.3.7: Create a new node, and take the current message topic search depth value topic[depth] as the content of the new node.
[0071] Step 1.3.8: Add the new node to the subscription method list of the next level nodes of the current node, and take the new node as the current node.
[0072] Step 1.3.9: Search depth depth+1, continue the next round of matching search.
[0073] Step 1.3.10: Repeat steps 1.3.3 to 1.3.9 until the addition of the message topic topic of all message subscriber methods is completed, and the message subscription tree is constructed.
[0074] Step 1.4: Register the service instance of the microservice module in the nacos registration center.
[0075] Step 1.5: The microservice module is successfully started and running.
[0076] Step 2: Create a message queue and a message listener.
[0077] The step 2 includes the following sub-steps:
[0078] Step 2.1: Introduce the dependency of the message middleware, such as spring-rabbit.jar, into the microservice module.
[0079] Step 2.2: When the microservice module is started, automatically create a message queue queue with the service instance name of the microservice module on the server of the message middleware, create an exchange exchange with a message topic topic on the server of the message middleware, and bind the message queue queue with the exchange exchange.
[0080] Preferably, the server of the message middleware can use the existing message middleware server RabbitMQ.
[0081] Step 2.3: Customize the message listener class and implement the interface of the message listener (org.springframework.amqp.core.MessageListener.java), create a message listener container instance such as SimpleMessageListenterContainer when the microservice module is started, and register the message queue queue and the customized message listener class into the message listener container instance, which can complete the creation of the message listener and the binding relationship between the message queue queue and the message listener.
[0082] When the microservice module is running, the message subscriber can be manually registered by executing the code, the message topic topic of the message subscriber method is added to the message subscription tree, the redis cache center is updated, and the corresponding message queue is automatically created on the server of the message middleware.
[0083] The execution code is as follows:
[0084] public BlockingQueue <reportpropertymessage>accept(){
[0085] String topic=” / device / product-1 / device-1 / property / report”;
[0086] Subscription subscription=Subscription.of("propertyreport",topic,newLinkedBlockingQueue(),Subscription.Feature.values());
[0087] return eventBus.subscribe(subscription,ReportPropertyMessage.class);
[0088] }
[0089] Step 3: The message sending end sends a message to the server of the message middleware.
[0090] Please refer to the accompanying drawings Figure 3 , and the step 3 comprises the following steps.
[0091] Step 3.1: Obtain all registered microservice modules from the nacos registration center.
[0092] Step 3.2: Traverse the microservice module, and pull the message subscription tree corresponding to the microservice module from the redis cache center in real time.
[0093] Step 3.3: According to the message topic of the sent message, the corresponding subscription method list is quickly matched through the message subscription tree, the message content is serialized and assembled into a ByteBuf by traversing the subscription method list.
[0094] Step 3.4: Send the ByteBuf of the message to each exchange exchange bound by the message topic topic, and send it to the message queue queue bound by the exchange exchange, and then push it to the message listener of the corresponding microservice module, and wait for the consumption of the consumer.
[0095] Step 4: The consumer consumes the message.
[0096] Please refer to the accompanying drawings Figure 4 , and the step 4 comprises the following steps.
[0097] Step 4.1: The message listener of the microservice module of the consumer receives the message pushed by the server of the message middleware.
[0098] Step 4.2: Obtain the method information of each message subscriber method by traversing the message method list through the message topic of the message and finding the matching subscription method list from the message subscription tree.
[0099] Step 4.3: Deserializes the obtained method information into the corresponding method parameter type, calls the subscription method through the reflection technology of Java, and completes the consumption processing of the message.
[0100] The above is only a preferred embodiment of the present application, and is not used to limit the protection scope of the application, therefore, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.< / reportpropertymessage>
Claims
1. A method for a Java microservice IoT distributed message gateway, characterized by: Includes the following steps: Step 1: Start the microservice module; Step 2: Create a message queue and message listener; Step 3: The message sender sends a message to the message middleware server; Step 4: Consume messages on the consumer side; Step 1 includes the following sub-steps: Step 1.1: Start the microservice module using Spring Boot; Step 1.2: When registering Java beans during the initialization of the microservice module, the system scans all class files under the specified package path to find message subscriber methods in the class files that have the custom annotation @Subscribe added to their methods; Step 1.3: Construct a message subscription tree from the message topics of all message subscriber methods, and store the message subscription tree in the Redis cache center; Step 1.4: Register the service instances of the microservice module to the Nacos registry; Step 1.5: The microservice module has started successfully and is running; Step 1.3 includes the following sub-steps: Step 1.3.1: Scan each message subscriber method one by one, and parse the attribute information of the custom annotation @Subscribe on the message subscriber method to obtain the message topic of the message subscriber method; Step 1.3.2: Divide the message topic into a topic array by " / ", and start the matching search from the root node of the message subscription tree, with an initial search depth of 0; Step 1.3.3: Determine if the search depth is equal to the length of the topic array. If yes, proceed to step 1.3.4; otherwise, proceed to step 1.3.
5. Step 1.3.4: Add the current message subscriber method to the current node's subscription method list, then proceed to Step 1.3.9; Step 1.3.5: Traverse the list of next-level nodes of the current node, and determine whether there exists a next-level node whose content is equal to the message topic depth value topic[depth]. If it exists, proceed to step 1.3.6; otherwise, proceed to step 1.3.
7. Step 1.3.6: Take the found next-level node as the current node and proceed to step 1.3.8; Step 1.3.7: Create a new node and use the current message topic lookup depth value topic[depth] as the content of the new node; Step 1.3.8: Add the new node to the subscription method list of the next-level node of the current node, and set the new node as the current node; Step 1.3.9: Find the depth by depth+1 and continue the next round of matching search; Step 1.3.10: Repeat steps 1.3.3 to 1.3.9 until all message topics for all message subscriber methods have been added, thus constructing a message subscription tree; Step 2 includes the following sub-steps: Step 2.1: The microservice module adds the dependency of the message middleware; Step 2.2: When the microservice module starts, a message queue is automatically created on the message middleware server with the service instance name of the microservice module, and an exchange is created on the message middleware server with the message topic, and the message queue is bound to the exchange. Step 2.3: Define a custom message listener class and implement the message listener interface. When the microservice module starts, create a message listener container instance and register the message queue and the custom message listener class into the message listener container instance. This completes the creation of the message listener and the binding relationship between the message queue and the message listener. Step 4 includes the following sub-steps: Step 4.1: The message listener of the microservice module on the consumer side receives messages pushed by the message middleware server; Step 4.2: Based on the message topic, find the list of matching subscription methods in the message subscription tree, and traverse the message method list to obtain the method information of each message subscriber method; Step 4.3: Deserialize the obtained method information into the corresponding method parameter types, and use Java reflection technology to call the subscription method to complete the message consumption process.
2. The method for a Java microservice IoT distributed message gateway according to claim 1, characterized in that: In step 2, a message queue is automatically created on the message middleware server according to the message topic of the message subscriber method.
3. The method for a Java microservice IoT distributed message gateway according to claim 1, characterized in that: in When a microservice module is running, message subscribers can be manually registered by executing code. This adds the message topic of the subscriber's method to the message subscription tree and updates the Redis cache center. At the same time, the corresponding message queue is automatically created on the message middleware server.
4. The method for a Java microservice IoT distributed message gateway according to claim 1, characterized in that: Step 3 includes the following sub-steps: Step 3.1: Retrieve all registered microservice modules from the Nacos registry; Step 3.2: Traverse the microservice modules and pull the message subscription tree corresponding to each microservice module from the Redis cache center in real time; Step 3.3: Based on the message topic of the sent message, quickly match the corresponding subscription method list through the message subscription tree, traverse the subscription method list, and serialize and assemble the message content into a ByteBuf; Step 3.4: Send the message's ByteBuf to the exchange bound to each message topic, and then send it to the message queue bound to that exchange. Finally, push it to the message listener of the corresponding microservice module, waiting for consumption by the consumer.
Citation Information
Patent Citations
Multi-container-based asymmetric processing system suitable for ubiquitous power Internet of Things edge terminal
CN111600936A
Artificial intelligence micro-service control method, system and device, storage medium and application
CN114338781A