nginx+tomcat +memcached

浪子不回头ぞ 提交于 2019-12-07 15:09:10

  <TI>           <T2>

       . \ / .

        . X .

       . / \ .

  <M1>          <M2>

Tomcat-1 (T1) 将 session 存储在 memcached-2 (T2)上。只有当 M2 不可用时,T1 才将 session

储在 memcached-1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩

溃时也不会丢失 session 会话,避免单点故障

我们需要准备两个节点(nginx1,nginx2)

首先配置 java 运行环境

参见 http://my.oschina.net/zhangxc73912/blog/207873

安装 apache-tomcat

[root@nginx1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local
[root@nginx1 ~]# mv /usr/local/apache-tomcat-7.0.37/ /usr/local/tomcat
[root@nginx1 ~]# cd /usr/local/
tomcat/bin/
[root@nginx1 bin]# ./startup.sh     //启动 tomcat ./shutdown.sh  关闭 tomcat

配置+测试

[root@nginx1 ~]# cd /usr/local/tomcat/webapps/ROOT
[root@nginx1 ROOT]# vi 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 = 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 = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

此测试也可显示 ip,可观测到服务器为那台,为后面做 memcache 做铺垫,同时也可以测出我

的安装的 tomcat 是否成功。

安装 nginx

yum install -y pcre-devel openssl-devel
[root@nginx1 nginx]# tar zxf nginx-1.4.2.tar.gz 
[root@nginx1 nginx]# cd nginx-1.4.2
[root@nginx1 nginx]#vi /mnt/nginx/nginx-1.4.2/auto/cc/gcc
#CFLAGS="$CFLAGS -g"
[root@nginx1 nginx-1.4.2]# ./configure 
[root@nginx1 nginx-1.4.2]# make && make install 
[root@nginx1 ~]# useradd nginx
[root@nginx1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

配置

[root@nginx1 ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
upstream tomcatcluster{
server 192.168.0.40:8080;
server 192.168.0.41:8080;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index test.jsp index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.jsp$ {
proxy_pass  http://tomcatcluster;
//凡是访问*.jsp 转至 8080 端口由 tomcat 处理
}
}
}

检测

[root@nginx1 ~]# nginx -t //检测配置 文件是否正确
[root@nginx1 ~]# curl -I localhost
HTTP/1.1 200 OK
Server: zhangxc-nginx/1.4.2
Date: Wed, 05 Mar 2014 05:00:23 GMT
Content-Type: text/html
[root@nginx1 ~]# nginx -s reload


可当我们刷新网页之后

解决方案,添加粘制位

所以我们需要重新加载 ngnx 模块

[root@nginx1 nginx]# tar zxf nginx-sticky-module-1.0.tar.gz 
[root@nginx1 nginx-1.4.2]#make clean
[root@nginx1 nginx-1.4.2]# ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module –addmodule=/mnt/nginx/nginx-sticky-module-1.0
[root@nginx1 nginx-1.4.2]#make && make install 
[root@nginx1 nginx-1.4.2]#vi /usr/local/nginx/conf/nginx
upstream tomcatcluster{
sticky; //其他和上面的文件内容相同
server 192.168.0.40:8080;
server 192.168.0.41:8080;
[root@nginx1 nginx-1.4.2]# nginx -s 
[root@nginx1 nginx-1.4.2]# nginx

然后就 ok 了一台 ,这样就一台服务器将会对于一个 ip 客户服务到底

于此同时也就出现一个问题,就是数据安全问题,虽然有服务器进行接替但是用户的数据丢失

了。

所以在此我们整合 memcached,解决这样的问题。

[root@nginx1 ~]# yum install -y memcached
[root@nginx1 ~]# /etc/init.d/memcached start
[root@nginx1 ~]# cd /usr/local/tomcat/lib/
添加如下包
asm-3.2.jar kryo-1.04.jar 
memcached-session-manager-tc6-1.6.3.jar reflectasm-1.01.jar
kryo-serializers-0.10.jar minlog-1.2.jar
memcached-session-manager-1.6.3.jar msm-kryo-serializer-1.6.3.jar
然后在编辑
/usr/loacl/tomcat/conf/context.xml
添加如下
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.0.40:11211,n2:192.168.0.41:11211"
failoverNodes="n1" // 另个节点为 n2
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>

这样就 ok 我们可以进行测试了

节点 2 和节点 1 的安装 tomcat memcached,其配置和节点 1 的相同。

推荐 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=11400711&id=4124279




易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!