Build load balancer and Tomcat clusters under Linux (ubuntu12.04)

Source: Internet
Author: User
Tags session id server port tomcat server

Previous blog We built a nginx+tomcat server configuration, but implemented static and dynamic separations, but only one tomcat.

So, next we build multiple tomcat on our machine, first we copy a previous tomcat (my Tomcat is under/usr/local/), execute the command

Cp/usr/local/tomcat1/usr/local/tomcat2
You should use different environment variables for two different tomcat, so we need to modify the/etc/profile file configuration information

sudo gedit/etc/profile
And then change it into this.

    Java_home=/usr/java/jdk1.7.0_71     classpath=.: $JAVA _home/lib/tools.jar: $JAVA _home/lib: $JAVA _home/bin      Export Java_home CLASSPATH            # tomcat1      catalina_base=/usr/local/tomcat1      catalina_home=/usr/local/ TOMCAT1      Export catalina_base catalina_home             # tomcat2      catalina_2_base=/usr/local/tomcat2      Catalina_ 2_HOME=/USR/LOCAL/TOMCAT2      export catalina_2_base catalina_2_home                  tomcat_home=/usr/local/tomcat1      Export Tomcat_home            tomcat_2_home=/usr/local/tomcat2      export Tomcat_2_home  
Last step we need to modify the config file inside the tomcat2. First we modify the tomcat2/bin below startup.sh and shutdown.sh add the content

Export Java_home=/usr/java/jdk1.7.0_71export path= $PATH: $JAVA _home/binexport classpath=.: $JAVA _home/lib/tools.jar : $JAVA _home/lib: $JAVA _home/binexport catalina_home= $CATALINA _2_home  export catalina_base= $CATALINA _2_base  
And then go in there and change a few places in Tomcat2/server.xml.

<server port= "8005" shutdown= "shutdown" >      <connector port= "8080" protocol= "                             http/1.1" connectiontimeout= "20000"                      redirectport= "8443"/> <connector port= "8009" protocol= "AJP/1.3" redirectPort= " 8443 "/>  

The 8005->8006,8080->8081,8009->8010 is good (the specific port by itself will be OK)

Then go into the tomcat1/bin,tomcat2/bi below to execute

./startup.sh
Ok

Use the browser to go in localhost:8080 and localhost:8081 look, two Tomcat has been built.

-------------------------------------------------------------------------------------------------Next Load Balancing--------------- ------------------------------------------------------------------------------------------


Several ways of Nginx load

Nginx's upstream currently supports 4 different ways of distribution
1), polling (default)
Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.
2), Weight
Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.
2), Ip_hash
Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.
3), Fair (third party)
The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.
4), Url_hash (third party) '


We use the simplest ip_hash to achieve, but also can solve the session problem, originally wanted to use memcached to solve the session problem, but these days have been not smooth, fast play dead. Use this first,

Ip_hash actually not be able to completely solve the IP problem, because there are many users my IP at any time may be in the change, ip_hash the name you know, is through the principle of hash hash of the user's IP hash to the specified tomcat, naturally take it for granted to solve the session problem , it is best to use memcached access to achieve session sharing well, look at my nginx.conf configuration below:

    #运行nginx所在的用户名和用户组http://zyjustin9.iteye.com/blog/2017237,http://freeloda.blog.51cto.com/2033581/1288553 #user           www www;    #启动进程数 worker_processes 8;          #全局错误日志及PID文件 Error_log/usr/local/nginx/logs/nginx_error.log Crit;          Pid/usr/local/nginx/nginx.pid;          #Specifies the value for maximum file descriptors the can is opened by this process.    Worker_rlimit_nofile 65535;      #工作模式及连接数上限 Events {use Epoll;    Worker_connections 65535;      #设定http服务器, use its reverse proxy feature to provide load balancing support for HTTP {#设定mime类型 include mime.types;      Default_type Application/octet-stream;      include/usr/local/nginx/conf/proxy.conf;      #charset gb2312;      #设定请求缓冲 server_names_hash_bucket_size 128;      Client_header_buffer_size 32k;      Large_client_header_buffers 4 32k;                  #client_max_body_size 8m;      Sendfile on;            Tcp_nopush on;            Keepalive_timeout 60;          Tcp_nodelay on;# Fastcgi_connect_timeout 300;    # Fastcgi_send_timeout 300;    # Fastcgi_read_timeout 300;    # fastcgi_buffer_size 64k;    # fastcgi_buffers 4 64k;    # fastcgi_busy_buffers_size 128k;          # fastcgi_temp_file_write_size 128k;    # gzip on;    # Gzip_min_length 1k;    # gzip_buffers 4 16k;    # Gzip_http_version 1.0;    # gzip_comp_level 2;    # gzip_types Text/plain application/x-javascript text/css application/xml;            # gzip_vary on;    #limit_zone crawler $binary _remote_addr 10m;            Upstream tomcat{# The name of the load Balancer site is tomcat, you can take the ip_hash yourself;       # Optional, according to the source IP mode to select the Web server, omitted to follow the default round-robin selection of Web server 127.0.0.1:8080;    # The IP address of the Web server and the Tomcat publishing port server 127.0.0.1:8081;                   } server {Listen 80;            # site listening port server_name localhost;        # Site Name location/{root HTML;        Index index.html index.htm;      Proxy_pass Http://tomcat; # Load Balancer pointing to the hairCloth Service Tomcat}}} 
This will be OK, build two identical projects under two Tomcat, index.jsp file

Tomcat under the project JSP is the same as the best can be changed to Node2, then you can access http://localhost/UE2/index.jsp (project I randomly built)

You can see that different IPs are mapped to different tomcat, but this does not completely solve the session sharing and load balancing, but also need to continue to study memcached


Build load balancer and Tomcat clusters under Linux (ubuntu12.04)

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.