Use nginx + Tomcat + memcached to build Web server load balancing

Source: Internet
Author: User

1 cause

Recently, a stress test was conducted on the newly developed web system, and it was found that the response speed of concurrent logon home pages under Tomcat's default configuration pressure to 600 people was severely affected, more than 2000 errors of 500 and 502 occur in one round. I made a look at the logon time statistics and printed out the total server processing time. I saw that some responses were indeed within 20 seconds, however, the average time is far different from that tested by LR. Therefore, it can be concluded that it does not take so much time to process the program. Due to the LAN testing, the network problem can also be ruled out. This outlines the problem in the Tomcat request response capability. First, increase the number of Tomcat threads to 1000, and find that the number of errors 500 and 502 has dropped to dozens, but the response time has not improved yet. Later, two Tomcat servers were started, and nginx was used for load balancing. The response time dropped by 40%. The processing duration of the two Tomcat servers was kept at about 1 second.

It seems that Tomcat performance is indeed a bottleneck of the system. It is necessary to assume that multiple servers are used to enhance the response capability. Previously, because it was just a test logon, multiple Tomcat servers do not need to share sessions, but they must work together in real use. Record the installation and configuration process of Server Load balancer.

 
2. Solution Selection

There are several ways to work together with multiple Tomcat servers. The following solutions can be considered:

1. Use the cluster method that comes with Tomcat. For more information about tomcat, see automatically copy session information in real time. It is easy to configure. However, the efficiency of this solution is relatively low, and the performance is not good under high concurrency.

2. Use nginx's access IP-based hash routing policy to ensure that the accessed IP address is always routed to the same tomcat. This configuration is simpler. However, our application is probably because a large number of users log on to the local area network at the same time, So load balancing will be useless.

3. memcached is used to centrally manage sessions of multiple Tomcat servers. This is the most direct solution, but the operation is also the most complicated.

Our system requires both performance and better use of Server Load balancer, so 3rd solutions are the first choice. The next step is the path to installation and setup.

 
3. install and configure
3.1 install memcached

1. Download the source package of libevent-1.4.14b-stable.tar.gzand memcached-1.4.7.tar.gz. The former is the dependency package of the latter and is an event-driven package.

2) the installation is very smooth, or the classic compilation and installation commands:

Tar zxvf libevent-1.4.14b-stable.tar.gz
CD libevent-1.4.14b-stable
../Configure -- prefix =/usr/local/libevent-1.4.14b
Make
Make install

Tar zxvf memcached-1.4.7.tar.gz
CD memcached-1.4.7
./Configure -- prefix =/usr/local/memcached-1.4.7 -- With-libevent =/usr/local/libevent-1.4.14b/
Make
Make install

3) Start memcached:

./Bin/memcached-D-M 256-u root-P 11211-C 1024-P/tmp/memcached. PID

3.2 memcached-session-Manager Configuration

It is already a mature solution for Tomcat to call memcached to store sessions. The open-source MSM can solve this problem. The jar package is used, which is vague in the official documentation. Here I use the kryo serialization solution, so I use more packages:

Kryo-1.03.jar

Kryo-serializers-0.8.jar

Memcached-2.5.jar (I officially see the latest has reached 2.7, but the MSM official said with 2.5, may not test the new package, especially the version of changelog 2.6 mentioned in the API has been adjusted, or don't upgrade it)

Memcached-session-manager-1.5.1.jar

Memcached-session-manager-tc7-1.5.1.jar

Minlog-1.2.jar

Msm-kryo-serializer-1.5.1.jar

Reflectasm-0.9.jar

These packages are all stored in the $ catalina_home/lib directory.

In addition, the four serialization solutions officially provided, kryo is the most efficient. For more information, see http://code.google.com/p/memcached-session-manager/wiki/serializationstrategies.

 

Next, modify the configuration file $ catalina_home/CONF/context. xml of Tomcat and change it to the new session storage method. Add the following content to the configuration file:

 
 
  1.     <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 
  2.       memcachedNodes="n1:127.0.0.1:11211" 
  3.       sticky="false" 
  4.       lockingMode="auto" 
  5.       sessionBackupAsync="false"   
  6.       sessionBackupTimeout="1000"  
  7. transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 
  8.       /> 

 

Add de. javakaffee. Web. MSM. Level = fine to the $ catalina_home/CONF/logging. properties file to view detailed Session access information in the Catalina. Out log.

 

In addition, add requesturiignorepattern = "in the Manager configuration ". *\. (PNG | GIF | JPG | CSS | JS) $ ". In Chrome browser test, the sessionid is suddenly changed, and then the interceptor jumps back to the homepage. If it is removed, everything will be normal, but the interceptor will only detect the action. It should be reasonable to say that it doesn't matter at all. Hope you can give me some advice!

 

3.3 nginx Configuration

Nginx is very simple. You only need to configure several more servers in upstream. Here I will post my Configuration:

 
 
  1. # User nobody;
  2. Worker_processes 16;
  3.  
  4.  
  5. Events {
  6. Use epoll;
  7. Worker_connections 65535;
  8. }
  9.  
  10.  
  11. HTTP {
  12. Include mime. types;
  13. Default_type application/octet-stream;
  14.  
  15. # Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
  16. # '$ Status $ body_bytes_sent "$ http_referer "'
  17. # '"$ Http_user_agent" "$ http_x_forwarded_for "';
  18.  
  19. # Access_log logs/access. Log main;
  20.  
  21. Client_header_buffer_size 32 K;
  22. Large_client_header_buffers 4 32 K;
  23. Client_max_body_size 8 m;
  24. Client_body_buffer_size 128 K;
  25.  
  26. Sendfile on;
  27. Tcp_nopush on;
  28.  
  29. # Keepalive_timeout 0;
  30. Keepalive_timeout 65;
  31.  
  32. Gzip on;
  33. Gzip_types text/JavaScript text/plain text/CSS application/XML application/X-JavaScript;
  34. Gzip_disable "MSIE [1-6] \. (?!. * Sv1 )";
  35.  
  36. Proxy_connect_timeout 300;
  37. Proxy_send_timeouts 300;
  38. Proxy_read_timeout 300;
  39. Proxy_buffer_size 16 K;
  40. Proxy_buffers 4 32 K;
  41.  
  42. Proxy_set_header X-forwarded-for $ remote_addr;
  43. Proxy_set_header connection close;
  44. Server_names_hash_max_size 1024;
  45. Server_names_hash_bucket_size 1024;
  46.  
  47. # Default cache parameters for use by Virtual Hosts
  48. # Set the cache path to tmpfs mounted disk, and the zone name
  49. # Set the maximum size of the on disk cache to less than the tmpfs file system size
  50. Proxy_cache_path./cache levels = keys_zone = pscms: 100 m max_size = 800 m;
  51. Proxy_temp_path./proxy;
  52.  
  53. # Configure backend server information
  54. Upstream web_server {
  55. # Ip_hash;
  56. Server localhost: 8080 max_fails = 3 fail_timeout = 30 s;
  57. Server localhost: 8180 max_fails = 3 fail_timeout = 30 s;
  58. }
  59.  
  60. Server {
  61. Listen 8888; # Listen for IPv4
  62. # Listen [:]: 80 default ipv6only = on; # Listen for IPv6
  63. SERVER_NAME localhost;
  64.  
  65. Charset UTF-8;
  66. Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
  67. '$ Status $ body_bytes_sent "$ http_referer "'
  68. '"$ Http_user_agent" "$ http_x_forwarded_for "';
  69. Access_log logs/host. Access. Log main;
  70. # Access_log off;
  71.  
  72. Location ~ . * \. (JSP | action )? $ {
  73. Proxy_set_header host $ http_host;
  74. Proxy_redirect off;
  75. Proxy_pass http: // web_server;
  76. Proxy_set_header host $ host;
  77. Proxy_set_header X-real-IP $ remote_addr;
  78. Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
  79. }
  80.  
  81. Location ~ . * \. (GIF | JPG | JPEG | PNG | BMP | SWF | JS | CSS) $ {
  82. # If the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream Server Load balancer pool for failover.
  83. Proxy_next_upstream http_502 http_504 error timeout invalid_header;
  84.  
  85. Proxy_cache pscms; # cache, using the Web cache zone cache_one
  86. Proxy_cache_valid 200 304 1 h; # set different cache times for different HTTP Status Codes
  87. Proxy_cache_valid 301 302 5 m;
  88. Proxy_cache_valid any 1 m;
  89. Proxy_set_header host $ host;
  90. Proxy_set_header X-real-IP $ remote_addr;
  91. Proxy_set_header X-forwarded-for $ remote_addr;
  92. Proxy_set_header accept-encoding "; # (or the backend server closes gzip), so that this machine will not cache compressed files, causing garbled characters
  93. Proxy_ignore_headers "cache-control" "expires"; # after this configuration is added, proxy_cache supports the expires set in the background.
  94. Proxy_pass http: // web_server;
  95. Expires 15 m;
  96. }
  97.  
  98. Location /{
  99. Proxy_set_header host $ http_host;
  100. Proxy_redirect off;
  101. Proxy_pass http: // web_server;
  102. Proxy_set_header host $ host;
  103. Proxy_set_header X-real-IP $ remote_addr;
  104. Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
  105. }
  106.  
  107. }
  108.  
  109. }

 

Reference:

1. http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration

2. http://wangrui.iteye.com/blog/500921

3 http://passover.blog.51cto.com/2431658/648182

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.