Requirements: Based on the microservice architecture used by ASP.NET Core, some services are infrastructure (basic services) that need to communicate frequently with a certain service, and high performance and low latency are particularly important.
Using the MemoryPack protocol to serialize data has two advantages over using JSON: fast serialization and small transfer of content, which improves performance.
Review:
The interface was called 200,000 times using MemoryPack and JSON, and the results were as follows:
MemoryPack takes 29,895 milliseconds JSON takes 34,283 milliseconds
Since I amLocal test, the gap is not very noticeableWhen cross-host calls, fields increase, and data volume increases, I believe the gap will be more obvious.
ASP.NET Core configuration supports the MemoryPack protocol
Since the object is serialized using the MemoryPack protocol and then sent over the HTTP protocol, ASP.NET Core needs to support the MemoryPack protocol, and a new MemoryPackInputFormatter class is created, the code is as follows:
Then MvcOptions adds support for the MemoryPack protocol, with the following code:
Define a transport object with the following UserProfile code:
The controller method adds an interface with the following code:
The console uses Refit to send HTTP requests
A console application uses MemoryPack to serialize objects and send HTTP requests to interfaces, with the following code:
The test is as follows:
When using MemoryPackSerializer to serialize an object, the following code will give an error:
System.Object is not registered in this provider It should be a MemoryPack bug, modified as follows:
(End)
|