首页 > 代码库 > nginx + tomcat + memcached
nginx + tomcat + memcached
user --> web --> nginx( 80 静态的 ) --> Tomcat ( 8080 *jsp 动态的 )
nginx的并发量很大,此架构的瓶颈是Tomcat,Tomcat来不及处理大并发量的,可以多个Tomcat来处理,nginx可以做附带均衡,让多个Tomcat来处理,可以解决大并发量的问题
nginx 可以对tomcat有健康检查,可以附带均衡
nginx + tomcat
/etc/init.d/mysqld stop
/etc/init.d/php-fpm stop
1.安装 nginx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.下载nginx包
2.解压 tar zfx nginx-1.6.1.tar.gz
3.修改文件
vim nginx-1.6.1/src/core/nginx.h
#define NGINX_VER "nginx/" ( 14行 版本号,外界人士可以看到,可以不写真的版本 )
vim nginx-1.6.1/auto/cc/gcc
#CFLAGS="$CFLAGS -g" ( 174行 把debuge关了,可以缩小所占的内存 )
4.编译 ./configure --prefix=/usr/local/nginx
5.安装 make && make install
6.修改配置文件
vi /usr/local/nginx/conf/nginx.conf
worker_processes 2; ( 3行 支持进程个数 )
events {
use epoll; ( 加速运行 ) ( 13行 )
worker_connections 1024;
}
7.连接 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
8.检测语法 nginx -t
9.开启 nginx
10.重启 nginx -s reload
2.安装 tomcat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(1)下载 lftp 192.168.2.251
sh /root/jdk-6u32-linux-x64.bin
mv /root/jdk1.6.0_32/ /usr/local/lnmp/jdk
(2)命令 vim /etc/profile
export JAVA_HOME=/usr/local/lnmp/jdk(zuihou)
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
(3)检测 echo $JAVA_HOME
/usr/local/lnmp/jdk
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:
/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin/:/usr/local/lnmp/jdk/bin
echo $CLASSPATH
.:/usr/local/lnmp/jdk/lib:/usr/local/lnmp/jdk/jre/lib
which java
/usr/local/lnmp/jdk/bin/java
which javac
/usr/local/lnmp/jdk/bin/javac
(4)java测试 vim /root/test.java
public class test {
public static void main(String[] args)
{
System.out.println("hello");
}
}
javac test.java ( 检测语法 )
java test ( 运行 )
hello ( 运行结果 )
(5)命令 vim .bashrc ( 当前用户起作用 )
内容:alias tomcat-start=‘/usr/local/tomcat/bin/startup.sh‘
alias tomcat-stop=‘/usr/local/tomcat/bin/shutdown.sh‘
alias tomcat-start=‘/usr/local/tomcat/bin/startup.sh‘ ( 临时有效 )
alias tomcat-stop=‘/usr/local/tomcat/bin/shutdown.sh‘
tomcat-stop ( 停止 tomcat )
tomcat-start ( 开启 tomcat )
3.整合nginx和tomcat
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(1)安装tomcat
解压 tar zfx apache-tomcat-7.0.37.tar.gz -C /usr/local/lnmp/
重命名 mv /usr/local/lnmp/apache-tomcat-7.0.37/ /usr/local/lnmp/tomcat ( mv 重命名 便于以后的更新,也可做连接 )
ln -s /usr/local/lnmp/apache-tomcat-7.0.37/ /usr/local/lnmp/tomcat ( mv 或 ln 一种即可 )
执行 /usr/local/lnmp/tomcat/bin/startup.sh ( 开启tomcat )
端口 netstat -antlp (8080)
(2)测试页面 vim /usr/local/lnmp/tomcat/webapps/ROOT/test.jsp
server220 The time is: <%=new java.util.Date()%> ( 动态的显示时间 )
(3)配置 vim /usr/local/lnmp/nginx/conf/nginx.conf
location / {
如果访问 192.168.2.220/index.jsp 没有出现 彩色的页面(有猫)就改如下的页面
root /usr/local/tomcat/webapps/ROOT; ( 45行 原来是html )
index index.jsp index.html index.htm;
}
location ~ \.jsp$ { (69)
proxy_pass http://127.0.0.1:8080;
}
(4)重启 nginx -s reload
(5)测试 192.168.2.220:8080
192.168.2.220/test.jsp
The time is: Sat Sep 20 11:50:51 CST 2014
4.附带均衡
需要2个测试机
*1.副机
(1)建目录 mkdir /usr/local/lnmp
(2)profiel vim /etc/profile
export JAVA_HOME=/usr/local/lnmp/jdk
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
(3)sh /usr/local/lnmp/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/lnmp/tomcat
Using CATALINA_HOME: /usr/local/lnmp/tomcat
Using CATALINA_TMPDIR: /usr/local/lnmp/tomcat/temp
Using JRE_HOME: /usr/local/lnmp/jdk
Using CLASSPATH: /usr/local/lnmp/tomcat/bin/bootstrap.jar:/usr/local/lnmp/tomcat/bin/tomcat-juli.jar
(4)端口 netstat -antlp (880)
(5)测试页面 vim /usr/local/lnmp/tomcat/webapps/ROOT/test.jsp
server105 The time is: <%=new java.util.Date()%>
*2.主机
(1)复制 scp -r /usr/local/lnmp/jdk/ 192.168.2.105:/usr/local/lnmp/
scp -r /usr/local/lnmp/tomcat/ 192.168.2.105:/usr/local/lnmp/
(2)配置 vim /usr/local/lnmp/nginx/conf/nginx.conf
upstream westos {(19)
server 192.168.2.105:8080;
server 192.168.2.220:8080;
}
location ~ \.jsp$ { (68)
proxy_pass http://westos;
}
(3)重启 nginx -s reload
(4)测试 192.168.2.220/test.jsp (变化)
5.同一个ip访问不做附带均衡
如果同一个ip有附带均衡,没有必要的,所以当一个同ip访问时,不做负载均衡
(1)下载 lftp i
(2)清除
cd nginx-1.6.0
make clean or make tidy ( 清除原来的编译,也可删除nginx-1.6.0 重新解压,并修改名字和debug )
(3)解压 tar zfx nginx-sticky-module-1.0.tar.gz -C nginx-1.6.0
(4)安装
cd nginx-1.6.0
./configure --prefix=/usr/local/lnmp/nginx/ --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-1.6.0/nginx-sticky-module-1.0/
nginx -s stop
make && make install
nginx
(5)配置 vim /usr/local/lnmp/nginx/conf/nginx.conf
upstream westos {
sticky; (20) ( 粘在一起 )
server 192.168.2.105:8080;
server 192.168.2.220:8080;
}
(6)检测 nginx -t
重启 nginx -s reload
(7)测试 192.168.2.220/test.jsp ( 刷新,页面不变,则代表成功 )
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4. memcached ( 存放tomcat的数据 ) 交叉存储
单点故障( 相互备份 )
1.主机
(1)安装memcached
yum install memcached -y
(2)所分配内存大小
free -m
vim /etc/sysconfig/memcached
CACHESIZE="30"
(3)启动服务 /etc/init.d/memcached start
(4)端口 netstat -antlp | grep 11211
2.副机
(1)安装memcached
yum install memcached -y
(2)所分配内存大小
free -m
vim /etc/sysconfig/memcached
CACHESIZE="30"
(3)启动服务 /etc/init.d/memcached start
(4)端口 netstat -antlp | grep 11211
3.测试是否可以存数据
如果没有telnet 需要安装 yum install -y telnet
telnet localhost 11211
(1)查看 stats
(2)添加 add user 0 0 6
westos
STORED
(3)获取 get user
VALUE user 0 6
westos
END
(4)设置 set user 0 0 6
redhat
STORED
(5)删除 delete user
DELETED
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nginx + tomcat + memcached
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nginx ( 静态访问 )
/ \
/ \
tomcat1 tomcat2 ( 动态访问 )
| \ / |
| \ / | ( 交叉存储 )
| / \ |
| / \ |
memcached1 memcached2 ( 数据存储 )
当t1挂了的时候,t2接管t1,继续把数据存放到m2,再有一个用户( 其他主机 )访问时,数据存放在m1中
当t2挂了的时候,t1接管t2,继续把数据存放到m1,再有一个用户( 其他主机 )访问时,数据存放在m2中
主机的时间要同步
node1:192.168.2.220
node2:192.168.2.105
node1 与 node2 配置基本相同,都需要做以下步骤
(1)tomcat停止 /usr/local/lnmp/tomcat/bin/shutdown.sh
(2)配置 tomcat
vi /usr/local/lnmp/tomcat/conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.2.220:11211,n2:192.168.2.105:11211"
failoverNodes="n1" //在 192.168.2.220 是 n2
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
在</Context>上添加内容
(3)下载jar
cd /usr/local/lnmp/tomcat/lib/
lftp i
mget asm-3.2.jar kryo-1.04.jar kryo-serializers-0.10.jar memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar minlog-1.2.jar msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar spymemcached-2.7.3.jar
rm -fr memcached-session-manager-tc6-1.6.3.jar
(4)测试页面 ( 从网上复制的 )
vi /usr/local/lnmp/tomcat/webapps/ROOT/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = http://www.mamicode.com/request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = http://www.mamicode.com/session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
(5)开启服务 /usr/local/lnmp/tomcat/bin/startup.sh
(6)查看日志 tail -f /usr/local/lnmp/tomcat/logs/catalina.out
INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2]
( 看到次行信息代表成功 )
(7)页面测试
1. 192.168.2.220/test.jsp (添加用户,数据信息存在 n2)
出现的 ID 2A5A98D9C32E8CB663EDE74F91C2D9D2-n2
可以用 telnet localhost 11211 进行测试
telnet localhost 11211
get A5A98D9C32E8CB663EDE74F91C2D9D2-n2
2. /usr/local/lnmp/tomcat/bin/shutdown.sh ( 停止tomcat )
nginx + tomcat + memcached