Nginx+tomcat7+memcached load Balancing cluster +session sharing

Source: Internet
Author: User
Tags couchbase openssl tomcat server pkill

Nginx+tomcat7+memcached load Balancing Cluster service Setup


Operating system: CentOS6.5
This document mainly explains how to build nginx+tomcat7+memcached Load Balancer Cluster server under CentOS6.5, Nginx is responsible for load balancing, TOMCAT7 is responsible for the actual service, memcached is responsible for synchronizing TOMCAT7 session To achieve the purpose of session sharing.
1. Installing Nginx
Nginx Official website: http://nginx.org/
Download the latest stable version. Before installing Nginx, you need to install the GCC, OpenSSL, pcre and zlib repositories first.

1.1 Installing GCC, gcc-c++
installation command:
#yum Install GCC
#yum Install gcc-c++

1.2 Installing OpenSSL
OpenSSL official website: http://www.openssl.org/
Installed version: openssl-1.0.1i.tar.gz
installation command:
#tar-ZXVF openssl-1.0.1i.tar.gz
#cd openssl-1.0.1i
#./config--prefix=/usr/local/openssl-1.0.1i #prefix指定安装目录
#make
#make Install
"Note":The config command is used here, not the usual configure command
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.

1.3 Installing Pcre
Pcre Official website: http://www.pcre.org/
Installed version: pcre-8.35.tar.gz
installation command:
#tar-ZXVF pcre-8.35.tar.gz
#cd pcre-8.35
#./configure--prefix=/usr/local/pcre-8.35 #prefix指定安装目录
#make
#make Install
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.

"Note":If the C + + compiler is not installed, the installation of this software will be error-free!

1.4 Installing Zlib
Zlib Official website: http://www.zlib.net/
Installed version: zlib-1.2.8.tar.gz
installation command:
#tar-ZXVF zlib-1.2.8.tar.gz
#cd zlib-1.2.8
#./configure--prefix=/usr/local/zlib-1.2.8 #prefix指定安装目录
#make
#make Install
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.

1.5 Installing Nginx
Installed version: nginx-1.6.1.tar.gz
installation command:
#tar-ZXVF nginx-1.6.1.tar.gz
#cd nginx-1.6.1
#./configure
--prefix=/usr/local/nginx-1.6.1 #prefix指定安装目录
--with-openssl=/home/zht/src/openssl-1.0.1i #指的是openssl源码路径
--with-pcre=/home/zht/src/pcre-8.3.5 #指的是pcre的源码路径
--with-zlib=/home/zht/src/zlib-1.2.8 #指的是zlib's Source path
--with-http_ssl_module
#make
#make Install
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.

1.5.1 Configuration Nginx
Configuration file directory:/usr/local/nginx-1.6.1/conf/nginx.conf
#cd/usr/local/nginx-1.6.1/conf
#vi nginx.conf

"The modified configuration file is as follows":
#创建进程的用户和用户组
User Feng Feng;

#useradd Feng

#服务进程数量, which is generally equal to the number of CPUs
Worker_processes 1;

#全局错误日志定义, it is recommended to turn on the error level log. [Debug | info | notice | warn | error | crit]
Error_log Logs/error.log error;
#error_log Logs/error.log Notice;
#error_log Logs/error.log Info;

#记录进程ID的文件
#pid Logs/nginx.pid;
Events {
One way in #epoll是多路复用IO (I/O multiplexing), but only for linux2.6 above the core, can greatly improve the performance of Nginx. Linux recommends using EPOLL,FREEBSD to use Kqueue.
Useepoll;
#一个worker_processe允许的最近并发连接数量
Worker_connections 1024;
}

HTTP {
Include Mime.types;
Default_type Application/octet-stream;

#log_format Main ' $remote _addr-$remote _user [$time _local] "$request" '
# ' $status $body _bytes_sent ' $http _referer '
# ' "$http _user_agent" "$http _x_forwarded_for" ';

#access_log Logs/access.log Main;

Sendfile on;
#tcp_nopush on;

#http连接的持续时间
Keepalive_timeout 65;

#gzip压缩设置
gzip on; #开启gzip
Gzip_min_length 1k; #最小压缩文件大小
Gzip_buffers 4 16k; #压缩缓冲区
#http的协议版本 (1.0/1.1), default 1.1, front End If squid2.5, use 1.0
Gzip_http_version 1.1;
#gzip压缩比, 1 compression is the fastest with minimum processing speed, 9 compression is the largest but the processing speed is the slowest (transmission is faster but CPU is more consumed)
Gzip_comp_level 2;
#和http头有关系, add a vary head, to the proxy server, some browsers support compression, and some do not support, so avoid wasting does not support compression, so according to the client's HTTP header to determine whether the need to compress
Gzip_varyon;
#gzip压缩类型, do not add text/html, otherwise there will be a warning message
Gzip_types text/plain text/javascript text/css application/xmlapplication/x-javascript Application/json;

#设定负载均衡的服务器列表, you can set multiple upstream, but Mysvr name to distinguish
Upstreammy ClusterServer1 {
#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.
Server 192.168.10.100:8080 weight=5;
Server 192.168.10.101:8080 weight=5;
Server 192.168.10.102:8080 weight=5;
}

server {
#nginx监听的端口号
Listen 80;
#域名可以有多个, separated by a space
server_name 192.168.10.222;

#字符编码方式
CharSet Utf-8;

#设定本虚拟主机的访问日志. Turning off logs can reduce IO and improve performance.
#access_log Logs/host.access.log Main;

#默认请求The site can also be configured for static and dynamic separation
Location/{
#定义服务器的默认网站根目录位置
root HTML;
#定义首页索引文件的名称
Index index.html index.htmindex.jsp;
List of servers #请求转向mysvr defined
Proxy_pass Http://myClusterServer1;
Proxy_redirect default;
#跟代理服务器连接的超时时间, it is important to note that the time out period cannot exceed 75 seconds, and when a server is dropped, it is forwarded to another server for 10 seconds.
Proxy_connect_timeout 10;
}

#error_page 404/404.html;

#redirect Server error pages to the static page/50x.html
#
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}

#proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# Proxy_pass http://127.0.0.1;
#}

#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root HTML;
# Fastcgi_pass 127.0.0.1:9000;
# Fastcgi_index index.php;
# Fastcgi_param Script_filename/scripts$fastcgi_script_name;
# include Fastcgi_params;
#}

#deny access to. htaccess files, if Apache ' s document Root
#concurs with Nginx ' s one
#
#location ~/\.ht {
# Deny All;
#}
}


# Anothervirtual host using mix of ip-, name-, and port-based configuration
#
#server {
# Listen 8000;
# Listen somename:8080;
# server_name somename alias Another.alias;

# location/{
# root HTML;
# index index.html index.htm;
#    }
#}


# Httpsserver
#
#server {
# Listen 443 SSL;
# server_name localhost;

# ssl_certificate Cert.pem;
# Ssl_certificate_key Cert.key;

# Ssl_session_cache shared:ssl:1m;
# ssl_session_timeout 5m;

# ssl_ciphers high:!anull:! MD5;
# ssl_prefer_server_ciphers on;

# location/{
# root HTML;
# index index.html index.htm;
#    }
#}

}
1.5.2 start and close Nginx
(1) Start
#/usr/local/nginx-1.6.1/sbin/nginx
Ensure that the 80 port of the system is not occupied by other programs


Reload the configuration file:
#/usr/local/nginx-1.6.1/sbin/nginx-s Reload

(2) Close:
#pkill Nginx

(3) Check whether the start is successful:
#netstat-ano | GREP80 result Input Description start successful

Open the browser to access the IP of this machine if the browser appears welcome to nginx! Indicates that Nginx is installed and running successfully. If you have configured a load Balancer server, you will see the Web pages in Tomcat.


2. Memcache installation
Memcached Official website: http://memcached.org/
Install memcached need to install Libevent,libevent official website: http://libevent.org/
This installation version:
Memcached-1.4.20.tar.gz
Libevent-2.0.21-stable.tar.gz
2.1 Installing Livevent
To see if you have installed: # RPM QA | grep libevent
If it is installed and the version is less than 1.3, first pass:
#rpm-E libevent--nodeps to uninstall.
#tar ZXVF libevent-2.0.21-stable.tar.gz
#cd libevent-2.0.21-stable
#./configure--prefix=/usr/local/libevent-2.0.21-stable #prefix指定安装路径
#make
#make Install

2.2 Installing memcached
#tar ZXVF memcached-1.4.20.tar.gz
#cd memcached-1.4.20
#./configure
--prefix=/usr/local/memcached-1.4.20
--with-libevent=/usr/local/libevent-2.0.21-stable
#make
#make Install

2.2.1 Check the loading situation
When the installation is complete, see if there is a memcached-1.4.20 directory in the directory specified by prefix

2.2.2 Viewing memcached and libevent version information
First navigate to the bin directory of the memcached:
#cd/usr/local/memcached-1.4.20/bin
Execute command:
#./memcached-i

2.2.3 Start memcached
#./memcached-d-v-p 12000-m 512-u Feng
Explanation:-D indicates that the MEMCACHED;-V is running as a daemon for output extract and error messages;-p Specifies the port number to listen on, and-m specifies the maximum memory that can be used, mb;-u specifies the account that runs memcached, not the root user.
Use # Ps-ef | grep memcached to view the process.

Follow the basic options: Description
-P <num> Listening TCP port (default: 11211)
-D Run as daemon memcached
-U <username> run memcached account, non-root user
-M <num> maximum memory usage, in MB, default of MB
-c <num> Number of soft connections, default is 1024 (maximum number of concurrent connections)
-V Output warnings and error messages
-VV request and return information for the print client
-H Printing Help information
-I print copyright information for memcached and libevent

2.2.4 using Telnet to verify that the service is available


Under CentOS (Linux) command:
# telnet 192.168.10.222 12000
Trying 192.168.10.222 ...
Connected to localhost (192.168.10.222).
Escape character is ' ^] '.
After the connection is successful, enter the command manually: stats
Manual input: Quit//exit
Connection Closed by foreign host
2.2.5 Stop memcached service
#pkill memcached

3. Install the tomcat+ configuration memcached
Tomcat official website: http://tomcat.apache.org/
3.1 Installing Tomcat
This time using Tomcat version: apache-tomcat-7.0.55.tar.gz
Unzip the tomcat into any directory.

3.2 Configuring memcached for Tomcat
3.2.1 Adding a library file for Tomcat
Tomcat to support memcached management session, you need to call some jar library files:
Add dependent jar packages for mem and MSM:
Couchbase-client-1.2.2.jar
Javolution-5.4.3.1.jar
Kryo-1.03.jar
Kryo-serializers-0.10.jar
Memcached-session-manager-1.6.5.jar
Memcached-session-manager-tc6-1.6.5.jar
Minlog-1.2.jar
Msm-kryo-serializer-1.6.5.jar
Reflectasm-0.9.jar
Spymemcached-2.10.2.jar
"Note":
msm1.6.5 relies on couchbase, need to add couchbase-client jar package, otherwise the launch will be reported: java.lang.noclassdeffounderror:com/couchbase/client/ Couchbaseclient.

Tomcat6 and TOMCAT7 use different MSM support packages: Memcached-session-manager-tc6-1.6.5.jar and Memcached-session-manager-tc7-1.6.5.jar, only one can be selected, or an error is initiated.

The version of the Lib package in the MSM source code is too low: spymemcached need to use 2.10.2, otherwise start tomcat error:

Java.lang.NoSuchMethodError:net.spy.memcached.MemcachedClient.set (ljava/lang/string;iljava/lang/object;) lnet/ Spy/memcached/internal/operationfuture;
Atde.javakaffee.web.msm.BackupSessionTask.storeSessionInMemcached (backupsessiontask.java:227)

Kryo-serializers need to use version 0.10, otherwise error:
caused By:java.lang.ClassNotFoundException:de.javakaffee.kryoserializers.DateSerializer

Some files: http://code.google.com/p/memcached-session-manager/downloads/list
Other documents to find out for themselves.
After downloading, place these library files in the Tomcat\lib directory.(all jar packages are included in the attachment)

3.2.2 Configuring memcached for Tomcat
Configuration file directory: Tomcat\conf\context.xml
Open the configuration file and add the following to the <Context>...</Context> node:
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:192.168.10.222:12000"#ip为mencached服务器的ip
Sticky= "false"
Requesturiignorepattern= ". *\. (png|gif|jpg|css|js|ico|jpeg) $ "
Sessionbackupasync= "false"
Copycollectionsforserialization= "false"
transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
"parameter description":
DocBase: Consistent with the appbase in <Host>, the site Deployment directory.
memcachednodes:memcached server information, when multiple servers are separated using spaces, such as:
n1:192.168.10.222:12001 n2:192.168.10.223:12000 n3:192.168.10.224:12000


There are also appbase= "deployment Directories" in the Server.xml configuration <Host> node
3.2.3 Test session Sharing
The test JSP code is as follows: index.jsp
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>tomcat7+memcached Sharing Session Test </title>
<body>
Sessionid:<%=session.getid ()%>
<BR>
Sessionip:<%=request.getservername ()%>
<BR>
Sessionport:<%=request.getserverport ()%>
<BR>
<%
Out.println ("This is Tomcat Server 8080.");
%>
</body>

Starting multiple Tomcat7 at the same time, I deployed 3, opened the browser to access the first TOMCAT7, and then visited the second and third TOMCAT7, Three Tomcat7 of the SessionID are the same: 5FBF6D6B6F37BE8248ED965536427005-N1, as long as not close the browser, no matter how refreshing, SessionID is unchanged. As a result, three TOMCAT7 can realize the session information sharing through memcached.

This article from "Feng 307" blog, declined reprint!

Nginx+tomcat7+memcached load Balancing cluster +session sharing

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.