测试环境:
nginx-one:192.168.0.110 (用于负载均衡测试)
tomcat-one+memcached:192.168.0.112
tomcat-two+memcached:192.168.0.114
软件版本:
Linux:Centos6.5
nginx-1.8.1.tar.gz
orcle:jdk-7u67-linux-x64.rpm
tomcat:apache-tomcat-7.0.57.tar.gz
libevent-2.0.22-stable.tar.gz(memcached需要基于libevent使用)
memcached:memcached-1.4.29.tar.gz
生产环境建议完全使用编译安装:
首先安装开发支持插件,不要管直接都安装上:
yum -y groupinstall "development tools" "server platform development"
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc++ readline-devel
首先安装nginx:
[root@vjidian-nginx-one src]# tar xf nginx-1.8.1.tar.gz
[root@vjidian-nginx-one src]# cd nginx-1.8.1
[root@vjidian-nginx-one nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
以上无报错,继续安装:
[root@vjidian-nginx-one nginx-1.8.1]# make && make install
编辑nginx配置文件,做负载均衡:
[root@vjidian-nginx-one ~]# vim /etc/nginx/nginx.conf
由于测试使用,简单写了一下,需要注意的地方已经标记出来:
然后保存退出,检查一下语法:
[root@vjidian-nginx-one ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx安装配置到这里就目前结束。
tomcat安装及配置:
oracle jdk版本:
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
首先安装jdk,使用rpm包简单安装:
[root@vjidian-tomcat-one src]# rpm -ivh jdk-7u67-linux-x64.rpm
然后验证下,能看到java目录说明安装成功:
[root@vjidian-tomcat-one src]# ls /usr/
bin etc games include java lib lib64 libexec local sbin share src tmp
编辑变量环境:
[root@vjidian-tomcat-one src]# vim /etc/profile.d/java.sh
添加下面内容:
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
重读生效:
[root@vjidian-tomcat-one src]# . /etc/profile.d/java.sh
看一下java版本:
[root@vjidian-tomcat-one src]# java -version
java version "1.7.0_67"
下面开始编译安装tomcat:
[root@vjidian-tomcat-one src]# tar xfv apache-tomcat-7.0.70.tar.gz -C /usr/local/
最好做个软链接:
[root@vjidian-tomcat-one src]# cd /usr/local/
[root@vjidian-tomcat-one local]# ln -s apache-tomcat-7.0.57/ tomcat
编写tomcat变量环境:
[root@vjidian-tomcat-one local]# vim /etc/profile.d/tomcat.sh
添加以下内容:
CATALINA_BASE=/usr/local/tomcat
PATH=$CATALINA_BASE/bin:$PATH
export CATALINA_BASE PATH
重读:
[root@vjidian-tomcat-one local]# . /etc/profile.d/tomcat.sh
以上没问题,跑个测试:
[root@vjidian-tomcat-one local]# catalina.sh configtest
然后启动(生产环境,请勿用root启动):
[root@vjidian-tomcat-one local]# catalina.sh start
以上tomcat已经编译安装完成,第二台192.168.0.114的tomcat安装照上。
下面开始重点memcached:
先安装libevent
libevent官网:http://libevent.org/
版本: libevent-2.0.22-stable.tar.gz
#查看是否已安装:
rpm qa | grep libevent
#如果已安装且版本低于1.3,直接卸载;
rpm -e libevent --nodeps
正式编译安装libevent:
[root@vjidian-tomcat-one src]# tar zxvf libevent-2.0.21-stable.tar.gz
[root@vjidian-tomcat-one src]# cd libevent-2.0.21-stable
指定安装目录:
[root@vjidian-tomcat-one src]# ./configure --prefix=/usr/local/libevent
安装:
[root@vjidian-tomcat-one src]# make && make install
然后安装memcached:
[root@vjidian-tomcat-one src]# tar zxvf memcached-1.4.20.tar.gz
指定安装目录,并且关联libevent:
[root@vjidian-tomcat-one src]# cd memcached-1.4.20
[root@vjidian-tomcat-one src]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
然后安装:
[root@vjidian-tomcat-one src]# make && make install
创建memcached用户
[root@vjidian-tomcat-one ~]# useradd -r -s /sbin/nologin memcached
到此memcached安装完成,启动memcached:
[root@vjidian-tomcat-one ~]# /usr/local/bin/memcached -d -m 1024 -u memcached -l 192.168.0.114 -p 11211 -c 1024 -P /tmp/memcached.pid
启动写的比较复杂,为了下面的解释方便:
Memcached一些常用的命令选项:
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
然后使用ss命令查看memcached端口(没指定-l 192.xxx.xxx.xxx,所以就是全局11211):
[root@vjidian-tomcat-one ~]# ss -tnl|grep 11211
LISTEN 0 128 :::11211 :::*
LISTEN 0 128 *:11211 *:*
可以查看memcached的状态信息:
[root@vjidian-tomcat-one ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats #输入stats会显示很多信息,这里就不再显示
退出使用 quit
停止memcached服务进程:
ps -ef | grep memcached
kill - 9 XXXX(进程号)
到这里memcached和libevent已经全部安装完毕:
tomcat+memcached会话保持:
首先需要找到tomcat和memcached支持的动态库:
据说官方动态库会话保持只有1分钟,这边有编写的保持60分钟,需要的小伙伴可以@我:
需要注意:本测试使用tomcat7,所以动态库支持7,其他版本自行找相关动态库
把上面的动态库全部放入/usr/local/tomcat/lib 目录中
然后编写tomcat配置:
[root@vjidian-tomcat-one ~]# vim /usr/local/tomcat/conf/server.xml
插入编写内容:
<Context path="" docBase="/usr/local/tomcat/webapps/ROOT">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.0.114:11211 n2:192.168.0.112:11211"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|jpeg|css|js)$"
sessionBackupAsync="false"
memcachedProtocol="binary"
sticky="false"
lockingMode="auto"
copyCollectionsForSerialization="false"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
</Context>
非常需要注意docBase="/usr/local/tomcat/webapps/ROOT">
为web的路径,一定要写全。(别问怎么强调这个,泪崩)
memcachedNodes="n1:192.168.0.114:11211 n2:192.168.0.112:11211"
改为你的ip和端口就可以了。
第二台tomcat机器同上设置:
编写测试session ID:
tomcat A的测试编写如下:
[root@vjidian-tomcat-one ~]# vim /usr/local/tomcat/webapps/ROOT/index.jsp
写入以下内容:
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.test.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("test.com","test.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
tomcat B的测试编写:
[root@vjidian-tomcat-two ~]# vim /usr/local/tomcat/webapps/ROOT/index.jsp
写入一下内容:
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.test.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("test.com","test.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
到此配置全部结束,开始测试,先启动memcached,再启动tomcat,再启动nginx:
然后测试一下:
两个session ID保持一致,怎么刷都不会变。搞定!!!!