- 准备好tomcat
第一台
tar vxf apache-tomcat-7.0.54.tar.gz
mv apache-tomcat-7.0.54 /usr/local/tomcat
tar vxf jdk-7u65-linux-x64.tar.gz
mv jdk1.7.0_65/ /usr/local/java
编辑vim /etc/profile加
export JAVA_HOME=/usr/local/java #设置java根目录
export PATH=$PATH:$JAVA_HOME/bin #在PATH环境变量中添加java跟目录的bin子目录
启动tomcat服务
/usr/local/tomcat/bin/startup.sh
打开浏览器,ip:8080显示tomcat主页
关闭服务;
/usr/local/tomcat/bin/shutdown.sh
建立一个web 站点
cd webapps/
mkdir web
vim index.jsp
Server Info:
SessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
out.println("server one");
%>
修改Tomcat的server.xml文件
cp server.xml server.xml.bak
vim server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="web" path="" reloadable="flase">
</Context>
</Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
打开浏览器,ip:8080显示tomcat主页
第二台
与第一台基本一致,更改测试页的ip地址
scp root@172.16.1.2:/usr/local/tomcat/webapps/web/index.jsp .
scp root@172.16.1.2:/usr/local/tomcat/conf/server.xml .
启动服务/usr/local/tomcat/bin/startup.sh
查看服务
netstat -anpult |grep java
浏览器访问测试 http://ip:8080
显示正常
2.准备nginx
第一台
源码安装nginx,
修改配置文件,配置负载均衡
cp /usr/local/nginx/conf/nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.conf
加upstream tomcat_server {
server 172.16.1.12:8080 weight=1;
server 172.16.1.2:8080 weight=1;
}
proxy_pass http://tomcat_server;
语法检查
/usr/local/nginx/sbin/nginx –t
启动服务/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
或者killadd -1 nginx
打开浏览器访问: http://ip ,该ip为nginx 的ip,刷新浏览器,会显示两台
第二台
nginx-2服务器配置:
配置方式与服务器nginx-1一致
客户端测试:
打开浏览器访问: http://ip #不断刷新可看到由于权重相同,页面会反复切换
3.安装配置keepalived
yum -y install keepalived
修改配置文件vim keepalived.conf
配置一个实例名字为VI_2,状态作为主,优先级100
vrrp_instance VI_2 {
#备用服务器上为 BACKUP
state MASTER
#绑定vip的网卡为ens33
interface ens33
virtual_router_id 51
#备用服务器上为90
nopreempt #为不抢占模式
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
#track_script {
#nginx
#}
virtual_ipaddress { #配置虚拟ip地址
172.16.1.200
}
}
vrrp_instance VI_1 { #配置另一个实例 VI_1,状态为 从,优先级为90
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #配置虚拟ip地址为254
192.168.200.254
}
}
配置第二台
Vim keepalive.conf
vrrp_instance VI_2 { #实例名字为VI_2,状态作为从,优先级90
#备用服务器上为 BACKUP
state SLAVE
#绑定vip的网卡为ens33
interface ens33
virtual_router_id 51
#备用服务器上为90
nopreempt
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.1.200 #配置虚拟ip地址
}
}
vrrp_instance VI_1 { #配置另一个实例 VI_1,状态为主,优先级为100
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.254
}
}
测试,关闭一台keepalived,打开浏览器,刷新页面,两台机器网页交替更新,测试正常
实现高可用。
4.安装memcached,实现session会话保持。
yum -y install gcc openssl-devel pcre-devel zlib-devel
先安tar xf libevent-2.0.15-stable.tar.gz
cd libevent-2.0.15-stable
./configure --prefix=/usr/local/libevent && make && make install
再装memcached
tar xf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/ && make && make install
配置
/usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/libevent/lib/
Ldconfig
/usr/local/memcached/bin/memcached -u root -m 512M -n 10 -f 2 -d -vvv -c 512
netstat -antp| grep :11211
显示端口开启
安装telnet测试yum -y install telnet
telnet 192.168.200.103 11211
显示正常
最后执行让Tomcat-1 Tomcat-2 通过(msm)连接到Memcached
将session包中的“*.jar复制到/usr/local/tomcat/lib/ 下面
cp session/* /usr/local/tomcat/lib/使tomcat支持memcached,
tomcat-1 和 tomcat-2 配置文件一模一样.
vim /usr/local/tomcat7/conf/context.xml
重启服务tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
如果成功,tomcat与Memcached 端口会连在一起,前后有变化
Tomcat-1与Tomcat-2如下图
netstat -antp|grep java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 62496/java
tcp 0 0 :::8009 :::* LISTEN 62496/java
tcp 0 0 :::8080 :::* LISTEN 62496/java
tcp 0 0 ::ffff:192.168.200.10:28232 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28231 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28230 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28228 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
tcp 0 0 ::ffff:192.168.200.10:28229 ::ffff:192.168.200.10:11211 ESTABLISHED 62496/java
[root@tomcat-1 ~]# netstat -antp|grep memcached
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28230 ESTABLISHED 62402/memcached
tcp 45 0 192.168.200.103:11211 192.168.200.103:28228 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28232 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28229 ESTABLISHED 62402/memcached
tcp 0 0 192.168.200.103:11211 192.168.200.103:28231 ESTABLISHED 62402/memcached
tcp 0 0 :::11211 :::* LISTEN 62402/memcached