redhat6.5 Tomcat+Nginx+Memcacheed集群部署

主机环境 redhat6.5 64位

实验环境 服务端1 ip172.25.29.1   nginx

服务端2 ip 172.25.29.2    tomcat+memcached

服务端3 ip 172.25.29.3    tomcat+memcaceed

安装包  jdk-7u79-linux-x64.tar.gz

apache-tomcat-7.0.37.tar.gz

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

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

防火墙状态关闭

1.jdk安装、环境配置及测试服务端2

1.解压、作软链接

[[email protected]]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/  #解压jdk

[[email protected]]# cd /usr/local/    #切换到解压目录

[[email protected]]# ls

bin  etc games  include  jdk1.7.0_79 lib  lib64  libexec sbin  share  src

[[email protected]]# ln -s jdk1.7.0_79/ java      #作软链接

[[email protected]]# ll

total44

drwxr-xr-x.2 root root 4096 Jun 28  2011 bin

drwxr-xr-x.2 root root 4096 Jun 28  2011 etc

drwxr-xr-x.2 root root 4096 Jun 28  2011 games

drwxr-xr-x.2 root root 4096 Jun 28  2011 include

lrwxrwxrwx.1 root root   12 Sep 24 10:50 java ->jdk1.7.0_79/    #查看

drwxr-xr-x.8 uucp  143 4096 Apr 11  2015 jdk1.7.0_79

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib64

drwxr-xr-x.2 root root 4096 Jun 28  2011 libexec

drwxr-xr-x.2 root root 4096 Jun 28  2011 sbin

drwxr-xr-x.5 root root 4096 Aug  8 21:38 share

drwxr-xr-x.2 root root 4096 Jun 28  2011 src

2.将jdk添加到环境变量

[[email protected]]# vim /etc/profile

export JAVA_HOME=/usr/local/java

exportCLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$PATH:$JAVA_HOME/bin

[[email protected]]# source /etc/profile

3.测试

[[email protected]]# echo $JAVA_HOME    #测试添加环境变量是否成功

/usr/local/java

[[email protected]]# echo $CLASSPATH

.:/usr/local/java/lib:/usr/local/java/jre/lib

[[email protected]]# echo $JAVA_HOME

/usr/local/java

[[email protected]]# cd java       #测试java的环境是否配置成功

[[email protected]]# vim test.java    #写个简单的测试页

public class test{

public static void main(String[] args)

{

System.out.println("Helloworld!");

}

}

[[email protected]]# javac test.java    #链接生成test.class文件

[[email protected]]# java test    #执行

Helloworld!

2.tomcat的安装服务端2

1.解压、作软链接

[[email protected]]# tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local/  #解压

[[email protected]]# cd /usr/local/

[[email protected]]# ls

apache-tomcat-7.0.8  etc   include  jdk1.7.0_79  lib64   sbin   src

bin                  games  java    lib          libexec  share

[[email protected]]# ln -s apache-tomcat-7.0.8/ tomcat   #作软链接

[[email protected]]# ll

total48

drwxr-xr-x.9 root root 4096 Sep 24 11:11 apache-tomcat-7.0.8

drwxr-xr-x.2 root root 4096 Jun 28  2011 bin

drwxr-xr-x.2 root root 4096 Jun 28  2011 etc

drwxr-xr-x.2 root root 4096 Jun 28  2011 games

drwxr-xr-x.2 root root 4096 Jun 28  2011 include

lrwxrwxrwx.1 root root   12 Sep 24 10:50 java ->jdk1.7.0_79/

drwxr-xr-x.8 uucp  143 4096 Sep 24 11:02 jdk1.7.0_79

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib64

drwxr-xr-x.2 root root 4096 Jun 28  2011 libexec

drwxr-xr-x.2 root root 4096 Jun 28  2011 sbin

drwxr-xr-x.5 root root 4096 Aug  8 21:38 share

drwxr-xr-x.2 root root 4096 Jun 28  2011 src

lrwxrwxrwx.1 root root   20 Sep 24 11:12 tomcat-> apache-tomcat-7.0.8/     #查看

[[email protected]]# cd tomcat/bin

[[email protected]]# ./startup.sh     #开启tomcat

2.测试

[[email protected]]#cd ..

[[email protected]]# vim webapps/ROOT/test.jsp    #写测试仪页

1 server2-The Time is <%=newjava.util.Date()%>

#测试 172.25.29.2:8080

172.25.29.2:8080/test.jsp

在服务端3上进行同样的配置也可以用scp把服务端2上java和tomcat目录传过去如下

[[email protected]]# scp -r java/ tomcat/ 172.25.29.3:/usr/local/

在进行系统环境里加上java的配置启动tomcat即可

3.Nginx添加sticky  (服务端1)

1.nginx负载均衡已经配置好了

在前面的博客里已经写过nginx源码安装这里就不再重复了也可以参考前面的博客

[[email protected]~]# cd /usr/local/lnmp/nginx/conf

[[email protected]]# vim nginx.conf

upstream wen {

server 172.25.29.2:8080;    #轮询机制

server 172.25.29.3:8080;

}

location / {

root   html;

index  index.html index.jspindex.php index.htm;   #默认发布目录

}

location ~ \.jsp$ {

proxy_pass   http://wen;

}

[[email protected]]# nginx -t    #检测

nginx:the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[[email protected]]# nginx -s reload    #刷新

测试 172.25.29.1

172.25.29.1/test.jsp

刷新之后

也就是server2 和server3相互交替

2.改变负载均衡机制为sticky

由于nginx是轮询机制如果在访问页面如填写信息时突然卡住刷新之后就会跳到另一个server上就得重新开始填写。但是Nginx里有很多算法其中的ip_hash也可以防止这些问题。使用ip_hash有一个问题是当客户端和服务器之间使用cdn内容分发系统高速缓存时客户端的访问到达cdn由cdn访问服务器识别的ip是cdn的ip那么将集中于访问后台的一台服务器(x相当于DDOS攻击)会加速服务器的损坏。那么为了防止这些问题就有用下面的算法sticky不是nginx里自带的nginx-sticky-module为 nginx 的第三方模块,使 nginx 支持 sticky 模式,所以需要将包加入配置、重新编译、安装nginx

1.重新源码安装nginx添加一个模块

[[email protected]]# nginx -s stop    #关闭nginx

[[email protected]]# tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz #解压

[[email protected]]# ls

nginx-1.8.1.tar.gz

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

[[email protected]]# make clean    #清除上一次的缓存文件

rm-rf Makefile objs

[[email protected]]# ./configure --prefix=/usr/local/lnmp/nginx   #重新配置--with-http_ssl_module--with-http_stub_status_module--add-module=/mnt/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

[[email protected]]# make     #编译、链接

[[email protected]]# make install   #安装

[[email protected]]#cd /usr/local/lnmp/nginx/conf

[[email protected]]# vim nginx.conf

upstream westos{

sticky;     #使用sticky

server 172.25.29.2:8080;

server 172.25.29.3:8080;

[[email protected]]# nginx -t   #检测nginx文件里是否有错误

nginx:the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[[email protected]]# nginx   #启动

2.测试

测试 172.25.29.1/test.jsp

刷新之后结果不变还是

4交叉存储避免单点故障添加memcached服务端2

配置好之后如果一台服务器宕机了那么这台服务器正在运行的业务将直接结束正在存储的数据丢失。为了防止这些问题Tomcat1将session存储到Tomcat2的memcached中当Tomcat2的memcached不可用时Tomcat1将session存储到自己的memcached上Tomcat2则正好相反。使用这种配置就避免了单点故障。

1.安装、开启memcached、写测试页

[[email protected]]# yum install -y memcached      #安装memcached

[[email protected]]# /etc/init.d/memcached start      #开启memcached

Startingmemcached:                                        [  OK  ]

[[email protected]]# bin/shutdown.sh   #关闭Tomcat

[[email protected]]# vim test.jsp    #写一个jsp的测试页面

<%@ page contentType="text/html;charset=GBK" %>

<%@ page import="java.util.*"%>

<html><head><title>Cluster AppTest</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>Sessionlist</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=20name="dataName">

<br>

key:<input type=text size=20name="dataValue">

<br>

<input type=submit>

</form>

</body>

</html>

[[email protected]]# cd /usr/local/tomcat/lib

[[email protected]]# ls /mnt/update/      #先前下载好的包

asm-3.2.jar                              minlog-1.2.jar

kryo-1.04.jar                           msm-kryo-serializer-1.6.3.jar

kryo-serializers-0.10.jar                reflectasm-1.01.jar

memcached-session-manager-1.6.3.jar      spymemcached-2.7.3.jar

memcached-session-manager-tc7-1.6.3.jar

[[email protected]]# cd /usr/local/tomcat/lib/

[[email protected]]# mv /mnt/update/* .    #将包移动到指定路径

[[email protected]]# ls

annotations-api.jar                 memcached-session-manager-tc7-1.6.3.jar

asm-3.2.jar                          minlog-1.2.jar

catalina-ant.jar                    msm-kryo-serializer-1.6.3.jar

catalina-ha.jar                      reflectasm-1.01.jar

catalina.jar                         servlet-api.jar

catalina-tribes.jar                  spymemcached-2.7.3.jar

ecj-4.2.1.jar                        tomcat-api.jar

el-api.jar                           tomcat-coyote.jar

jasper-el.jar                        tomcat-dbcp.jar

jasper.jar                           tomcat-i18n-es.jar

jsp-api.jar                          tomcat-i18n-fr.jar

kryo-1.04.jar                        tomcat-i18n-ja.jar

kryo-serializers-0.10.jar            tomcat-jdbc.jar

memcached-session-manager-1.6.3.jar  tomcat-util.jar

[[email protected]]# cd ..

UsingCATALINA_BASE:   /usr/local/tomcat

UsingCATALINA_HOME:   /usr/local/tomcat

UsingCATALINA_TMPDIR: /usr/local/tomcat/temp

UsingJRE_HOME:        /usr/local/java

UsingCLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[[email protected]]# vim conf/context.xml    #添加节点及节点失效后该怎么存储

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:172.25.29.2:11211,n2:172.25.29.3:11211"     #结点

failoverNodes="n1"           #当n2失效时存储到n1上

requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderF    actory"

/>

[[email protected]]# bin/startup.sh   #开启Tomcat

服务端3和服务端2的配置基本相同唯一不同的是上面提到的/usr/local/tomcat/conf/context.xml里的36行把“n1”改成“n2”可以用scp传过去再做修改。

2.测试

测试 172.25.29.1/test.jsp

添加用户

添加完成

添加3个用户

[[email protected]]# tail -f logs/catalina.out      #查看日志

Sep28, 2016 11:39:53 PM de.javakaffee.web.msm.serializer.kryo.KryoTranscoder<init>

INFO:Starting with initialBufferSize 102400 and maxBufferSize 2048000

Sep28, 2016 11:39:53 PM de.javakaffee.web.msm.MemcachedSessionServicestartInternal

INFO:MemcachedSessionService finished initialization, sticky true, operation timeout1000, with node ids [n2] and failover node ids [n1]

Sep28, 2016 11:39:53 PM org.apache.coyote.AbstractProtocol start

INFO:Starting ProtocolHandler ["http-bio-8080"]

Sep28, 2016 11:39:53 PM org.apache.coyote.AbstractProtocol start

INFO:Starting ProtocolHandler ["ajp-bio-8009"]

Sep28, 2016 11:39:53 PM org.apache.catalina.startup.Catalina start

INFO:Server startup in 1921 ms

user1= 111

user2= 222

user1= 111           #上面创建的用户

user2= 222

user1= 111

user3= 333

^C

[[email protected]]# telnet 172.25.29.3 11211  #远程登陆服务端3查看是否是否写进入11211是memcached的端口号

Trying172.25.29.3...

Connectedto 172.25.29.3.

Escapecharacter is ‘^]‘.

get50B9BAB1CBB21C9BF884CC3613560752-n2   #get后面的是上面截图里的ID

VALUE50B9BAB1CBB21C9BF884CC3613560752-n2 2048 125

[1]WWqt2Wqt01WqtWqt#50B9BAB1CBB21C9BF884CC3613560752-n2

user2

222user1

111user3

333       #大概可以看出来添加的用户

END

quit

Connectionclosed by foreign host.

[[email protected]]# bin/shutdown.sh  #关闭Tomcat1

UsingCATALINA_BASE:   /usr/local/tomcat

UsingCATALINA_HOME:   /usr/local/tomcat

UsingCATALINA_TMPDIR: /usr/local/tomcat/temp

UsingJRE_HOME:        /usr/local/java

UsingCLASSPATH:      /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

当关闭Tomcat1续输入数据时并没有中断而是跳到Tomcat2上且数据并未丢失

[[email protected]]# telnet 172.25.29.3 11211     #远程登陆服务端3

Trying172.25.29.3...

Connectedto 172.25.29.3.

Escapecharacter is ‘^]‘.

get50B9BAB1CBB21C9BF884CC3613560752-n2

VALUE50B9BAB1CBB21C9BF884CC3613560752-n2 2048 137

[1]WWqt2Wqu01WquWqu4#50B9BAB1CBB21C9BF884CC3613560752-n2



user2  222user1  111user4  444user3  333   #大概可以看到数据还是写入服务端3

END

quit

Connectionclosed by foreign host.

时间: 2024-08-29 11:09:04

redhat6.5 Tomcat+Nginx+Memcacheed集群部署的相关文章

Tomcat+Nginx+Memcacheed集群部署

主机环境 redhat6.5 64位 实验环境 服务端1 ip172.25.29.1   nginx 服务端2 ip 172.25.29.2    tomcat+memcached 服务端3 ip 172.25.29.3    tomcat+memcaceed 安装包  jdk-7u79-linux-x64.tar.gz apache-tomcat-7.0.37.tar.gz nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz asm

tomcat+nginx+redis集群试验

Nginx负载平衡 + Tomcat + 会话存储Redis配置要点 使用Nginx作为Tomcat的负载平衡器,Tomcat的会话Session数据存储在Redis,能够实现0当机的7x24 运营效果.因为将会话存储在Redis中,因此Nginx就不必配置成stick粘粘某个Tomcat方式,这样才能真正实现后台多个Tomcat负载平 衡,用户请求能够发往任何一个tomcat主机,当我们需要部署新应用代码时,只要停止任何一台tomcat,所有当前在线用户都会导向到运行中的 tomcat实例,因

Nginx+Tomcat+Memcached 实现集群部署时Session共享

一.简介 我们系统经常要保存用户登录信息,有Cookie和Session机制,Cookie客户端保存用户信息,Session在服务端保存用户信息,如果浏览器不支持Cookie或者用户把Cookie禁掉了,Cookie就用不了,还有不同的浏览器采用不用方式保存Cookie,所以我们采用Session服务端来保存,上一节我们有介绍了Tomcat集群部署,怎么样集群的Tomcat对同个用户请求的都能获取保存在Session的用户信息,采用了Memcached管理Session,Memcached 是一

nginx+keepalived集群部署

原文地址:http://blog.51cto.com/1054054/2306779

Nginx+Tomcat 集群部署

1.Nginx + Tomcat 集群部署 简单配置 1 #user nobody; 2 worker_processes 4;#工作进程的个数 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; #单个进程连

Nginx+Tomcat集群部署

为了获取更好的性能,我们常常需要将tomcat进行集群部署.下文通过nginx转发实现tomcat集群,并通过nginx-upstream-jvm-route插件保证session的粘滞. 应用场景环境: server1 服务器上安装了 nginx + tomcat01 server2 服务器上只安装了 tomcat02 server1 IP 地址: 192.168.1.88 server2 IP 地址: 192.168.1.89 安装步骤: 1)在server1 上安装配置 nginx + n

Nginx + Tomcat 反向代理 负载均衡 集群 部署指南

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/53535435 Nginx是一种服务器软件,也是一种高性能的http和反向代理服务器,同时还是一个代理邮件服务器.也就是说,我们在Nginx上可以发布网站,可以实现负载均衡(提高应答效率,避免服务器崩溃),还可以作为邮件服务器实现收发邮件等功能.而最常见的就是使用Nginx实现负载均衡. Nginx与其他服务器的性能比较: Tomcat服务器面向Java语言,是重量级的服务器,而N

Web集群部署(Nginx+Keepalived+Varnish+LAMP+NFS)

Web集群部署(Nginx+Keepalived+Varnish+LAMP+NFS)  一.服务介绍   1.1 Nginx服务 Nginx是一个高性能的HTTP和反向代理服务器,也是一个支持IMAP/POP3/SMTP的代理服务器.Nginx即支持Web服务正向代理,也支持反向代理,尤其是反向代理功能十分强大.Nginx支持缓存功能,负载均衡,FASTCGI协议,支持第三方模块.时下Nginx的Web反向代理功能非常流行.   1.2 Keepalived     Keepalived见名知意

使用Tomcat+Redis来实现集群部署中的Session共享问题

一.工作中因为要使用到Tomcat集群部署,此时就涉及到了Session共享问题,主要有三种解决方案: 1.使用数据库来存储Session 2.使用Cookie来存储Session 3.使用Redis来存储Sesssion 二.本文中主要讲一下第3种方案,也就是使用Redis来存储Session,Github中已经有该开源组件(tomcat-redis-session-manager),下面讲一下配置的步骤 1.配置tomcat配置文件context.xml <Valve className=&qu