memcached介绍:
Memcached是一个高性能的分布式内存对称缓存系统;通过缓存查询数据库结果,介绍数据库访问次数,以提高web应用的速度、提高扩展性。
Memcached特点:
协议简单
基于libevent的事件处理
内置内存存储方式
memcached不互相通信的分布式
一、使用环境准备
实验环境:
TomcatA(node2):172.16.2.13
TomcatB(node3):172.16.2.14
memcached(node1):172.16.2.12
memcached(node4): 172.16.2.15
Nginx(node5): 172.16.2.16
实验使用软件:
二、配置tomcat节点
TomcatA节点:
1、安装jdk
[root@node2 tomcat]# rpm -ivh jdk-7u67-linux-x64.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar..
[root@node2 tomcat]# cat /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$PATH/bin:$PATH [root@node2 tomcat]# source /etc/profile.d/java.sh
验证jdk安装
[root@node2 src]# java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
2、安装tomcat
[root@node2 tomcat]# tar xf apache-tomcat-7.0.55.tar.gz -C /usr/local [root@node2 tomcat]# cd /usr/local [root@node2 local]# ln -sv apache-tomcat-7.0.55/ tomcat `tomcat' -> `apache-tomcat-7.0.55/'
[root@node2 src]# cat /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH [root@node2 src]# source /etc/profile.d/tomcat.sh
验证tomcat安装
[root@node2 bin]# cat /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH [root@node2 bin]# source /etc/profile.d/tomcat.sh [root@node2 bin]# catalina.sh version 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 Server version: Apache Tomcat/7.0.55 Server built: Jul 18 2014 05:34:04 Server number: 7.0.55.0 OS Name: Linux OS Version: 2.6.32-504.el6.x86_64 Architecture: amd64 JVM Version: 1.7.0_67-b01 JVM Vendor: Oracle Corporation
node3安装与node2安装相同,这里就不在演示了
3、测试tomcat安装是否成功
启动taomcat [root@node2 ~]# catalina.sh start [root@node3 ~]# catalina.sh start
访问测试:
三、配置memcached节点
安装memcached
[root@node1 ~]# yum -y install memcached libmemcached
启动memcached
[root@node1 ~]# /etc/init.d/memcached start Starting memcached: [ OK ]
查看监听端口:默认为11211
[root@node1 ~]# ss -tlnp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:11211 *:*
node4节点配置与node1节点配置相同这里就不在演示了
四、配置tomcat节点支持memcached
TomcatA节点:
添加memcached的相关组件
[root@node2 tomcat]# cp memcached-session-manager-tc7-1.8.2.jar memcached-session-manager-1.8.2.jar javolution-5.5.1.jar msm-javolution-serializer-1.8.2.jar spymemcached-2.10.2.jar /usr/local/tomcat/lib/
添加一个容器:在默认的虚拟主机中添加
<Host name="localhost" appBase="webapps unpackWARs="true" autoDeploy="true"> <Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.16.2.12:11211,n2:172.16.2.15:11211" 定义memca节点 failoverNodes="n2" 故障转移主机,n2为上面的定义的主机 requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"当请求的内容匹配此处定义的时忽略 transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory "/> </Context> </Host>
创建对应的测试目录和默认文档
[root@node2 tomcat]# mkdir /usr/local/tomcat/webapps/test/{lib,classes,WEB-INF} 创建虚拟目录 [root@node2 tomcat]# vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <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>
重新启动tomcat
[root@node2 tomcat]# catalina.sh stop [root@node2 tomcat]# catalina.sh start
访问测试:
TomcatB节点与TomcatA节点配置相同,而index.jsp内容如下 [root@node3 tomcat]# cat /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <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>
访问node3节点测试:
五、配置Nginx节点
1、安装Nginx
[root@node5 ~]# yum -y install nginx
2、配置nginx负载tomcat
[root@node5 ~]# vim /etc/nginx/nginx.conf http { upstream tomcat { server 172.16.2.13:8080; server 172.16.2.14:8080; } } [root@node5 ~]# vim /etc/nginx/conf.d/default.conf location / { # root /usr/share/nginx/html; index index.jsp index.html index.htm; proxy_pass http://tomcat/; }
3、启动nginx
[root@node5 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@node5 ~]# /etc/init.d/nginx start Starting nginx: [ OK ]
4、访问测试:会发现只有标题会发生变化,而session id不会发生变化
六、模拟memcached故障,查看session有node4节点负责,但没有发生变化
1、模拟故障:停止node1的memcached
[root@node1 ~]# /etc/init.d/memcached stop Stopping memcached: [ OK ]
2、访问测试,
实验完成,大家在做的时候要是遇到问题可以一起解决,仅供跟人学习使用,请大神勿喷,谢谢。
原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/6746