一.在server上安装jdk和apache-tomcat
1.解压到指定目录/usr/local/
[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-7.0.37 tomcat
[root@server1 local]# ln -s jdk1.7.0_79 java
[root@server1 local]# ls
apache-tomcat-7.0.37 games jdk1.7.0_79 libexec sbin tomcat
bin include lib lnmp share
etc java lib64 openresty src
2.配置java的环境变量
[root@server1 local]# vim /etc/profile (写在文件最后)
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
[root@server1 local]# source /etc/profile 保存
3.检测jdk环境是否正确:
[root@server1 local]# vim test.java
public class test {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
[root@server1 ~]# javac test.java #javac编译.java脚本
[root@server1 ~]# java test # 运行脚本
Hello World! # 运行结果
4.进入tomcat的发布目录,测试tomcat是否安装正确
[root@server1 ~]# cd /usr/local/tomcat/
[root@server1 tomcat]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
[root@server1 tomcat]# cd bin/
[root@server1 bin]# ./startup.sh 开启tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server1 bin]# netstat -tnlp 查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 6840/memcached
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 889/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15405/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1014/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1090/master
tcp 0 0 0.0.0.0:41243 0.0.0.0:* LISTEN 907/rpc.statd
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 15381/php-fpm
tcp 0 0 :::11211 :::* LISTEN 6840/memcached
tcp 0 0 :::111 :::* LISTEN 889/rpcbind
tcp 0 0 :::8080 :::* LISTEN 15567/java
tcp 0 0 :::56820 :::* LISTEN 907/rpc.statd
tcp 0 0 :::22 :::* LISTEN 1014/sshd
tcp 0 0 ::1:25 :::* LISTEN 1090/master
tcp 0 0 :::8009 :::* LISTEN 15567/java
tcp 0 0 :::3306 :::* LISTEN 1392/mysqld
[root@server1 conf]# cd ../sbin/
[root@server1 sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@server1 sbin]# ./nginx -s reload 重新加载nginx
[root@server1 sbin]# cd ..
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html example.php index.html index.php
[root@server1 html]# vim index.jsp
the time is: <%=new java.util.Date() %>
二、对inginx做相应的配置实现server1和server2的tomcat负载均衡:
vim /usr/local/lnmp/nginx/conf/nginx.conf
[root@server1 sbin]# vim /usr/local/openresty/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream tomcat {
server 172.25.254.1:8080; 轮询
server 172.25.254.2:8080;
}
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
#location /memc {
# internal;
# memc_connect_timeout 100ms;
# memc_send_timeout 100ms;
# memc_read_timeout 100ms;
# set $memc_key $query_string;
# set $memc_exptime 300;
# memc_pass memcache;
#}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# set $key $uri$args;
# srcache_fetch GET /memc $key;
# srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
加载nginx
[root@server1 sbin]# ./nginx -s reload
在tomcat发布目录下写发布文件
[root@server1 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# ls
asf-logo.png bg-nav.png RELEASE-NOTES.txt tomcat.svg
asf-logo-wide.gif bg-upper.png tomcat.css WEB-INF
bg-button.png build.xml tomcat.gif
bg-middle.png favicon.ico tomcat.png
bg-nav-item.png index.jsp tomcat-power.gif
[root@server1 ROOT]# vim test.jsp
the time is: <%=new java.util.Date() %>
配置server2
首先将server1下的tomcat和java拷贝到/usr/local
[root@server2 ~]# scp -r root@server1:/usr/local/tomcat /usr/local/
[root@server2 ~]# scp -r root@server1:/usr/local/java /usr/local/
编辑 /etc/profile
[root@server2 local]# vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
[root@server2 local]# source /etc/profile
开启tomcat并查看端口
[root@server2 local]# cd tomcat/
[root@server2 tomcat]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
[root@server2 tomcat]# cd bin/
[root@server2 bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 bin]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 863/rpcbind
tcp 0 0 0.0.0.0:35893 0.0.0.0:* LISTEN 881/rpc.statd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 987/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1065/master
tcp 0 0 :::111 :::* LISTEN 863/rpcbind
tcp 0 0 :::8080 :::* LISTEN 1233/java
tcp 0 0 :::22 :::* LISTEN 987/sshd
tcp 0 0 :::46102 :::* LISTEN 881/rpc.statd
tcp 0 0 ::1:25 :::* LISTEN 1065/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1233/java
tcp 0 0 :::8009 :::* LISTEN 1233/java
server1将test.jsp文件发送到server2的tomcat发布目录下
[root@server1 ROOT]# scp test.jsp root@server2:/usr/local/tomcat/webapps/ROOT/
server2查看并修改:
[root@server2 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server2 ROOT]# ls
asf-logo.png bg-nav.png RELEASE-NOTES.txt tomcat-power.gif
asf-logo-wide.gif bg-upper.png test.jsp tomcat.svg
bg-button.png build.xml tomcat.css WEB-INF
bg-middle.png favicon.ico tomcat.gif
bg-nav-item.png index.jsp tomcat.png
[root@server2 ROOT]# cat test.jsp
server2 the time is: <%=new java.util.Date() %>
测试:
网页输入172.25.254.1/test.jsp 刷新轮询
三.在tomcat中实现session共享
什么是session:
session:在计算机中,尤其是在网络应用中,称为“会话控制”。Session对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。Session对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在 Session 对象中。
为了对数据的保存更加牢靠,我们选择交叉存放session的方法,即将访问tomcat1上的数据存放在memcache2中,将访问tomcat2上的数据存放在memcache1中,这样存放,当某个tomcat服务断了之后,访问它的客户session并不会消失,而是存放在了对立的memcache,如果存放session的memcache坏了,那么它的数据会立即切换到另一个memcached中.
1.在server1和server2端修改我们的测试页面test.jsp
首先在物理机导入test.jsp到server1
[kiosk@foundation150 Desktop]$ scp test.jsp root@172.25.254.1:/usr/local/tomcat/webapps/ROOT
这是一个可以提交信息的页面:
[root@server1 sbin]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# vim 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>
将test.jsp复制到server2的tomcat文件发布目录下
[root@server1 ROOT]# scp test.jsp root@server2:/usr/local/tomcat/webapps/ROOT/
2.安装nginx的sticky模块和nginx-1.10.1安装包
物理主机发送:
[kiosk@foundation150 Desktop]$ scp nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz root@172.25.254.1:/root
配置server1
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 nginx-1.10.1]# vim src/core/nginx.h 隐藏版本号
[root@server1 nginx-1.10.1]# vim auto/cc/gcc 注释debug日志
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]# make && make install
[root@server1 nginx-1.10.1]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx -s stop 关闭openresty的nginx
[root@server1 sbin]# cd /usr/local/lnmp/nginx/
[root@server1 nginx]# cd conf/
[root@server1 conf]# cp /usr/local/openresty/nginx/conf/nginx.conf . 拷贝之前编译好的配置文件
cp: overwrite `./nginx.conf'? y
对inginx做相应的配置:
vim /usr/local/lnmp/nginx/conf/nginx.conf
[root@server1 conf]# vim nginx.conf
修改:
upstream tomcat {
sticky; 添加
server 172.25.254.1:8080;
server 172.25.254.2:8080; 实现tomcat的负载均衡
}
[root@server1 conf]# cd ..
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ls
nginx nginx.old
[root@server1 sbin]# ./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
[root@server1 sbin]# ./nginx 开启nginx
3.下载memcached服务端(目的是在每次经过浏览器访问tomcat时,记录两者之间的session)
物理真机发送
[kiosk@foundation150 Desktop]$ scp -r memcacheed-swssion root@172.25.254.1:/usr/local/tomcat/lib
[kiosk@foundation150 Desktop]$ scp -r memcacheed-swssion root@172.25.254.2:/usr/local/tomcat/lib
server1配置
[root@server1 sbin]# cd /usr/local/tomcat/logs/
[root@server1 logs]# > catalina.out 清空日志方便后续测试查看
[root@server1 ~]# cd /usr/local/tomcat/lib/
[root@server1 lib]# cd memcacheed-swssion/
[root@server1 memcacheed-swssion]# ll
total 788
-rw-r--r-- 1 root root 43398 Feb 20 10:02 asm-3.2.jar
-rw-r--r-- 1 root root 94830 Feb 20 10:02 kryo-1.04.jar
-rw-r--r-- 1 root root 60575 Feb 20 10:02 kryo-serializers-0.10.jar
-rw-r--r-- 1 root root 132158 Feb 20 10:02 memcached-session-manager-1.6.3.jar
-rw-r--r-- 1 root root 9258 Feb 20 10:02 memcached-session-manager-tc7-1.6.3.jar
-rw-r--r-- 1 root root 4879 Feb 20 10:02 minlog-1.2.jar
-rw-r--r-- 1 root root 23760 Feb 20 10:02 msm-kryo-serializer-1.6.3.jar
-rw-r--r-- 1 root root 11615 Feb 20 10:02 reflectasm-1.01.jar
-rw-r--r-- 1 root root 407912 Feb 20 10:02 spymemcached-2.7.3.jar
[root@server1 memcacheed-swssion]# cp -r * ..
[root@server1 memcacheed-swssion]# cd ..
[root@server1 lib]# rm -fr memcacheed-swssion/
[root@server1 lib]# cd /usr/local/tomcat/conf/
[root@server1 conf]# vim context.xml
最后
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.254.1:11211,n2:172.25.254.2:11211" # 节点一定要对应
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
scp context.xml root@server2:/usr/local/tomcat/conf/ 发送给server2
[root@server1 conf]# /etc/init.d/memcached status 查看
memcached (pid 1371) is running...
[root@server1 conf]# cd ..
[root@server1 tomcat]# cd bin/
[root@server1 bin]# ./shutdown.sh 关闭tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server1 bin]# ./startup.sh 重新开启tomcat 目的加载刚才的服务
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
server2配置
[root@server2 ~]# cd /usr/local/tomcat/lib/memcacheed-swssion/
[root@server2 memcacheed-swssion]# cp -r * ..
[root@server2 memcacheed-swssion]# cd ..
[root@server2 lib]# rm -fr memcacheed-swssion/
[root@server2 lib]# cd /usr/local/tomcat/conf/
[root@server2 conf]# vim context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.254.1:11211,n2:172.25.254.2:11211"
failoverNodes="n2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
[root@server2 tomcat]# yum install -y memcached
[root@server2 tomcat]# /etc/init.d/memcached status
memcached is stopped
[root@server2 tomcat]# /etc/init.d/memcached start
Starting memcached: [ OK ]
[root@server2 tomcat]# cd bin/
[root@server2 bin]# ./shutdown.sh 关闭tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 bin]# ./startup.sh 开启tomcat
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
要实现加载配置
查看日志(tomcat/log)是否一切都配置得当,出现下图的语句说明session配置完成
测试:
网页输入172.25.2.1/test.jsp
建立用户 user 密码 key
将server2的tomcat服务关闭
进入网页 继续建立 user key
进入server1 查看