session sharing mechanism

Session sharing mechanism tomcat backs up cache information in memcache

1. The use of tomcat and nginx

[root@server1 conf]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# /etc/init.d/php-fpm start
Starting php-fpm done
[root@server1 conf]# nginx##开启服务
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload


(18)
http {
        upstream westos{
        ip_hash;##开启ip_hash固定一个主机访问的后端服务器
        server 172.25.39.2:8080;
        server 172.25.39.3:8080;
}

 (90)   location ~ \.jsp$ {
                proxy_pass http://westos;   ##所有以.jsp结尾的都转换

        }

[root@server1 conf]# cat nginx.conf————The contents of all files without large-scale comments are as follows

用;这里表示注释
;user  nobody;
worker_processes  2;
worker_cpu_affinity 01 10;
;error_log  logs/error.log;
;error_log  logs/error.log  notice;
error_log  logs/error.log  info;


;pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


http {
 upstream westos{
        ip_hash;
        server 172.25.78.2:8080;
        server 172.25.78.3:8080;
}


    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

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;
}

location ~ \.jsp$ {
                proxy_pass http://westos;  
        }


    }



   server {
       listen       443 ssl;
       server_name  localhost;

       ssl_certificate      cert.pem;
       ssl_certificate_key  cert.pem;

       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;
       }
   }
server {
listen 80;
server_name www.westos.org;
location / {

proxy_pass http://westos;

}
}
}

Writing test page numbers
[root@server2 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server2 ROOT]# vim test.jsp
content copied from Lao Wu document

<%@ page 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>");
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>

scp test.jsp 172.25.39.3:/root/tomcat/webapps/ROOT##Copy the
same page for 3

nginx+tomcat+memcache####shared implementation

1.
get {—from Lao Wu
asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc6-1.6.3.jar
memcached -session-manager-tc7-1.6.3.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar }
Because the tomcat used is 7, put 6's deletion

2. Put these things into /usr/local/tomcat/lib

3. Open memc
/etc/init.d/memecached start of server2 and server3

  1. vim /usr/local/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.39.2:11211,n2:172.25.39.3:11211"
failoverNodes="n1"##在server2和server3上分别弄n1和n2如果tomcat1 故障了那么就n1取缓存数据否则就是取自己的缓存---n1就相当于上课讲解的memc2
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>


. \ / .
. X .
. / \ .

Tomcat-1 (T1) stores the session on memcached-2 (T2). Only when M2 is unavailable, T1 stores the session on memcached-1 (M1 is the T1 failoverNode). The advantage of using this configuration is that when T1 and M1 crash at the same time, the session will not be lost, avoiding a single point of failure.

5. Open the tomcat service
/usr/local/tomcat/bin/startup.sh (close the service)
/usr/local/tomcat/bin/shutdown.sh

cd /usr/local/tomcat
tail -f logs/catalina.out Test whether the service is successfully opened

(INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2])证明成功

6. yum install telnet -y do both -- telnet function monitoring

Test
172.25.78.1/test.jsp
telnet localhost 11211 Remote monitoring local port 11211 monitoring storage

[root@server3 lib]# telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
get 48844C8F70F022944863813004730A7E-n2
VALUE 48844C8F70F022944863813004730A7E-n2 2048 136
W]hB8]h01]h)]h)#48844C8F70F022944863813004730A7E-n2user2456user3789user8888usr1123
END

write picture description here

Guess you like

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