TomcatA将 session 存储在 memcachedB上。只有当 MB不可用时,TA 才将 session 存储在 memcachedA 上(MA 是 TA failoverNode)。使用这种配置的好处是,当 TA 和 MA 同时崩溃时也不会丢失 session 会话,避免单点故障。
主机角色:
node1: 172.25.24.1: nginx、tomcat、memcached
noe2 : 172.25.24.2 :tomcat、memcached
nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz ##静态模块
server1:
root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]# yum install gcc pcre-devel openssl-devel -y
[root@server1 nginx-1.10.1]# make && make install
[root@server1 nginx-1.10.1]# cd /usr/local/nginx/
[root@server1 nginx]# ls
conf html logs sbin
[root@server1 nginx]# du -sh
5.8M .
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ls
nginx
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /sbin/ ##软连接,方便启动nginx
[root@server1 sbin]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 sbin]#
[root@server1 lib]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 lib]#
[root@server1 lib]# nginx ##开启服务测试nginx是否完好
[root@server1 lib]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream tomcat{
sticky; ##函数
server 172.25.24.1:8080; ##监听端口tomcat端口就为8080
server 172.25.24.2:8080;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /usr/local/tomcat/webapps/ROOT; ##添加访问页面
index index.php index.html index.htm;
#index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.jsp$ {
#proxy_pass http://127.0.0.1:8080;
proxy_pass http://tomcat;
}
# 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.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual 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;
# }
#}
# HTTPS server
#
#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;
# }
#}
}
[root@server1 lib]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
[root@server1 lib]# nginx -s reload
server1与server2共同进行:
配置JDK
JDK是运行一切java所需要的基础环境
[root@server2 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/ ##这是一个java的编译代码
[root@server2 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 ~]# cd /usr/local/
[root@server2 local]# ln -s jdk1.7.0_79/ java
[root@server2 local]# vim /etc/profile ##添加环境
root@server2 local]# source /etc/profile ##刷新环境
[root@server2 ~]# vim test.java ##写一个简单的java来测试是否安装成功
root@server2 ~]# javac test.java ##检测
[root@server2 ~]# java test ##运行
Hello World!
安装tomcat
[root@server2 ~]# cd /usr/local/
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat ##作软连接,软连接的好处就是方便
[root@server2 local]# cd tomcat/bin
[root@server2 bin]# ./startup.sh ##开启tomcat服务
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 bin]# cd /usr/local/tomcat/webapps/ROOT/ ##这是tomcat的发布目录
[root@server2 ROOT]# vim test.jsp ##tomcat的文件格式为jsp
[root@server2 ROOT]# cat test.jsp
gge contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<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>
%
%</html>pgcheck=0
测试tomcat是否安装成功:
server1上JDK的安装与server2完全相同:
memcached的安装
root@server1 tomcat]# yum install memcached -y
[root@server2 ~]# yum install memcached -y
[root@server1 tomcat]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@server1 tomcat]#
[root@server2 ~]# /etc/init.d/memcached start
Starting memcached:
[root@server1 tomcat]# vim /usr/local/tomcat/conf/context.xml
[root@server1 tomcat]# scp /usr/local/tomcat/conf/context.xml server2:/usr/local/tomcat/conf/
并把其中的failoverNodes改为n2
[root@foundation24 jar]# scp * server2:/usr/local/tomcat/lib
[root@server2 lib]# ls
annotations-api.jar jsp-api.jar tomcat7-websocket.jar
asm-3.2.jar kryo-1.04.jar tomcat-api.jar
catalina-ant.jar kryo-serializers-0.10.jar tomcat-coyote.jar
catalina-ha.jar memcached-session-manager-1.6.3.jar tomcat-dbcp.jar
catalina.jar memcached-session-manager-tc7-1.6.3.jar tomcat-i18n-es.jar
catalina-tribes.jar minlog-1.2.jar tomcat-i18n-fr.jar
ecj-4.4.2.jar msm-kryo-serializer-1.6.3.jar tomcat-i18n-ja.jar
el-api.jar reflectasm-1.01.jar tomcat-jdbc.jar
jasper-el.jar servlet-api.jar tomcat-util.jar
jasper.jar spymemcached-2.7.3.jar websocket-api.jar
[root@server1 tomcat]# yum install -y telnet
[root@server2 lib]# yum install -y telnet
测试:
[ root@server2 local]# telnet localhost 11211 ##用telnet命令可以看出server1上的msm缓存,存到server2上了
root@server1 local]# cd tomcat/
[root@server1 tomcat]# bin/shutdown.sh ## 关闭tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
当关闭server1的tomcat时,刷新,切换到server2,存储还在,因为缓存在server2 上
打开server1的tomcat,并且在浏览器中输入xixi=xixi
,