A kind of dispatching method of multi-interface data
Technical field
The present invention relates to internet areas, and in particular to a kind of dispatching method of multi-interface data.
Background technique
During gradually developing, each page is more and more with business, and the extra data needed can be more next for website
More, these data often have respectively primary information source, such as database, memory, file, in order to make exploitation more efficient,
Often respectively encapsulate these primary information sources with the mode of encapsulation in most of companies, it is each it is packaged after message come
Also cry interface in source, and these interfaces are often on a server, can be to so each page is when obtaining data
Interface requests on multiple servers obtain desired data.One page will show that certain business will request various businesses respectively
The interface of data is provided, such several ways are had:
(1) completely from background request data
User opens the page, initiates request to backstage, sends acquisition request from background program to all data-interfaces, so
After transmit to foreground, displaying is responsible for by foreground again again later, if there is interface that problem occurs under this mode, it may occur that
Domino effect, leads to entirely backstage paralysis, and this problem is very normal in some PHP programs for temporarily multithreading not being supported to develop
See, is tended not in this way when interface is more;
(2) AJAX request interface is sent by foreground completely and obtains data
User open the page, it is obstructed later platform, completely by foreground send AJAX request interface obtain data, eventually by return
Letter of transfer number shows service logic, completely dispenses with backstage, but this mode due to safety problem and is of little use.In addition, all by
Foreground sends request, if business demand number of requests is more, or when the case where interdepending, machine matches the browser set low
Can because number of requests is excessive and the phenomenon that there is Caton for a long time.
(3) front and back hybrid mode:
Show that core business, front end transmission AJAX show non-using call back function by background request core business interface priority
Core business, current this mode are most commonly seen.But the bigger time-consuming of interface general data amount of this mode core business
Very long, concurrent utilization is not high;And non-core services shift foreground onto and establish AJAX request, once non-core services quantity is excessive,
Foreground can there is a phenomenon where Catons when handling these requests;In addition, during business development, one or more groups
Being responsible for one business of completion causes non-core services more and more, and the AJAX that foreground is called is also more and more, and the page just loads
It is complete to go to handle many AJAX voice and packet data concurrent services, it is more likely that the phenomenon that will appear Caton.
In conclusion need the method for an Interface Controller to manage the calling of various interfaces, it can be in stable output
Non-core services are efficiently added while core business for the page.
Summary of the invention
In order to solve the above-mentioned technical problems, the present invention provides a kind of dispatching method of multi-interface data, for working as webpage
The Caton phenomenon occurred when multiple interfaces is requested to establish an interface scheduling mechanism, it is as much as possible concurrent using background process,
To achieve the effect that efficiently to add non-core services while stable output core business for the page.
The present invention is realized with following technical solution,
A kind of dispatching method of multi-interface data including the core interface for processing core business and handles non-core industry
The non-core interface of business is scheduled, comprising the following steps:
S1. it is interface class, and sorted interface is included in cell therefor:
The core interface that will be directly dispatched is included in core interface container,
The core interface that will not be able to directly be dispatched, which has been included in, relies on core interface container,
The non-core interface that will be directly dispatched is included in non-core interface container,
The non-core interface that will not be able to directly be dispatched, which has been included in, relies on non-core interface container;
S2. the page parameter in HTTP request is obtained;
S3. had according to page parameter update and rely on core interface container, core interface container, non-core interface container
Non-core interface container is relied on having;
S4. the interface not being scheduled in the core interface container and non-core interface container is concurrently adjusted
With;
S5. according to the call result of S4, forward end sends data;
S6. front end shows the call result of interface in S5, and judges whether there is non-core interface to be processed, if depositing
Then the non-core interface is being called;
S7. forward end shows the call result of interface in S6, and judges whether there is non-core interface to be processed, if depositing
Continuing to be called the non-core interface, and scheduling result is shown in front end.
Preferably, it updates to have in S3 and relies on core interface container, core interface container, non-core interface container and have dependence
Non-core interface container includes:
Under the premise of the known page parameter, traversal, which has to rely on core interface container and have, relies on non-core interface appearance
Device,
If there is the core interface for relying on and existing in core interface container and capable of directly being dispatched, will directly be dispatched
Core interface move to the core interface container,
If there is the non-core interface for relying on and existing in non-core interface container and capable of directly being dispatched, will described in can be by
The non-core interface directly dispatched moves to the non-core interface container.
Preferably, S4 includes:
S41. it opens thread parallel and dispatches not being scheduled in the core interface container and non-core interface container
Interface;
If S42. obtaining response, success response is judged whether it is, if so, S43 is carried out, if it is not, then directly stopping institute
Have for interface scheduling thread, directly return http response: 502 server problem of message status or 307 jump to it is corresponding
The page;
S43. the interface flag that will acquire the response is scheduled interface, inquires whether the response results are to have dependence
Core interface container and/or there is undetermined parameter required for the interface relied in non-core interface container, if it is, carrying out
Otherwise S44 carries out S46;
S44. the undetermined parameter is substituted with the response results, and under the premise of known to the undetermined parameter, judgement
Whether need the interface of the undetermined parameter can directly be scheduled, if can, S45 is carried out, otherwise, carries out S46;
S45. if desired the interface of the undetermined parameter be core interface, then by the core interface from have rely on core connect
Mouthful container moves to core interface container, and if desired the interface of the undetermined parameter is non-core interface, then non-core connects described
It mouthful relies on non-core interface container and moves to non-core interface container from having;
S46. the thread for obtaining response continues to dispatch not adjusted in the core interface container and non-core interface container
The interface spent, until whole core interfaces are responded.
Preferably, S4 further includes obtaining to open in the core interface quantity N and thread maximum number of concurrent tMax, S4 being not called upon
The quantity of the thread opened is not more than tMax;If N is not more than tMax, the non-core interface quantity and core interface number of Parallel Scheduling
Amount ratio is less than maximum and concurrently compares, otherwise, first Parallel Scheduling core interface, and and N value is obtained in real time, when N is less than tMax, scheduling
The non-core interface quantity and core interface quantity ratio for meeting Parallel Scheduling in the process concurrently compare no more than maximum.
Preferably, S4 further includes holding before opening parallel thread to the core interface container and the non-core interface
Interface in device is ranked up according to importance, successively to the core interface container and the non-core interface in scheduling process
The interface not being scheduled in container is called.
Preferably, S4 further includes before opening parallel thread, to it is described have rely on core interface container and described have dependence
Interface in non-core interface container is ranked up according to importance.
Preferably, S5 includes:
The non-core interface for not obtaining response and/or response failure is judged whether there is, if so, then not obtaining sound for described
The element for answering and/or responding the non-core interface of failure is packaged together with the data obtained in S4;Otherwise, it is only packaged in S4 and obtains
Data;
Result will be packaged and be sent to front end.
Preferably, the element includes interface id and the scheduling for not obtaining the non-core interface of response and/or response failure
The parameter of the non-core interface needs for not obtaining response and/or response failure.
Preferably, described to have dependence core interface container, core interface container, non-core interface container and have dependence non-core
Interface in heart interface container includes the status indicator for characterizing the Interface status.
Preferably, interface calling is carried out by HTTP request in S4, ajax carries out interface calling in S5 and S6.
The beneficial effects of the present invention are:
The present invention provides a kind of dispatching methods of multi-interface data to utilize this by establishing an interface scheduling mechanism
Interface scheduling mechanism generates interface scheduling strategy, as much as possible concurrent using background process, so that the concurrent quantity on backstage reaches
To reasonable maximum value, foreground quantity of request interface under the premise of not influencing core business and showing reaches minimum, so that after
Platform concurrently can take data using the advantage of cpu multicore to greatest extent and foreground is only concerned rendering and user's interaction rather than passes
Transmission of data, advantage of the invention are main further include:
Front page layout can with minimum frequency and backstage exchange data, when displaying the rearrangement of the page can be effectively reduced
It redraws;Concurrent processing a large amount of interactive interface in backstage ensure that core business is available, and non-core heart function maximal efficiency is forward
Platform is shown.
Detailed description of the invention
Fig. 1 is the dispatching party flow chart of one embodiment multi-interface data;
Specific embodiment
To make the object, technical solutions and advantages of the present invention clearer, the present invention is made into one below in conjunction with attached drawing
Step ground detailed description.
Embodiment 1:
In one embodiment, as shown in Figure 1, a kind of dispatching method of multi-interface data, including for processing core
The core interface of business and the non-core interface of processing non-core services are scheduled, and the present embodiment is with video playback website
Example, core interface are to realize that webpage plays the relevant interface of video capability, and non-core interface is to realize connecing for the other functions of webpage
Mouthful, for example realize comment function, the interface of associated video list display function, subscription function and download function is non-core connects
Mouthful.
The dispatching method of multi-interface data the following steps are included:
S1. it is interface class, and sorted interface is included in corresponding queue:
The core interface that will be directly dispatched is included in core interface queue,
The core interface that will not be able to directly be dispatched, which has been included in, relies on core interface queue,
The non-core interface that will be directly dispatched is included in non-core interface queue,
The non-core interface that will not be able to directly be dispatched, which has been included in, relies on non-core interface queue;
S2. the page parameter in HTTP request is obtained;
S3. the parameter page parameter relied on as interface, update, which has, relies on core interface queue, core interface
Queue, non-core interface queue and has and rely on non-core interface queue;
S4. the interface not being scheduled in the core interface queue and non-core interface queue is concurrently adjusted
With;
S5. according to the call result of S4, forward end sends data;
S6. front end shows the call result of interface in S5, and judges whether there is non-core interface to be processed, if depositing
Then the non-core interface is being called;
S7. forward end shows the call result of interface in S6, and judges whether there is non-core interface to be processed, if depositing
Continuing to be called the non-core interface, and scheduling result is shown in front end.
Specifically, it updates to have and relies on core interface queue, core interface queue, non-core interface queue and have dependence non-core
Heart interface queue method are as follows:
Under the premise of the known page parameter, traversal, which has to rely on core interface queue and have, relies on non-core interface team
Column,
If there is the core interface for relying on and existing in core interface queue and capable of directly being dispatched, will directly be dispatched
Core interface move to the core interface queue,
If there is the non-core interface for relying on and existing in non-core interface queue and capable of directly being dispatched, will described in can be by
The non-core interface directly dispatched moves to the non-core interface queue.
Firstly, first introducing several definition:
CoreList be core interface queue, CoreDependList be have rely on core interface queue,
AppendList is non-core interface queue, and AppendDependList is to have to rely on non-core interface queue.
Interface includes following element:
Id: interface is uniquely requested number;
State: Interface status, -1 indicates that the non-parameter for relying on parameter is taken, and 0 indicates not request, and 1 indicates that request carries out
In, 2 indicate that request is completed, and 3 indicate request failure;
Url: request address;
Method: requesting method;
Param: when request interface will band parameter;
Core interface is fully completed mark: CoreListState;
CoreListState is that 0 expression core interface has not sent request and obtained return value, and 1 indicates to obtain
All return values, -1 indicates that there are interfaces not to respond to.
Maximum thread: tMax
Concurrent maximum quantity is handled, for describing physical computer theoretically reasonable thread concurrency, the present embodiment
The operation method of a Thread Count is provided, Memery is the proceeding internal memory of program distribution (note that this quantity is single backstage journey
Sequence triggering, be multiplied by worker quantity if there is multiple worker), it is 128M in the present embodiment, can be looked into ulimit-s
See the thread stack size of default, this value is 8M under normal circumstances;Then tMax:128/8=16.
The maximum of core business interface and non-core services interface concurrently compares: CoreVSAppend
Core interface is often just several when concurrent there are core business, in the present embodiment all not all the remaining threads
As non-core thread, thread is more also can be slow to entire process poll, if kernel thread can cut off remaining line when completing
Journey, if the request thread of non-core services interface excessively will cause the pressure of website without completion again;CoreVSAppend this
The setting of a value is by the average response time CoreTimeAve of core interface and the average response time of non-core interface
What AppendTimeAve was compared, this needs to see that the log of interface determines the average response time of each interface;Assuming that
Concurrently than being 2, then the request for allowing to have 2 core concurrent requests and 4 non-core services interfaces.
Control centre's label: messageDispatch
One page may have several control centres, can be generated in algorithm second and third time to control centre's transmission
Which control centre is data come to distinguish, and request can all take scheduling label, scheduling label [scheduler every time
Name-which time request] it indicates;
User sends request:
After user opens the page on browser, HTTP request is sent to interface control centre, has page in message entity
Face parameter and parameter messageDispatch=playerPage-1, this value are only to be shown to be request broadcast page for the first time
Control centre.
The page parameter (if there is encryption, first to decrypt and use afterwards) in HTTP message entity has been put into S2 and S3
The argument section of the interface of dependence, and check it is all have rely on interface queue (CoreDependList and
AppendDependList), and the interface for having taken all necessary parameters is removed to the interface queue for having dependence
(CoreDependList and AppendDependList) and be moved into do not rely on interface requests queue (CoreList,
AppendList), and change interface requests state and be changed to 0 by -1.
Specifically, S4 includes:
S41. the interface not being scheduled in thread parallel scheduling CoreList and AppendDependList is opened;
If S42. obtaining response, success response is judged whether it is, if so, by the mode bit for the interface for obtaining response
State is changed to 2 by 1, carries out S43, if it is not, the mode bit state for the interface for obtaining response is changed to 3 by 1, directly stops all
For the thread of interface scheduling, directly return http response: 502 server problem of message status 307 jumps to corresponding page
Face;
S43. inquire whether the response results are interfaces in CoreDependList and/or AppendDependList
Otherwise required undetermined parameter, carries out S46 if it is, carrying out S44;
S44. the undetermined parameter is substituted with the response results, and under the premise of known to the undetermined parameter, judgement
Whether need the interface of the undetermined parameter can directly be scheduled, if can, the interface of the undetermined parameter will be needed
State has -1 to become 0, carries out S45, otherwise, carries out S46;
S45. if desired the interface of the undetermined parameter is core interface, then by the core interface from having
CoreDependList moves to CoreList, and if desired the interface of the undetermined parameter is non-core interface, then by the non-core
Heart interface moves to AppendList from AppendDependList;
S46. the thread for obtaining response continues to dispatch the interface not being scheduled in CoreList and AppendList, directly
Whole interfaces into CoreList are responded.
Specifically, S4 further includes obtaining to open in the core interface quantity N and thread maximum number of concurrent tMax, S4 being not called upon
The quantity of the thread opened is not more than tMax;If N is not more than tMax, the non-core interface quantity and core interface number of Parallel Scheduling
Amount ratio is less than maximum and concurrently compares, otherwise, first Parallel Scheduling core interface, and and N value is obtained in real time, when N is less than tMax, scheduling
The non-core interface quantity and core interface quantity ratio for meeting Parallel Scheduling in the process concurrently compare no more than maximum.
Specifically, S4 further include open parallel thread before, to CoreList, CoreDependList,
Interface in AppendDependList and AppendList is ranked up according to importance, successively right in scheduling process
The interface not being scheduled in CoreList and AppendList is called.
The data of the concurrent and part non-core services of all core business interfaces have been handled in S5 by S4, if
Have the interface element for being responded and being responded failure in AppendList or not, not responded in entire AppendList and
The id and parameter for responding the element of failure break into a data packet and encrypt, and are packaged together with the S4 data obtained, if
Element in AppendList, which has all responded, also only sends out core business data and requested non-core services data, arranges below
It is sent to the data content of front end out:
A indicates core business data and requested non-core services data;
B indicates the label from which control centre;
C indicates what the data that unsolicited non-core services interface id and unsolicited non-core services interface need were constituted
Data encryption packet.
The request not responded is handled in S6 again:
After data are taken in front end, shows that S5 is sent to a data on foreground to the page, then determine whether there are data in c, such as
Fruit has reads b from for which control centre, and the data of c are sent back interface control centre with AJAX again;
Corresponding control centre obtains unsolicited non-core services interface id and unsolicited non-core services interface
Data deciphering is initiated concurrent process to initiate to request to remaining interface, the state all requested by the data encryption packet needed
When becoming non-zero, indicates the data for having taken all non-core services, data are packaged, front end is sent to, if there is connecing
Mouth sends (state state is 3) non-core interface of request failure, and each request of transmission request failure is packaged respectively, is added
It is close, the data content for being sent to front end is listed below:
A indicates requested non-core services data;
B indicates the label from which control centre;
C indicates the non-core services interface ciphering packet of failure;
The request not responded is handled in S7 again:
The non-core services data in a are taken, front end is presented in, if there are data in data above block c, respectively to tune
AJAX request is initiated at degree center, obtains response data, then be shown to front end.
Embodiment 2:
Nginx server is used in the present embodiment: having a master, M worker, master in nginx server
Control worker.
Using nginx server carry out multi-interface data dispatching method the following steps are included:
S1. it is interface class, and sorted interface is included in corresponding queue:
The core interface that will be directly dispatched is included in core interface queue,
The core interface that will not be able to directly be dispatched, which has been included in, relies on core interface queue,
The non-core interface that will be directly dispatched is included in non-core interface queue,
The non-core interface that will not be able to directly be dispatched, which has been included in, relies on non-core interface queue;
S2. the page parameter in HTTP request is obtained;
S3. the parameter page parameter relied on as interface, update, which has, relies on core interface queue, core interface
Queue, non-core interface queue and has and rely on non-core interface queue;
S4. the interface not being scheduled in the core interface queue and non-core interface queue is concurrently adjusted
With the quantity of the thread of concurrent invocation is proceeding internal memory, the quantity at thread station and the number of worker of program distribution by Memery
M is measured to determine;
S5. according to the call result of S4, forward end sends data;
S6. front end shows the call result of interface in S5, and judges whether there is non-core interface to be processed, if depositing
Then the non-core interface is being called;
S7. forward end shows the call result of interface in S6, and judges whether there is non-core interface to be processed, if depositing
Continuing to be called the non-core interface, and scheduling result is shown in front end.
Specifically, it updates to have and relies on core interface queue, core interface queue, non-core interface queue and have dependence non-core
Heart interface queue method are as follows:
It updates to have in S3 and relies on core interface container, core interface container, non-core interface container and have dependence non-core
Interface container includes:
Under the premise of the known page parameter, traversal, which has to rely on core interface container and have, relies on non-core interface appearance
Device,
If there is the core interface for relying on and existing in core interface container and capable of directly being dispatched, will directly be dispatched
Core interface move to the core interface container,
If there is the non-core interface for relying on and existing in non-core interface container and capable of directly being dispatched, will described in can be by
The non-core interface directly dispatched moves to the non-core interface container.
Specifically, S4 includes:
S41. it opens thread parallel and dispatches not being scheduled in the core interface container and non-core interface container
Interface;
If S42. obtaining response, success response is judged whether it is, if so, S43 is carried out, if it is not, then directly stopping institute
Have for interface scheduling thread, directly return http response: 502 server problem of message status or 307 jump to it is corresponding
The page;
S43. the interface flag that will acquire the response is scheduled interface, inquires whether the response results are to have dependence
Core interface container and/or there is undetermined parameter required for the interface relied in non-core interface container, if it is, carrying out
Otherwise S44 carries out S46;
S44. the undetermined parameter is substituted with the response results, and under the premise of known to the undetermined parameter, judgement
Whether need the interface of the undetermined parameter can directly be scheduled, if can, S45 is carried out, otherwise, carries out S46;
S45. if desired the interface of the undetermined parameter be core interface, then by the core interface from have rely on core connect
Mouthful container moves to core interface container, and if desired the interface of the undetermined parameter is non-core interface, then non-core connects described
It mouthful relies on non-core interface container and moves to non-core interface container from having;
S46. the thread for obtaining response continues to dispatch not adjusted in the core interface container and non-core interface container
The interface spent, until whole core interfaces are responded.
In addition, S4 further includes obtaining to open in the core interface quantity N and thread maximum number of concurrent tMax, S4 being not called upon
Thread quantity be not more than tMax;If N is not more than tMax, the non-core interface quantity and core interface quantity of Parallel Scheduling
Ratio is less than maximum and concurrently compares, otherwise, first Parallel Scheduling core interface, and and N value is obtained in real time, when N is less than tMax, dispatched
The non-core interface quantity and core interface quantity ratio for meeting Parallel Scheduling in journey concurrently compare no more than maximum.
Specifically, S5 includes:
The non-core interface for not obtaining response and/or response failure is judged whether there is, if so, then not obtaining sound for described
The element for answering and/or responding the non-core interface of failure is packaged together with the data obtained in S4;Otherwise, it is only packaged in S4 and obtains
Data;
Result will be packaged and be sent to front end.
Specifically, the element includes interface id and the scheduling for not obtaining the non-core interface of response and/or response failure
The parameter of the non-core interface needs for not obtaining response and/or response failure.
Specifically, described to have dependence core interface container, core interface container, non-core interface container and have dependence non-core
Interface in heart interface container includes the status indicator for characterizing the Interface status.
In the present embodiment, interface calling is carried out by HTTP request in S4, passes through ajax progress interface calling in S5 and S6.
In addition, in the present embodiment:
(1) all requests write in text document, and total interface calls clear;
(2) there are when dependence, be first packaged into one to connect for parameter when the interface of two or more non-core services calls
Mouth is called again;
(3) average response time of the interface of core business is not less than the non-core services interface response time;
(4) response time of total interface in an order of magnitude, did not had the presence of slow interface, and excessively slow interface makes pressure
Use expired time;
(5) response data of interface is no more than 3M data.
Embodiment 3:
Third embodiment, the difference with one embodiment are:
The request time of total interface is recorded, if interface requests will unsuccessfully use a very big response time
It indicates interface malloc failure malloc, all interface response times is recorded and sorted, this sequence is a sample;Statistics
After the interface ordered samples for accessing website to n user, the response time of each interface is averaged, administrator is reported to.
If website visiting amount is very big, the mode record interface state of random sampling can be used, the sample of random statistical A% is as system
Metering.
The present embodiment can find the problem of interface in time, and optimize the interface of low-response;If interface responded slowly,
The state of this interface is directly set to 3, and foreground is allowed to be called using ajax.
Embodiment 4:
4th embodiment is with one embodiment difference:
S1 is optimized:
When creating queue, request of the interface on same server is put together as far as possible, the reason is that, when building
When vertical http request, keep-alive attribute can be added in heading, be can establish a TCP data when concurrent and passed
Defeated channel sends multiple HTTP requests and is used to save the time.
Embodiment 5:
5th embodiment is with one embodiment difference:
Under the premise of not knowing the configuration of nginx, the subprocess for not being the nginx of reverse proxy is natively accounted for very greatly
Resource, interrupted if robbing resource together in the new process of pull-up and drawing, O&M can't allow open excessive process;So it is merely able to
Use thread;
1. the advantages of multithreading:
Object and variable be it is shared, can directly be operated;
Filec descriptor is that shared, different thread can directly operate the same resource;
2. the shortcomings that multithreading:
It needs to lock when operating nonlocal variable, programming difficulty is high;
EMS memory error occurs for one thread, and entire process can all terminate.
However, if website is often unstable in some period interface, it may occur that certain thread interrupt in scheduling process
Therefore S4 is optimized so as to cause entire scheduling process interrupt, optimal way are as follows:
(1) it requires Nginx using reversed proxy server, forwards a request to multiple network servers;It is taken in multiple networks
Deployment interface scheduler on business device;
(2) it uses multi-course concurrency instead from the background and does not use multi-thread concurrent request interface, scheduler at this time is one
A parent process, concurrent request process are its subprocess, and the stability of entire parent process can be improved.
This optimal way also has a shortcoming, and the memory of process consumption is excessively high, do not consider resource can with process come
It substitutes thread and realizes that interface is concurrent.
Embodiment 6:
6th embodiment, the difference with one embodiment are:
The present embodiment concurrently takes data using node.js realization.
Embodiment 7:
7th embodiment, the difference with one embodiment are:
The present embodiment uses nginx, there is a master in nginx, and M worker, master control worker.
Foreground extends the concurrency of ajax using the H5 new features of worker.
The above disclosure is only the preferred embodiments of the present invention, cannot limit the right model of the present invention with this certainly
It encloses, therefore equivalent changes made in accordance with the claims of the present invention, is still within the scope of the present invention.