The high performance of the Java Web front-end

Source: Internet
Author: User
Tags flush rar java web tomcat tomcat server

Introduction

In the first part of the front-end optimization, the main explanation of the static resources of some optimization measures, including image compression, CSS sprites technology, GZIP compression and so on. In this part, this paper will explain the important Flush mechanism, static and dynamic separation, HTTP persistent connection, flexible application of HTTP protocol, CDN and so on. Combining these technologies or ideas is believed to make the performance of Java WEB applications a higher level.

The use of Flush mechanism

In fact, in Web technology, the Flush mechanism is not new, its idea is not to wait until the content of the Web page all loaded, one-time write back to the client, but can be part of successive return. If the page is large, it is obviously an unwise choice to write back all of the content at once, because it can create a long blank space for the page. The Flush mechanism allows developers to gradually return the contents of a Web page to the client in a document-flow order, which allows the user to know that our system is working, only to wait a little longer, so that the user will be "willing" to wait. The Flush mechanism is a classic way to improve the user experience, and has been in use ever since. If the Web page is large, this mechanism is recommended for use. In Java Web technology, implementing Flush is very simple, as long as the Flush method of Httpservletresponse.getwriter output stream is invoked, the content that has already finished loading can be written back to the client.

But does every page have to use the technology? I certainly do not suggest so. It is also beneficial to have the page content loaded and then returned to the client at once. We know that the network transmission also has the largest transmission unit, the content loaded after the one-time output can maximize the use of transmission bandwidth, reduce the block, reduce the number of transmission, that is, in fact, Flush mechanism will increase the user wait time, increase browser rendering time, but for large web pages, It is worthwhile to reduce this efficiency to enhance the user experience.

Static and dynamic separation

The so-called static and dynamic separation, that is, the Web application in the content of the statically and dynamically on different Web servers, targeted processing of dynamic and static content, so as to achieve performance improvement. This article is based on the Java Web to explain web optimization, while the Java Web's mainstream server software is Tomcat. Sadly, Tomcat is weak in the ability to handle concurrent and static resources, and this is where Tomcat is criticized. But afterward, now that we've chosen the Java Web, we should use our programmers ' minds to try to improve performance. Static and dynamic separation is one of the methods, since the ability of Tomcat to deal with passive resources is weak, then the processing task of static resources to the appropriate software, and let Tomcat focus on processing Jsp/servlet request.

For static resource processing server software, we can choose Nginx, it is a Russian development of software, seems to be more excellent than Apache. It supports high concurrency and has a strong ability to handle static resources, which is exactly what we want, isn't it? In fact, there are a lot of static and dynamic separation scheme, some people use the combination of apache+tomcat, but also the combination of Tomcat+tomcat, but two Tomcat were placed in different hosts, different domain names. The APACHE+TOMCAT scheme is the same as the Nginx scheme principle, they are all based on reverse proxy, and the configuration of Apache is slightly more complicated than using Nginx configuration and static separation. In Apache, the Mod_proxy module is responsible for the implementation of the reverse proxy. The core configuration, as shown in Listing 1, is part of my involvement in a project.

Listing 1. Apache core configuration with static and dynamic separation

 <proxy balancer://proxy> balancermember http://192.168.1.178:8080 loadfactor=1 balancermember http://192.168.1.145:8080 loadfactor=1 </Proxy> namevirtualhost *:80 <virtualhost *:80> ServerAdmin 
       service@xuanli365.com ServerName www.xuanli365.com documentroot/www directoryindex index.shtml <Directory/www> allowoverride all AddType text/html. shtml Ad 
       Dtype application/x-rar. rar AddHandler server-parsed. shtml Options +includesnoexec
   </Directory> rewriteengine on proxyrequests off proxypass/static/! Proxypass/balancer://proxy/proxypassreverse/balancer://proxy/proxypreservehost on </VirtualHost> 

From the Apache official introduction to the Mod_proxy module, we know that the Proxypass attribute can map a remote server to the URL space of the local server, which means that this is an address mapping feature. In the configuration in Listing 1, when the access path is not under/static/(! is not), it is forwarded to the backend server (that is, Tomcat), otherwise the native is accessed if it is a/static/path. For example, when accessing www.xuanli365.com/static/css/index.css, the actual processing request is the Apache server, while accessing the www.xuanli365.com/index.jsp, then Apache The request is forwarded to the back-end Tomcat server, and the actual page accessed is http://192.168.1.178 (or 145): 8080/index.jsp, which enables the separation of the movement. The Simple load balancing (loadfactor factor) is actually included in the configuration of Listing 1.

In fact, we can easily open a large portal to see, I opened the Tencent website, arbitrary view of the address of the two pictures, I found that one is: http://mat1.gtimg.com/www/iskin960/ Qqcomlogo.png, while the other is: Http://img1.gtimg.com/v/pics/hv1/95/225/832/54158270.jpg. It can be seen that the site to store pictures resources using a number of domain names, we use the host command of Linux to view the IP address of two domain names, the results are shown in Figure 1.

Figure 1. Static and dynamic separation of a website

As you can see, by looking at the IP address, we find that these images are likely to be stored on different hosts (why is it possible?). Because a host can have more than one IP, and the content of the picture and the dynamic content of the Web page is not under the same IP, it is likely to be static separation. More than one domain name has been mentioned earlier, you can increase the number of concurrent downloads of browsers, improve download efficiency.

In this paper, we use another strategy to demonstrate the static and dynamic separation, and its approximate structure is shown in Figure 2.

Figure 2. The static and dynamic separation structure designed in this paper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.