Three ways to keep a session
- Sessions Sticky Session binding: By implementing a unified session in the configuration of the front-end scheduler sent to the same post-originating server
- Session cluster conversation cluster: Configure Tomcat to keep all Tomcat session information consistent.
- Session Server Sessions Service: all session to the dedicated session service management.
Here's a third way to do session server with memcached
First, the structure diagram
System environment
All servers use CentOS7.3
The required packages
The following files are placed in the class library directory on the Tomcat server host
rpm -ql tomcat-lib # /usr/share/java/tomcat/ # yum安装默认在此目录/usr/local/tomcat/lib # 在官网rpm安装的默认在此目录了
Memcached-session-manager-1.8.3.jarhttpRepo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.8.3/memcached-session-manager-tc7-1.8. 3.jar http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.8.3/ spymemcached-2.11. 1.jar http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/javolution -5.4. 3. 1.jar http://memcached-session-manager.googlecode.com/svn/maven/javolution/ Javolution/5.4.3.1/msm-javolution-serializer-1.8. 3.jar http://repo1.maven.org/maven2/de/ javakaffee/msm/msm-javolution-serializer/1.8.3/
Second, Apache detailed configuration
Installing httpd
install httpd
Creating a configuration file
/etc/httpd/conf.d
Create a new configuration file under the
Shell > vim/etc/httpd/conf.d/ajp-tomcat.conf#<proxy balancer://tomcatservers># define host GroupBalancermember ajp://172.18.68.21:8009 Route=tomcataBalancermember ajp://172.18.68.22:8009 ROUTE=TOMCATBProxyset lbmethod=byrequests</proxy>#<virtualhost *:80># Create a virtual hostServerName www.baidu.comProxyviaOnProxyrequestsOffProxypreservehostOnProxyviaOn<proxy *>RequireAll granted</Proxy>proxypass/balancer://tomcatservers/# Set all schedules to backendproxypassreverse/balancer://tomcatservers/# Set all scheduled to back end <location/> # centos7 default deny, so allow Require All granted </Location> <Location/balancer-manager> Apache Admin page SetHandler Balancer-manager Proxypass! # match to Apache admin page not proxy Require all granted # settings allow </Location>& lt;/virtualhost>#Shell > Systemctl start httpd
Iii. Configuration of Tomcat
The configuration of the two Tocat is similar, the configuration files are basically the same, only the two test pages created later are different.
Installing Tomcat
install tomcat tomcat-admin-webapps tomcat-webapps
Modifying a configuration file
Under/etc/tomcat/, there is a server.xml file, open this file and add or modify the following configuration within the host tag.
Vim/etc/tomcat/server.xml Configure the following within the host tag <context path= "/myapp" Docbase= "MyApp" reloadable= "true" > <manager classname= " De.javakaffee.web.msm.MemcachedBackupSessionManager "Memcachednodes=" N1 : 172.18.68.31:11211,n2:172.18.68.32:11211 " #n1: Ip:prot for back end two memcached address #failoverNodes =" N2 "# set n2 standby. So N1 is the main requesturiignorepattern=# match files to these ends without considering Cookietranscoderfactoryclass=" de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory "/></context>" Span class= "hljs-comment" ># yellow flag section is a streaming management tool, if you use a different streaming tool, change both names
Download the packages that depend on
Place the file with the end of all. jar files at the beginning of the article with a soft cloth placed in the class library directory on the Tomcat server host
rpm -ql tomcat-lib # 通过此命令查看.jar文件存放的目录,就是类库文件的目录/usr/share/java/tomcat/ # yum安装的tomcat类库路径/usr/local/tomcat/lib # 在官网rpm安装的tomcat类库路径
Add Test page
The following two test pages are not the same, so they are created separately under their respective directories.
Add a test page to Tomcata
Shell > MKDIR-PV/usr/local/tomcat/webapps/test/web-inf/{classes,lib}shell > Vim/usr/local/tomcat/webapps/test/index.jsp Add the following content:<%@ page language= "java"%><title>tomcata</title><body><font color= "Red" >tomcata.magedu.com</font><table align= "centre" border= "1 "> <tr> <td> Session id</td><% session.setattribute ("magedu.com" , "magedu.com"); %> <td><%= Session.getid ()%></td> </tr> <tr> <td>created on</td> <td><%= session.getcreationtime ()%></td> </tr></table> </body></HTML>
Add a test page to TOMCATB
Shell > MKDIR-PV/usr/local/tomcat/webapps/test/web-inf/{classes,lib}shell > Vim/usr/local/tomcat/webapps/test/index.jsp<%@ page language= "java"%><title>tomcatb</title><body><font color= "Blue" >tomcatb.magedu.com</font><table align= "centre" border= "1 "> <tr> <td> Session id</td> <% session.setattribute ("magedu.com" , "magedu.com"); %> <td><%= Session.getid ()%></td> </tr> <tr> <td>created on</td> <td><%= session.getcreationtime ()%></td> </tr> </table> </body>
Start the service
start tomcat
Iv. Configuration of memcached
Mechached basically do not have any configuration, the installation of the service can be started.
install memcachedsystemctl start memcached
V. Testing
Use the browser to access the http://172.18.68.11, and then refresh multiple times, as long as the value of SessionID This column is not changed to indicate that the session has remained unchanged
Installing lamp (apache,mariadb,php7.1) on Ubuntu 17.10 http://www.linuxidc.com/Linux/2017-10/148065.htm
Use apache+mariadb+php7.1 to install WordPress http://www.linuxidc.com/Linux/2017-10/148066.htm on Ubuntu 17.10
This article permanently updates the link address : http://www.linuxidc.com/Linux/2017-11/148718.htm
Reprinted from Aubin
Apache+tomcat+memcached Implementing session Hold