If you have not set up the nginx environment, please refer to my previous blog
Nginx Load Balancing
Lab Environment
Win7: tomcat6 + jdk6 + memcached
Linux: vmware9 + Ubuntu + jdk6 + nginx + tomcat6 + memcached
First, install memcached in windows.
Memcached-1.2.6-win32-bin.zip, 64-bit can also
Install memcached.exe-D install as a Windows Service
Memcached.exe-d start
Configure the memcached port, HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ memcached Server
ImagePath: "D: \ Java learning \ nginx Server Load balancer Configuration \ Windows Installation Package \ memcached.exe"-P 12345-M 128-D runservice
Among them, port 12345 is the memcached port, which must be used in Tomcat CONF/context .. XML
Configure CONF/context.. xml of Tomcat
The CONF/context. xml configuration methods for tomcat in Linux and Windows are the same <? XML version = '1. 0' encoding = 'utf-8'?> <Context> <watchedresource> WEB-INF/web. XML </watchedresource> <manager classname = "de. javakaffee. web. MSM. memcachedbackupsessionmanager "memcachednodes =" N1: 192.168.0.108: 12345 N2: 192.168.110.130: 12345 "requesturiignorepattern = ". */. (PNG | GIF | JPG | CSS | JS) $ "sessionbackupasync =" false "sessionbackuptimeout =" 100 "transcoderfactoryclass =" de. javakaffee. web. MSM. serializer. javolution. javolutiontranscoderfactory "copycollectionsforserialization =" false "/> </context>
Add five jar packages under Tomcat lib
Javolution-5.4.3.1.jar
Memcached-2.6.jar
Memcached-session-manager-1.5.1.jar
Memcached-session-manager-tc6-1.5.1.jar
Msm-javolution-serializer-1.5.1.jar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
Install memcached in Linux
Libevent-1.3.tar.gz
Memcached-1.2.2.tar.gz
Decompress the package using tar ~~
First install libevent
Go to the/usr/local/memcache Install files/libevent-1.3 unzipped libevent directory
# You can add parameters later. The installation path is installed in usr/local/lib by default if no parameter is added.
./Configure
# Compile
Make
# Installation
Make install
Check whether the installation is successful
Enter ls usr/local/bin/lib/*. So. If the following information is displayed, the installation is successful.
zhengx@zhengx-virtual-machine:/$ ls usr/local/lib/*.sousr/local/lib/libevent.so
Next, install memcached in Linux
Go to/usr/local/memcache Install files/libevent-1.3/memcached-1.2.2. Decompressed memcached directory
#-With-libevent =/usr/local/lib must be added; otherwise, the installation fails! /Usr/local/lib is the installation directory of libevent
./Configure-with-libevent =/usr/local/lib
Make
Make install
If an error cannot be created, run sudo chmod 777/usr/local-R.
How to start memcached
/Usr/local/bin/memcached-D-M 256-u root-l 192.168.1.5-P 12345-C 256-P/data/memcached. PID
Configure tomcat in Linux, the same as that in win7
Then http: // 192.168.110.130/T/in the browser will not change the session
Server IP Address: |
192.168.0.108 |
Session ID: |
AAB11F62F8E5631B35BD92F5230AD4D5-n1 |
Session Creation Time: |
Thu Jul 11 20:56:57 CST 2013 |
Last session time: |
Thu Jul 11 17:34:12 CST 2013 |
Session string: |
Org. Apache. Catalina. session. standardsessionfacade @ fe30af |
Refresh again. Check the session ID (session_id)
Server IP Address: |
192.168.110.130 |
Session ID: |
AAB11F62F8E5631B35BD92F5230AD4D5-n1 |
Session Creation Time: |
Thu Jul 11 20:56:57 CST 2013 |
Last session time: |
Thu Jul 11 20:56:59 CST 2013 |
Session string: |
Org. Apache. Catalina. session. standardsessionfacade @ 136e7f3 |
In session. setattribute (Key, value) in worker AT1 ). In tomcat2 session. getattribute (key); Test
Server IP Address: |
192.168.0.108 |
Session ID: |
7bf5b9dfcb6f35a49ba9fa9ef3813434-n1 |
|
|
|
|
|
|
Get value from session: |
I'm Tomcat on Windows. |
Note the server startup sequence: Start nginx first, and then tomcat1 and tomcat2.
Server IP Address: |
192.168.110.130 |
Session ID: |
7bf5b9dfcb6f35a49ba9fa9ef3813434-n1 |
Session Creation Time: |
Tue Jul 16 16:43:23 CST 2013 |
Last session time: |
Tue Jul 16 16:45:29 CST 2013 |
Session string: |
Org. Apache. Catalina. session. standardsessionfacade @ 1162cdf |
Get value from session: |
I'm Tomcat on Windows. |