Nginx-based tomcat load balancing and clustering (super simple)

Transferred from jacky2007: Today I saw the article "Apache-based tomcat load balancing and cluster configuration" became a javaEye hotspot.
At a glance, it feels too complicated and there are too many things to configure, so I write a more concise method here.

To cluster tomcat is mainly to solve the problem of SESSION sharing, so I use memcached to save sessions, and multiple TOMCAT servers can share SESSION.

You can write your own tomcat extension to save SESSION to memcached.
It is recommended to use the open source project memcached-session-manager (http://code.google.com/p/memcached-session-manager/ ), hereinafter referred to as msm.

How to install nginx, memcached, tomcat will not say much.

First explain the test environment:
tomcat1, nginx, memcached are installed in 192.168.1.11
tomcat2 is installed in 192.168.1.101

to implement nginx-based tomcat load balancing and cluster configuration step by step

1 , tomcat cluster
    1, first download msm and its dependencies
    http: //memcached-session-manager.googlecode.com/files/memcached-session-manager-1.3.0.jar

    http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-jodatime-1.3.0.jar

http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer -cglib-1.3.0.jar

http://spymemcached.googlecode.com/files/memcached-2.4.2.jar

http://memcached-session-manager.googlecode.com/files/javolution-5.4.3.1.jar

2. Put these 5 packages in the $TOMCAT_HOME/lib directory

3. Modify the $TOMCAT_HOME/conf/server.xml

Xml code to collect the code
<Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable= "true" > 
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 
    memcachedNodes="n1:localhost:11211" 
    requestUriIgnorePattern=".*\.  (png|gif|jpg|css|js)$" 
    sessionBackupAsync="false" 
    sessionBackupTimeout="100" 
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" 
    copyCollectionsForSerialization="false" 
    /> 
</Context> 
The memcachedNodes here is to fill in the memcached node, when there are multiple nodes It can be separated by a space, such as:
n1:localhost:11211 n2:localhost:11212

The unit of sessionBackupTimeout is minutes

E:/java_codes/TestSession/WebContent Replace it with your WEB directory

  and restart the two TOMCAT after modification, this time has been resolved The sharing problem of SESSION.

Second, configure nginx to achieve load balancing
   Take my nginx.conf as an example
Xml code Collection code
#user nobody; 
worker_processes 1; 
 
error_log logs/error.log;   
 
events { 
    worker_connections  1024; 

 
 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
 
    sendfile        on; 
    keepalive_timeout  65; 
 
    #gzip  on; 
    upstream  www.docyeah.com   { 
              server   192.168.1.11:8080; 
              server   192.168.1.101:8080; 
    } 
    server { 
        listen       80; 
        server_name  www.docyeah.com; 
        charset utf-8; 
        location / { 
            root   html; 
            index  index.html index.htm; 
            proxy_pass        http://www.docyeah.com; 
            proxy_set_header X-Real-IP $remote_addr; 
            client_max_body_size 100m; 
        } 
 
 
        location ~ ^/(WEB-INF)/ {  
        deny all;  
        }  
 
        error_page 500 502 503 504 /50x.html; 
        location = /50x.html { 
            root html; 
        } 
 
    } 


Replace www.docyeah.com with your domain name
192.168.1.11 and 192.168.1.101 with your server's IP

OK, it's done. Just start nginx.

This is the load balancing and clustering solution I used. I hope everyone will make a brick.

ps: There is a problem with the editor of javaeye. After adding color to the code, it is messed up


http://www.iteye.com/topic/676347

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171565&siteId=291194637