Data source switching method and system based on HTTP request
Through the data source switching method based on HTTP requests, dynamically manage and switch data sources, the cumbersome problem of the data source switching process in the prior art is solved, and the flexibility and efficiency of data processing are improved.
Patent Information
- Application Number
- CN202411513464.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-28
- Publication Date
- 2025-02-07
AI Technical Summary
The configuration process of existing multi-data source switching solutions is cumbersome, which limits the dynamic expansion or reduction of data sources, affecting the flexibility and adaptability of the system.
The data source switching method based on HTTP request is adopted, and the device identification in the HTTP request is obtained, and the identification of the data source to be switched is determined in combination with the preset switching relationship, and dynamic switching is performed to support the increase or decrease of the number of data acquisition devices.
It realizes dynamic management and switching of data sources, improves the flexibility and efficiency of data processing, and supports changes in the number of data acquisition devices.
Smart Images

Figure CN119397100A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a data source switching method and system based on HTTP request. Background Art
[0002] In today's industrial production field, the application of multiple data sources has become a norm. Therefore, it is particularly important to realize the dynamic configuration and flexible switching of multiple data sources. By aggregating and centrally processing and displaying information from multiple different data sources, it is not only helpful to discover the differences between different data acquisition devices corresponding to multiple data sources, but also to obtain more timely and comprehensive data. This provides strong support for making accurate decisions based on data, thereby promoting the process of industrial intelligence. In addition, in-depth analysis, mining and application of these aggregated data can further enhance the intrinsic value of the data.
[0003] However, existing multi-data source switching solutions often have cumbersome configuration processes and usually require all data sources to be determined in the early stages of system development, which limits the ability to dynamically expand or reduce data sources in the future, thereby affecting the flexibility and adaptability of the system. Therefore, developing a solution that can simplify configuration and support dynamic management of data sources is crucial to improving the efficiency and effectiveness of industrial data processing. Summary of the invention
[0004] The present invention provides a data source switching method and system based on HTTP request, which is used to solve the defects of cumbersome data source switching process and low data source switching efficiency in the prior art. The method of the present invention supports the increase and decrease of the number of data acquisition devices, and can dynamically and conveniently manage and switch device data sources to improve the flexibility and efficiency of data processing.
[0005] The present invention provides a data source switching method based on HTTP request, comprising: obtaining an HTTP request; the HTTP request includes a device identification of a data acquisition device; the HTTP request is a request for querying data of the data acquisition device; determining a data source identification of a data source to be switched according to the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the corresponding relationship between the device identification and the data source identification; switching the data source according to the data source identification of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0006] According to a data source switching method based on HTTP request provided by the present invention, before obtaining the HTTP request, it also includes: obtaining the data source identifiers of all data sources; determining the corresponding device identifier according to each of the data source identifiers; binding each of the data source identifiers and the corresponding device identifier in the form of a key-value pair to obtain the preset switching relationship.
[0007] According to a data source switching method based on HTTP request provided by the present invention, the HTTP request also includes a data query request; the device identification of the data acquisition device is set in the request header parameter of the HTTP request according to the data query request; the data query request is set in the request body of the HTTP request; the data query request is a request for querying data in a database connected to the data acquisition device.
[0008] According to a data source switching method based on HTTP request provided by the present invention, after obtaining the HTTP request, it also includes: using a section algorithm to process the HTTP request to obtain the device identification of the data acquisition device.
[0009] According to a data source switching method based on HTTP request provided by the present invention, after determining the data source identifier of the data source to be switched according to the HTTP request and the preset switching relationship, it also includes: storing the data source identifier of the data source to be switched in the thread context.
[0010] According to a data source switching method based on HTTP request provided by the present invention, after the data source switching is performed according to the data source identifier of the data source to be switched, it also includes: using a database connection pool to query the data of the data acquisition device from the database according to the data source identifier of the data source to be switched.
[0011] According to the present invention, a data source switching method based on HTTP request also includes: periodically clearing thread context.
[0012] The present invention also provides a data source switching system based on HTTP requests, comprising: a request acquisition module, used to obtain HTTP requests; the HTTP request includes a device identification of a data acquisition device; the HTTP request is a request to query data of the data acquisition device; a data source identification determination module, used to determine the data source identification of a data source to be switched according to the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the correspondence between the device identification and the data source identification; a switching module, used to switch the data source according to the data source identification of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0013] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for switching data sources based on HTTP requests as described above is implemented.
[0014] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the data source switching method based on HTTP request as described in any one of the above is implemented.
[0015] The present invention provides a data source switching method and system based on HTTP request, the method comprising: obtaining an HTTP request; the HTTP request includes a device identification of a data acquisition device; the HTTP request is a request for querying data of the data acquisition device; determining the data source identification of the data source to be switched according to the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the corresponding relationship between the device identification and the data source identification; switching the data source according to the data source identification of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched. The method of the present invention supports the increase and decrease of the number of data acquisition devices, and can dynamically and conveniently manage and switch the device data source to improve the flexibility and efficiency of data processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0017] Figure 1 This is one of the flow charts of the data source switching method based on HTTP request provided by the present invention.
[0018] Figure 2 This is the second flow chart of the data source switching method based on HTTP request provided by the present invention.
[0019] Figure 3 It is a structural schematic diagram of a data source switching system based on HTTP request provided by the present invention.
[0020] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0021] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0022] Please refer to Figure 1 , Figure 1 This is one of the flow charts of the data source switching method based on HTTP request provided by the present invention.
[0023] The present invention provides a data source switching method based on HTTP request, comprising: 101: Obtaining an HTTP request; the HTTP request includes a device identifier of a data acquisition device; the HTTP request is a request for querying data of the data acquisition device; 102: Determine the data source identifier of the data source to be switched according to the HTTP request and the preset switching relationship; the preset switching relationship is used to represent the corresponding relationship between the device identifier and the data source identifier; 103: Switch the data source according to the data source identifier of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0024] In order to solve the technical problems existing in the prior art, the present invention provides a data source switching method based on HTTP requests. The backend of the data processing system first listens to and obtains HTTP requests from the frontend. These requests may be initiated by users through a graphical interface to request to query the data of the data acquisition device in the data acquisition system. Each HTTP request contains a device identifier of a data acquisition device. The data processing system maintains a preset switching relationship table, which maps each device identifier to a unique data source identifier. The backend parses the device identifier in the HTTP request and determines the corresponding data source identifier according to the preset switching relationship table. This step ensures that the backend knows which data source should be used to query the data. Once the data source identifier is determined, the system will switch to the corresponding data source. This may involve modifying the database connection string, updating the data source information in the thread context, or performing other necessary operations to ensure that subsequent data query operations are performed on the correct data source. After the data source switching is completed, the backend performs the data query operation. For example, the backend may execute an SQL query to retrieve data collected by the data acquisition device within a specific time range (such as vehicle status data). The query results can be returned through the HTTP response. This allows users or system components to access and analyze data collected from the data acquisition device.
[0025] The data source switching method based on HTTP request of the present invention enables the data processing system to flexibly and dynamically switch to the corresponding data source according to the device identification of the data acquisition device, thereby realizing effective management and query of data of different devices. This method improves the scalability and flexibility of the system, allowing devices to be easily added or removed without reconfiguring the entire data acquisition system.
[0026] Please refer to Figure 2 , Figure 2The second flowchart of the data source switching method based on HTTP request provided by the present invention.
[0027] As a preferred embodiment, before obtaining the HTTP request, it also includes: obtaining the data source identifiers of all data sources; determining the corresponding device identifier based on each data source identifier; binding each data source identifier and the corresponding device identifier in the form of a key-value pair to obtain a preset switching relationship.
[0028] In this embodiment, the front end of the data processing system provides a user interface. This page allows the user to input or configure the URL (Uniform Resource Locator) information (data source identifier) of the device data source to the back end. The back end generates a Menu identifier (device identifier) based on the URL information and data ID of the data source, and then binds the Menu identifier and the device data source URL in a key-value pair to obtain a preset switching relationship, which is then stored in the MySQL database so that the back end can quickly find the corresponding data source URL based on the Menu identifier.
[0029] Of course, the database may also be a relational database such as PostgreSQL or Oracle, and the present invention does not make any special limitation thereto.
[0030] As a preferred embodiment, the HTTP request also includes a data query request; the device identification of the data acquisition device is set in the request header parameter of the HTTP request according to the data query request; the data query request is set in the request body of the HTTP request; the data query request is a request for querying data in the database connected to the data acquisition device.
[0031] In this embodiment, the device information is obtained according to the page requirements on the data display page, so that the device Menu ID can be obtained. The page requirements can be provided by the front-end developer or designer of the data processing system. When the device data needs to be displayed, the front-end stores the Menu ID in the HTTP request header parameter and the data query requirements in the HTTP request body according to the data query requirements, and passes them to the back-end code. The device Menu ID is a unique identifier associated with the device data source URL. This identifier is used to specify in the HTTP request header which device data source the back-end needs to obtain data from. Therefore, through the device Menu ID, the data processing system can dynamically switch and obtain device data from multiple data sources to meet the user's display needs for different device data.
[0032] The following fields may be included in an HTTP request: Accept (media types that the client can handle. This means that the client can accept JSON, text, and any type of media), Accept-Encoding (compression formats that the client can handle. This means that gzip and deflate compression algorithms are supported), Accept-Language (language preferred by the client. This means that Chinese is preferred, followed by English, etc.), Authorization (token used for authentication. This is a Bearer token, usually used for OAuth2 authentication), Connection (indicates that this request hopes to maintain a connection with the server. This is set to keep-alive, which means that the connection will not be closed after the request and can be reused), Content-Language (language preferred by the client. This means that the client supports gzip and deflate compression algorithms), and the like. ength (the length of the request body. Here it indicates that the size of the request body is 102 bytes), Content-Type (the media type of the request body. Here it indicates that the data sent is in JSON format), Cookie (Cookies stored by the client. Here is a cookie named ingredients-token), Host (the host name and port of the request. Here it indicates that the request is sent to port 8080 of the server with an IP address of 10.0.67.88), Menu (a custom request header field used to identify the device or data source associated with the request. Here it is identified as 2045) and Origin (the URI of the request source. Here it indicates that the request comes from http: / / 10.0.67.88:8080). The present invention is not particularly limited here.
[0033] As a preferred embodiment, after obtaining the HTTP request, the method further includes: using a section algorithm to process the HTTP request to obtain a device identification of the data acquisition device.
[0034] Aspect algorithm is a programming paradigm that allows developers to separate cross-cutting concerns from business logic. Cross-cutting concerns refer to functions that affect multiple modules or aspects, such as logging, transaction management, security, etc. In languages that support AOP, such as Java, this can be achieved by defining "aspects". Aspects can define additional behaviors that need to be executed before and after specific code is executed or when an exception is thrown. For example, transactions can be automatically started and committed before and after data access operations without explicitly writing these codes in each data access method. In this embodiment, in order to improve performance, the front end may send multiple HTTP requests in parallel, and the back end processes all HTTP requests uniformly and obtains the device data source URL according to the Menu identifier.
[0035] In addition, the Java code example for running the backend slice algorithm in this embodiment is as follows, and the present invention is not particularly limited here.
[0036]
[0037] As a preferred embodiment, after determining the data source identifier of the data source to be switched according to the HTTP request and the preset switching relationship, the method further includes: storing the data source identifier of the data source to be switched in the thread context.
[0038] Thread context generally refers to the data and state associated with a specific thread in a multi-threaded environment. In this embodiment, the device data source URL is saved in the thread context to ensure that each thread can access the correct data source URL without having to query it every time the database is accessed. The data source URL is associated with the thread that initiates the request, avoiding confusion of data sources between different requests.
[0039] In addition, the Java code example of running the device data source and saving it to the thread context in this embodiment is as follows, and the present invention is not particularly limited here.
[0040]
[0041] As a preferred embodiment, after switching the data source according to the data source identifier of the data source to be switched, the method further includes: using a database connection pool to query the data of the data acquisition device from the database according to the data source identifier of the data source to be switched.
[0042] Database connection pool is a technology for creating and managing database connections. Database connection is the communication channel between the application and the database, but creating and destroying the connection is a relatively time-consuming operation. Therefore, using a database connection pool can improve the performance and scalability of the application. In this embodiment, in an environment with multiple data sources, different devices may be connected to different databases. The device data source URL provides the information required to connect to a specific database (such as database address, port, database name, authentication information, etc.). The back end obtains a connection from the database connection pool, which points to the database specified by the device data source URL. Use the selected database connection to perform SQL queries or other data retrieval operations to obtain the data of the required data acquisition device. The query results are returned to the application for further processing or sent directly to the front end. After the query is completed, the database connection is returned to the connection pool instead of closing it. In this way, other requests can use this connection. Through the database connection pool in this embodiment, the data processing system can quickly and reliably access multiple data sources while ensuring efficient use of resources.
[0043] As a preferred embodiment, the method further includes: periodically clearing the thread context.
[0044] In order to prevent memory overflow, in this embodiment, the thread context is periodically cleared, for example, the device data source URL in the thread context is cleared after the query is completed. By periodically clearing the thread context, the data processing system can avoid memory leaks caused by the thread holding database connections or other resources for a long time. Periodically clearing the thread context can also prevent data inconsistency problems caused by residual data source identifiers in the thread context, ensuring that each request uses the correct data source.
[0045] The following is a description of a data source switching system based on HTTP request provided by the present invention. The data source switching system based on HTTP request described below and the data source switching method based on HTTP request described above can be referred to each other.
[0046] Please refer to Figure 3 , Figure 3 A schematic diagram of the structure of a data source switching system based on HTTP request provided by the present invention.
[0047] The present invention also provides a data source switching system based on HTTP request, comprising: a request acquisition module 1, used to obtain HTTP request; the HTTP request includes the device identification of the data acquisition device; the HTTP request is a request for querying the data of the data acquisition device; a data source identification determination module 2, used to determine the data source identification of the data source to be switched according to the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the correspondence between the device identification and the data source identification; a switching module 3, used to switch the data source according to the data source identification of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0048] Figure 4 An example of a structural diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute a data source switching method based on an HTTP request, the method comprising: obtaining an HTTP request; the HTTP request includes a device identification of a data acquisition device; the HTTP request is a request for querying data of the data acquisition device; determining a data source identification of a data source to be switched according to the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the corresponding relationship between the device identification and the data source identification; switching the data source according to the data source identification of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0049] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0050] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the data source switching method based on HTTP request provided by the above methods, and the method includes: obtaining an HTTP request; the HTTP request includes a device identifier of a data acquisition device; the HTTP request is a request to query data of the data acquisition device; according to the HTTP request and a preset switching relationship, determining the data source identifier of the data source to be switched; the preset switching relationship is used to characterize the correspondence between the device identifier and the data source identifier; switching the data source according to the data source identifier of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
[0051] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the data source switching method based on HTTP requests provided by the above-mentioned methods, the method comprising: obtaining an HTTP request; the HTTP request comprises a device identifier of a data acquisition device; the HTTP request is a request for querying data of the data acquisition device; determining a data source identifier of a data source to be switched based on the HTTP request and a preset switching relationship; the preset switching relationship is used to characterize the correspondence between the device identifier and the data source identifier; switching the data source based on the data source identifier of the data source to be switched, so as to query data of the data acquisition device through the data source to be switched.
[0052] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0053] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0054] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data source switching method based on HTTP request, characterized in that: include: Get HTTP request; The HTTP request includes a device identification of the data acquisition device; The HTTP request is a request for querying the data of the data acquisition device; Determine the data source identifier of the data source to be switched according to the HTTP request and the preset switching relationship; The preset switching relationship is used to characterize the corresponding relationship between the device identifier and the data source identifier; The data source is switched according to the data source identifier of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
2. The method for switching data source based on HTTP request according to claim 1, characterized in that: Before obtaining the HTTP request, the method further includes: Get the data source identifiers of all data sources; Determine a corresponding device identifier according to each of the data source identifiers; Each of the data source identifiers and the corresponding device identifiers are bound in a key-value pair manner to obtain the preset switching relationship.
3. The method for switching data source based on HTTP request according to claim 1, characterized in that: The HTTP request also includes a data query request; the device identification of the data acquisition device is set in the request header parameter of the HTTP request according to the data query request; The data query request is set in the request body of the HTTP request; the data query request is a request for querying data in the database connected to the data acquisition device.
4. The method for switching data source based on HTTP request according to claim 1, characterized in that: After obtaining the HTTP request, the following steps are also included: The HTTP request is processed using a section algorithm to obtain a device identification of the data acquisition device.
5. The method for switching data source based on HTTP request according to claim 1, characterized in that: After determining the data source identifier of the data source to be switched according to the HTTP request and the preset switching relationship, the method further includes: The data source identifier of the data source to be switched is stored in the thread context.
6. The method for switching data source based on HTTP request according to claim 1, characterized in that: After the data source is switched according to the data source identifier of the data source to be switched, the method further includes: Using a database connection pool, the data of the data acquisition device is queried from a database according to the data source identifier of the data source to be switched.
7. The method for switching data source based on HTTP request according to any one of claims 1 to 6, characterized in that: Also includes: Clear the thread context periodically.
8. A data source switching system based on HTTP request, characterized in that: include: Request acquisition module, used to obtain HTTP requests; The HTTP request includes a device identification of the data acquisition device; The HTTP request is a request for querying the data of the data acquisition device; A data source identification determination module, used to determine the data source identification of the data source to be switched according to the HTTP request and the preset switching relationship; The preset switching relationship is used to characterize the corresponding relationship between the device identifier and the data source identifier; The switching module is used to switch the data source according to the data source identifier of the data source to be switched, so as to query the data of the data acquisition device through the data source to be switched.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method for switching data sources based on HTTP requests as described in any one of claims 1 to 7 is implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for switching data sources based on HTTP requests as claimed in any one of claims 1 to 7 is implemented.