Nginx+tomcat+memcached

Source: Internet
Author: User
Tags memcached serialization

######### Nginx+tomcat+memcache ############

Experimental environment
rhel6.5
3 hosts iptables firewall off

Experimental host
172.25.8.2 Nginx
172.25.8.3 Tomcat memcached
172.25.8.4 Tomcat memcached

Principle of experiment
Nginx accepts client requests for load balancing on the front end
Tomcat-1 (T1) stores the session on Memcached-2 (T2). T1 stores the session on Memcached-1 only if M2 is not available (M1 is T1 's failovernode failover point). The advantage of using this configuration is that when both T1 and M1 crash, the session sessions are not lost and the single point of failure is avoided.

########## #安装tomcat ###########
Installation on 172.25.8.3 and 172.25.8.4 hosts

1. Installation package
Apache-tomcat-7.0.37.tar.gz # #tomcat安装包
Jdk-7u79-linux-x64.tar.gz # #jdk是java语言的软件开发工具包

2. Installation process
Tar zxf apache-tomcat-7.0.37.tar.gz-c/usr/local
Mv/usr/local/apache-tomcat-7.0.8/usr/local/tomcat
Tar zxf jdk-7u79-linux-x64.tar.gz-c/usr/local
Mv/usr/local/jdk1.7.0_79/usr/local/jdk

3. Configure Environment variables
Vim/etc/profile # #在文件末尾添加下列参数. Used to set environment variables must be specified otherwise tomcat cannot start
Export JAVA_HOME=/USR/LOCAL/JDK
Export classpath=: $JAVA _home/lib
Export path= $PATH: $JAVA _home/bin

Source/etc/profile # #刷新

echo $JAVA _home, Echo $PATH # #查看环境变量是否生效
Java-version # #命令检查java版本

4. Testing
/usr/local/tomcat/bin/startup.sh # #启动脚本. The closed script is under this directory shutdown.sh
Using catalina_base:/usr/local/tomcat
Using Catalina_home:/usr/local/tomcat
Using Catalina_tmpdir:/usr/local/tomcat/temp
Using Jre_home:/USR/LOCAL/JDK
Using CLASSPATH:/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

5. Browser access http://172.25.8.3:8080 can see the Cat Web page

6. Sync directly to the 172.25.8.4 host to complete the Tomcat installation
1) scp-r/usr/local/jdk/usr/local/tomcat/[email protected]:/usr/local
# #将172.25.8.3 's JDK and Tomcat directories are sent directly to the 172.25.8.4 host
2) scp/etc/profile [email protected]:/etc/profile # #同步环境变量
Souce/etc/profile
3)/usr/local/tomcat/bin/startup.sh # #启动脚本


######## memcached ###########
Installation on 172.25.8.3 and 172.25.8.4 hosts

1. Install and Open
Yum Install Memcached-y
/etc/init.d/memcached Star # #开启memcached


Serialization scheme settings for the ######### session ############
On 172.25.8.3 and 172.25.8.4 hosts

1. Turn off Tomcat
/usr/local/tomcat/bin/shundown.sh

2. Place the following packages in the/usr/local/tomcat/lib directory
Kryo-1.04.jar
Kryo-serializers-0.10.jar
Memcached-session-manager-1.6.3.jar
Memcached-session-manager-tc7-1.6.3.jar
Msm-kryo-serializer-1.6.3.jar
Reflectasm-1.01.jar
Minlog-1.2.jar
Spymemcached-2.7.3.jar

2. Edit Context.xml # #用于T1-M2,T2-M1 can be cached between
Vim/usr/local/tomcat/conf/context.xml
<Context>
......
<manager classname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "m1:172.25.8.3:11211,m2:172.25.8.4:11211"
Failovernodes= "M1" #在 Node2 This is set to "M2"
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
Transcoderfactoryclass= "De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>

3.session Serialization Scheme Introduction 4 kinds of official recommendations
1). Java serialization
2). Msm-kryo-serializer
3). Msm-javolution-serializer
4). Msm-xstream-serializer
One of the best-performing serialization schemes is Kryo, which uses the kryo approach.


############# Nginx Installation ######################
Installing Nginx on the 172.25.8.2 host

1. Installation package
Nginx-1.10.1.tar.gz
Nginx-sticky-module-1.0.tar.gz

2. Installation process
1) Yum install-y pcre-devel openssl-devel gcc

Tar zxf nginx-1.10.1.tar.gz
Tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz-c nginx-1.10.1
# #nginx-sticky-module for nginx third-party module, so that nginx support sticky mode, so-called sticky mode refers to the same user's access request is sent to the same Tomcat instance processing.

Useradd-s/sbin/nologin Nginx

CD nginx-1.10.1
./configure--user=nginx--group=nginx--prefix=/usr/local/nginx--with-http_ssl_module--with-http_stub_status_ Module--add-module=nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/##./configure What does the process need to fill?

Make;make Install

/usr/local/nginx/sbin/nginx # #开启ngixn
Browser Access http//172.25.8.2 # #测试nginx安装是否成功
/usr/local/nginx/sbin/nginx-s Stop # #关闭nginx

vim/usr/local/nginx/conf/nginx.conf # #编辑nginx配置文件
HTTP {
Upstream www.example.com {# #设置负载均衡模块
Sticky # #sticky模式
Server 172.25.8.3:8080;
Server 172.25.8.4:8080;
}
server {# #在server段添加location段
Location ~ \.jsp$ {# #所有jsp页面交给 tomcat processing, static and dynamic separation
Proxy_pass http://www.example.com;
}


##### #测试 Tomacat+memcache Effect ######
On the 172.25.8.3 and 172.25.8.4 hosts

1.# #编辑测试网页
vim/usr/local/tomcat/webapps/root/test.jsp
Test page code
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.util.*"%>
<body>
Server Info:
<%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>");%>
<%
Out.println ("<br> ID" + session.getid () + "<br>");
String dataname = Request.getparameter ("Dataname");
if (dataname! = null && dataname.length () > 0) {
String DataValue = Request.getparameter ("DataValue");
Session.setattribute (Dataname, DataValue);
}
Out.print ("<b>session list</b>");
Enumeration E = Session.getattributenames ();
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Session.getattribute (name). toString ();
OUT.PRINTLN (name + "=" + value+ "<br>");
SYSTEM.OUT.PRINTLN (name + "=" + value);
}
%>
%<form action= "test.jsp" method= "POST" >
%name:<input type=text size=20 name= "Dataname" >
%<br>
%key:<input type=text size=20 name= "DataValue" >
%<br>
%<input type=submit>
%</form>
%</body>
%
2. Browser access to 172.25.8.3:8080/test.jsp and 172.25.8.38080/test.jsp can be seen
Server info:172.25.8.3:8080 # #对应主机
ID bef1a4c679665595ea5180210933026f-m2 # #memcached的存储位置, T1 cache to m2
Session List 123 = 321 # #已经传的内容
Name # #要上传的name
Key # #要上传的keynamekey随意编辑
Submit Query # #上传按键

3. Effect view
1) View the log files generated by Tomcat by default on the 172.25.8.3 host Catalina.out
Tail-n5/usr/local/tomcat/logs/catalina.out
return content 123 = 321 # #172.25.8.3 local storage of uploaded 123=321 Records

2) View cached records by t1-m2 on the 172.25.8.4 host
telnet localhost 11211 # #11211是memcached默认端口连接查看本地的缓存记录
Get bef1a4c679665595ea5180210933026f-m2 # #get命令在telnet进入之后使用B: F-M1 is the ID shown in the browser just now
Return content: Omitted.. 123321 # #m2中有缓存记录
Quit # #quit命令退出

Telnet 172.25.8.4 11211 # #telnet IP 11211 can also be viewed

3) Normal mode T1 cache to m2t2 cache to M1

When M2 is turned off, the browser access will see that it has switched to M1

######### #测试nginx负载均衡的效果 ############
Vim/etc.hosts # #在哪个机子测试就编辑/etc.hosts
172.25.8.2 www.example.com
"Attention off fire Strong"


Summary visit http://www.example.com/test.jsp
1) Different host accesses are dispatched to different Tomcat instances for processing
2) requests from the same host are given to the same tomcat instance for processing
3) If you drop the Tomcat instance that is currently responding, Nginx will automatically dispatch the user's request to another Tomcat instance without losing the session.







This article from the "12148275" blog, reproduced please contact the author!

Nginx+tomcat+memcached

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.