In some large-scale software projects, if the user experience requirements are high, it is necessary to make a comparison of service invocation efficiency.
There are two things about service invocation: Local invocation (in-project) and remote invocation (inter-project).
Local invocation, in the same circumstances as other factors, because there is no need for external communication, its efficiency is certainly the highest. But the problem is that as the business grows, we have to call it remotely when a server is not satisfied.
Remote communication may have a lot, now compare, LOCAL,RABBITMQ RPC, webapi efficiency.
Environment is: Local computer, to meet the requirements of the consistent hardware environment;
Start Rabbitmq,webapi separately, using the same algorithm to ensure that all business logic is consistent. In order to maximize the efficiency of WEBAPI, no third-party framework is used.
such as: Only the Servlet interface is implemented
@Override
public void Service (ServletRequest servletrequest, Servletresponse servletresponse) throws Servletexception, IOException {
String val = servletrequest.getparameter ("num");
int fibval = 0;
try {
Fibval = fib (Integer.valueof (Val));
} catch (Exception ex) {
}
Servletresponse.setcontenttype ("text/html");
Servletresponse.setcharacterencoding ("Utf-8");
PrintWriter writer = Servletresponse.getwriter ();
Writer.println (Fibval);
Writer.flush ();
Writer.close ();
}
. The
request side is as follows:
public static void Main (string[] args) throws Exception {
String message = Readstrfromconsole ();
Long start = System.currenttimemillis ();
int localval = FIB (integer.valueof (message));
System.out.println ("Local:first:" + (System.currenttimemillis ()-start));
SYSTEM.OUT.PRINTLN (message + "s Fibo is" + localval);
start = System.currenttimemillis ();
Rpcclient fiborpc = new Rpcclient ();
String response = fiborpc.call (message);
Fiborpc.close ();
System.out.println ("Rpc:second:" + (System.currenttimemillis ()-start));
SYSTEM.OUT.PRINTLN (message + "s Fibo is" + response);
start = System.currenttimemillis ();
String http_response = Remoterequest.get ("http://localhost:8080/TestWeb/test?num=" + message);
System.out.println ("Http:second:" + (System.currenttimemillis ()-start));
System.out.println (message + "s Fibo is" + http_response);
} The
evaluates the value of FIB (41), and the test results are as follows:
Connected to the target VM, address: ' 127.0.0.1:55175 ', Transport: ' Socket '
41
local:first:1017
Fibo ' s is 165580141
rpc:second:1207
Fibo ' s is 165580141
Disconnected from the target VM, address: ' 127.0.0.1:55175 ', Transport: ' Socket '
http:third:2106
Fibo ' s is 165580141
Second run:
41
local:first:1017
Fibo ' s is 165580141
rpc:second:1227
Fibo ' s is 165580141
http:third:1963
Fibo ' s is 165580141
Third time:
41
local:first:1022
Fibo ' s is 165580141
rpc:second:1193
Fibo ' s is 165580141
Disconnected from the target VM, address: ' 127.0.0.1:55203 ', Transport: ' Socket '
http:third:1969
Fibo ' s is 165580141
。。。
After more calls, you will find:
Local, around 1000ms;
RABBITMQ RPC, around 1200ms;
WebAPI, around 2000ms.
As a result, RABBITMQ RPC is much more efficient in remote calls than WEBAPI.
However, there are a few limitations, RABBITMQ RPC can only be used for the same network segment, but for cross-network segments can still be resolved.
Service invocation Efficiency comparison