In this section, I will show you how to implement Tomcat session persistence based on memcached. Let's get started now.
Tomcat Introduction
The Tomcat server is a free open-source Web application server, which is a lightweight application server. It is widely used in small and medium systems and concurrent users, is the first choice for developing and debugging JSP programs. In fact, Tomcat is an extension of the Apache server, but it runs independently. Therefore, when you run tomcat, it actually runs independently as a process independent of Apache. In our application process, Apache is used to process HTML page requests, while Tomcat actually runs JSP pages and servlets. In addition, like Web servers such as Tomcat and IIS, Tomcat can process HTML pages. In addition, it is a Servlet and JSP Container, and an independent servlet container is the default mode of Tomcat. However, Tomcat cannot process static html as well as Apache servers.
Tomcat installation
The first thing we need to do isDeploy the Java environmentOnly install JDK.
Now I have a JDK.
I'm using the jdk-7u9-linux-x64.rpm version.
Install it directly.
[[Email protected] ~] # Rpm-IVH jdk-7u9-linux-x64.rpm
Tomcat is also downloaded here. Apache-tomcat-7.0.55.tar.gz can be used for direct decompression.
[[Email protected] ~] # Tar xf apache-tomcat-7.0.55.tar.gz-C/usr/local/we put it in/usr/local/
Generally, we make a link.
[[Email protected] local] # ln-sv apache-Tomcat-7.0.55/tomcat
Next, modify the environment variables.
[[Email protected] local] # Cat/etc/profile. d/Java. sh: Java variable
Java_home =/usr/Java/latest
Path = $ java_home/bin: $ path
Export java_home path
[[Email protected] local] # Cat/etc/profile. d/tomcat. sh: Tomcat
Export catalina_home =/usr/local/tomcat
Export Path = $ catalina_home/bin: $ path
Now we can use Catalina. Sh to start Catalina. Sh, which is the Service Startup Script.
[[Email protected] local] # Catalina. Sh start
Using catalina_base:/usr/local/tomcat
Using catalina_home:/usr/local/tomcat
Using catalina_tmpdir:/usr/local/tomcat/temp
Using jre_home:/usr/Java/latest
Using classpath:/usr/local/tomcat/bin/Bootstrap. jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
In this way, Tomcat is started.
[[Email protected] local] # netstat-tnlp
Active Internet connections (only servers)
PROTO Recv-Q send-Q local address foreign address State PID/program name
TCP 0 0 0.0.0.0: 111 0.0.0.0: * Listen 1473/rpcbind
TCP 0 0 0.0.0.0: 22 0.0.0.0: * Listen 1726/sshd
TCP 0 0 127.0.0.1: 631 0.0.0.0: * Listen 1578/cupsd
TCP 0 0 127.0.0.1: 25 0.0.0.0: * Listen 1823/Master
TCP 0 0 0.0.0.0: 46718 0.0.0.0: * Listen 1549/rpc. statd
TCP 0 0: 36716: * Listen 1549/rpc. statd
TCP 0 0: 111: * Listen 1473/rpcbind
TCP 0 0: 8080: * Listen 2147/Java
TCP 0 0: 22: * Listen 1726/sshd
TCP 0 0: 1: 631: * Listen 1578/cupsd
TCP 0 0: 1: 25: * Listen 1823/Master
TCP 0 0: FFFF: 127.0.0.1: 8005: * Listen 2147/Java
TCP 0 0: 8009: * Listen 2147/Java
Through the above results, we can see that the service has started three ports: 8080 8009 8005
Now we can view it in a browser. Access through port 8080.
650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/28/8733640_1411912261PWnB.png "Height =" 515 "/>
OK, Tomcat service, and can be enabled.
Next, let's take a look at how to build the Apache + Tomcat + memcached architecture.
Lab:
Front-end Apache: 192.168.1.200
Tomcat node: 192.168.1.201 (tomcata), 192.168.1.202 (tomcatb)
Memcached nodes (one master and one slave for high availability): 192.168.1.201, 192.168.1.202 (machine memory is limited, so no independent host is used)
Experiment topology:
650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/28/8733640_1411912262jXIt.png "Height =" 374 "/>
Front-end Apache server configuration
We use the Apache proxy module here. Because the kernel in centos6.5 supports this proxy module, we only need to install httpd directly in Yum and then add the following files to the configuration file:
[[Email protected] ~] # Vim/etc/httpd/CONF. d/mod_proxy.conf
<Proxy balancer: // lbcluster1>
Balancermember http: // 192.168.1.202: 8080 loadfactor = 1
Balancermember http: // 192.168.1.201: 8080 loadfactor = 1
Proxyset lbmethod = byrequests
</Proxy>
Proxyvia off
Proxyrequests off
Proxypass/balancer: // lbcluster1/
Proxypassreverse/balancer: // lbcluster1/
Memcached Server
The cache server can be directly installed on two nodes using yum, which is very simple.
Yum install memcached
Start the service after installation
[[Email protected] ~] # Service memcached start
Starting memcached: [OK]
You can see that port 11211 is enabled.
[[Email protected] ~] # SS-tnl
State Recv-Q send-Q local address: Port peer address: Port
Listen 0 128: 11211 :::*
Listen 0 128 *: 11211 *:*
Tomcat configuration
The most important thing is the tomcat configuration.
We use memcached-session-manager to implement session persistence. However, he needs several Class Libraries and I have downloaded them.
This is the official address of the project, you can download http://code.google.com/p/memcached-session-manager/ here
[[Email protected] MSM] # ls
Javolution-5.5.1.jar
Memcached-session-manager-tc7-1.8.2.jar
Spymemcached-2.10.2.jar
Memcached-session-manager-1.8.2.jar
Msm-javolution-serializer-1.8.2.jar
Put these libraries into the Lib library of Tomcat,
[[Email protected] MSM] # Cp */usr/local/tomcat/lib/
Go in and check to make sure the copy is successful.
650) This. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-Right: 0px; "Title =" image "border =" 0 "alt =" image "src =" http://img1.51cto.com/attachment/201409/28/8733640_14119122623OkD.png "Height =" 137 "/>
Now, modify the configuration file./Usr/local/tomcat/CONF/server. xml
Define a context container for testing on the host of two Tomcat servers, respectively,Create a session manager in it, as shown below:
<Context Path = "/test" docbase = "/usr/local/tomcat/webapps/test" reloadable = "true"> <manager classname = "de. javakaffee. web. MSM. memcachedbackupsessionmanager "memcachednodes =" N1: 192.168.1.201: 11211, N2: 192.168.1.202: 11211 "# point to two memcached servers failovernodes =" N1 "requesturiignorepattern = ". *\. (ICO | PNG | GIF | JPG | CSS | JS) $ "transcoderfactoryclass =" de. javakaffee. web. MSM. serializer. javolution. javolutiontranscoderfactory "/> </context>
Provide the test folders and test files:
Tomcata:
# Mkdir-PV/usr/local/tomcat/webapplications/test/WEB-INF/{classes, lib}
Mkdir: Created directory '/usr/local/tomcat/webapps/Test'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF/classes'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF/lib'
# Vim/usr/local/tomcat/webapps/test/index. jsp
<% @ Page Language = "Java" %>
<HTML>
<Head> <title> tomcata </title> <Body>
<H1> <font color = "red"> tomcata.magedu.com <Table align = "center" 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>
Copy a web. xml file
# Cp/usr/local/tomcat/CONF/Web. xml/usr/local/tomcat/webapps/test/WEB-INF/
Tomcatb:
# Mkdir-PV/usr/local/tomcat/webapplications/test/WEB-INF/{classes, lib}
Mkdir: Created directory '/usr/local/tomcat/webapps/Test'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF/classes'
Mkdir: Created directory '/usr/local/tomcat/webapps/test/WEB-INF/lib'
# Vim/usr/local/tomcat/webapps/test/index. jsp
<% @ Page Language = "Java" %>
<HTML>
<Head> <title> tomcatb </title> <Body>
<H1> <font color = "blue"> tomcatb.magedu.com <Table align = "center" 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>
Copy a web. xml file
# Cp/usr/local/tomcat/CONF/Web. xml/usr/local/tomcat/webapps/test/WEB-INF/
Now we can start testing.
650) This. width = 650; "style =" float: none; "Title =" qq 40928225722.jpg "src =" http://s3.51cto.com/wyfs02/M00/4B/1E/wKiom1QoIgaRtLaDAAEMXKvryac127.jpg "alt =" wkiom1qoigartladaaemxkvryac127.jpg "/>
650) This. width = 650; "style =" float: none; "Title =" qq 40928225757.jpg "src =" http://s3.51cto.com/wyfs02/M01/4B/20/wKioL1QoIi-AVROCAAEYsE_VpHo417.jpg "alt =" wKioL1QoIi-AVROCAAEYsE_VpHo417.jpg "/>
OK, the sessionid is not changed, and the session is successfully maintained.
So here is the content of this section, old rules, what is not a good place to welcome criticism and correction, thank you!
This article from the Linux cainiao it Road, "blog, please be sure to keep this source http://wiggin.blog.51cto.com/8733640/1559177
Httpd + Tomcat + memcached for session persistence