Introduced
Recently, there has been a strong interest in the rest-style application architecture, suggesting that the elegant design of the web is beginning to be noticed. Now, we understand the scalability and elasticity inherent in the 3W architecture (Architecture of the World Wide Web) and explore ways to use its paradigm. In this article, we'll explore a little-known tool that can be exploited by web developers, an unobtrusive "ETag Response header," and how to integrate it into a dynamic Web application based on spring and hibernate. To improve application performance and scalability.
The Spring Framework application we will be using is based on the "Pet Clinic (Petclinic)". The download file contains instructions on how to add the necessary configuration and source code, and you can try it yourself.
What is an "ETag"?
The HTTP protocol specification defines the etag as the entity value of the requested variable (see http://www.w3.org/protocols/rfc2616/rfc2616-sec14.html--, section 14.19). Another argument is that the ETag is a token that can be associated with a Web resource. A typical web resource can be a Web page, but it may also be a JSON or XML document. The server is solely responsible for judging what the token is and what it means and transmitting it to the client in the HTTP response header.
How does the ETag help improve performance?
Smart server developers will use the "If-none-match" header of the etags and get requests to take advantage of the caching of clients such as browsers. Because the server first generates an etag, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate its (client) cache by passing the token back to the server.
The process is as follows:
- The client requests a page (a).
- The server returns page A and adds an etag to a.
- The client presents the page and caches the page along with the ETag.
- The customer requests page A again and passes the etag that the server returned on the last request to the server.
- The server examines the ETag and determines that the page has not been modified since the last client request, directly returning a response of 304 (unmodified--not Modified) and an empty response body.
The remainder of this article shows two ways to leverage the etag in a spring framework-based Web application that uses spring MVC. First we will use the servlet 2.3 Filter, which uses the MD5 checksum (checksum) of the presentation view (rendered view) to implement the method of generating the ETag (a "shallow" etag implementation). The second method uses a more complex method to track the model used in view to determine the ETag validity (an "in-depth" etag implementation). Although we are using spring MVC, the technology can be applied to any MVC-style web framework.
Before we go on, we emphasize that the technology that is presented here is to improve the performance of dynamically generated pages. Existing optimization techniques should also be considered as part of the overall optimization and application performance tuning analysis. (see below).
top-down web cache
This article mainly involves the use of HTTP caching techniques for dynamically generated pages. When considering improving the performance of your Web application, you should take a holistic, top-down approach. For this purpose, it is important to understand the layers that the HTTP request passes through, and the appropriate technology to apply depends on the hotspot you are interested in. For example:
- Apache is used as the front end of the servlet container to handle static files such as slices and JavaScript scripts, and an etag response header can also be created using the FILEETAG directive.
- Use optimization techniques for JavaScript files, such as merging multiple files into one file and compressing spaces.
- Use the gzip and cache control headers (Cache-control headers).
- To determine where your Spring framework app hurts, consider using Jamonperformancemonitorinterceptor.
- Make sure you take full advantage of the ORM tool's caching mechanism, so objects do not need to be regenerated from the database frequently. Taking the time to determine how to make the query cache work for you is worthwhile.
- Make sure you minimize the amount of data that gets in the database, especially the large list. If each page requests only a small subset of the large list, then the data for the large list should be obtained once from one of the pages.
- Minimizes the amount of data that is put into the HTTP session. This frees up memory and helps when the cluster is being applied.
- Use the database profiling tool to see what indexes were used at the time of the query, and the entire table was not locked when it was updated.
Of course, the maxim of Application performance optimization is: Two measurements, one cut (measure twice, cut once). Oh, wait, this is for carpentry! Yes, but it also works well here!
ETAG Filter Content Body
The first way to consider this is to create a servlet Filter that will generate its ETag tokens based on the content of the page ("View" in MVC). At first glance, any performance gains from using this approach seem counterintuitive. We still have to produce the page, and we have added the calculation time to generate the token. However, the idea here is to reduce bandwidth usage. In large response time scenarios, such as your host and client are distributed on both sides of the planet, this is largely beneficial. I've seen the Tokyo office use an application hosted on a New York server with a response time of up to Ms. As the number of concurrent users grows, this becomes a huge bottleneck.
Code
The technique we use to generate tokens is based on the calculation of MD5 hashes from the page content. This is done by creating a wrapper on top of the response. The wrapper uses a byte array to hold the resulting content, and we use the array's MD5 hash value to calculate the token after the filter chain is processed.
The implementation of the Dofilter method is as follows.
public void DoFilter (ServletRequest req, servletresponse Res, Filterchain chain) throws IOException,
servletexception {
HttpServletRequest ServletRequest = (httpservletrequest) req;
HttpServletResponse servletresponse = (httpservletresponse) res;
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Etagresponsewrapper wrappedresponse = new Etagresponsewrapper (Servletresponse, BAOs);
Chain.dofilter (ServletRequest, wrappedresponse);
byte[] bytes = Baos.tobytearray ();
String token = ' "' + etagcomputeutils.getmd5digest (bytes) + '";
Servletresponse.setheader ("ETag", token); Always store the ETag in the header
String Previoustoken = Servletrequest.getheader ("If-none-match");
if (Previoustoken! = null && previoustoken.equals (token)) {//Compare previous token with current one
Logger.debug ("ETag match:returning 304 Not Modified");
Servletresponse.senderror (httpservletresponse.sc_not_modified);
Use the same date we sent when we created the ETag the first time through
Servletresponse.setheader ("Last-modified", Servletrequest.getheader ("if-modified-since"));
} else {//First time Through-set last modified time-to-now
Calendar cal = Calendar.getinstance ();
Cal.set (Calendar.millisecond, 0);
Date lastmodified = Cal.gettime ();
Servletresponse.setdateheader ("Last-modified", Lastmodified.gettime ());
Logger.debug ("Writing body content");
Servletresponse.setcontentlength (bytes.length);
Servletoutputstream SOS = Servletresponse.getoutputstream ();
Sos.write (bytes);
Sos.flush ();
Sos.close ();
}
}
http://www.bkjia.com/PHPjc/318543.html www.bkjia.com true http://www.bkjia.com/PHPjc/318543.html techarticle recently, the public has shown a strong interest in the rest-style application architecture, which suggests that the elegant design of the web is beginning to get people's attention. Now, we are beginning to understand the "3W architecture (ARC ...