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.jar http://repo1.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
yum 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 group Balancermember ajp://172.18.68.21:8009 route=tomcata balancermember ajp://172.18.68.22:8009 route=tom CATB Proxyset lbmethod=byrequests</proxy>#<virtualhost *:80> # Create a virtual host ServerName www.baidu.com Proxyvia on proxyrequests Off PROXYPR Eservehost on Proxyvia in <proxy *> Require all granted </pro Xy> proxypass/balancer://tomcatservers/# Set all dispatch to backend Proxypassreverse/balanc er://tomcatservers/# Set all scheduled to backend <location/> # cent OS7 is denied by default, so allow Require all granted </Location> <location/balancer-manage R> # Apache Admin page SetHandler Balancer-manager proxypass! # Matching to Apache Admin page does not proxy Require all granted # settings Allow & Lt;/location></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
yum 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在host标签内配置以下内容<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为后端两个memcached地址#failoverNodes="n2" # 设定n2备用。所以n1就是主requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" # 匹配到这些结尾的文件不考虑cookietranscoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/></Context> # 黄色标记部分为流式化管理工具,如果使用其他流式化工具,更改这两个名即可
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添加如下内容:<%@ page language="java" %>
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" %>
Start the service
systemctl start tomcat
Iv. Configuration of memcachedMechached basically do not have any configuration, the installation of the service can be started.
yum install memcachedsystemctl start memcached
V. TestingUse 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
Nginx+tomcat+memcached Implementing session Hold