1.安装jdk---为tomcat提供环境(jdk是java程序必备软件)
[root@server2 lamp]# cp -r jdk1.7.0_79/ /usr/local/
[root@server2 lamp]# cd /usr/local/
[root@server2 local]# ln -s jdk1.7.0_79/ java
[root@server2 local]# ll
total 44
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 Jul 23 09:19 java -> jdk1.7.0_79/
drwx------ 4 root root 4096 Jul 23 09:18 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 Jul 18 21:58 share
drwxr-xr-x. 2 root root 4096 Jun 28 2011 src
[root@server2 local]# vim /etc/profile ##写入全局变量的文件,及所有bash都可使用,而/.bash_profile是局部变量,只有特定bash可以使用
[root@server2 local]# source /etc/profile ##使修改生效
[root@server2 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
[root@server2 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server2 ~]# cat test.java
public class test {
public static void main(String[] arge) {
System.out.println("Hello World !");
}
}
[root@server2 ~]# javac test.java ##语法检测
[root@server2 ~]# java test ##执行效果,说明jdk可以正常使用,环境搭建成功
Hello World !
2.nginx+tomcat
[root@server2 lamp]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server2 lamp]# cd /usr/local/
[root@server2 local]# ln -s apache-tomcat-7.0.37/ tomcat ##做软连接方便后面的操作
[root@server2 local]# cd tomcat/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 -antlp ##tomcat监听8080端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 906/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 982/master
tcp 0 0 172.25.66.2:22 172.25.66.250:49921 ESTABLISHED 1031/sshd
tcp 0 0 :::22 :::* LISTEN 906/sshd
tcp 0 0 ::1:25 :::* LISTEN 982/master
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 1147/java
tcp 0 0 :::8009 :::* LISTEN 1147/java
[root@server2 tomcat]# cd webapps/ROOT/
[root@server2 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
index.html是jsp的默认发布文件
tomcat的默认网页页面
##编辑一个测试网页,jsp的默认发布目录是/usr/local/tomcat/webapps/ROOT/,默认发布页面为该目录下的index.jsp
[root@server2 ROOT]# cat test.jsp
the time is: <%=new java.util.Date() %>
17 http {
18 upstream westos { ##负载均衡服务池
19 #ip_hash;
20 server 172.25.66.2:8080;
21 server 172.25.66.3:8080;
22 #server 127.0.0.1:8000 backup;
23 }
76 location ~ \.php$ { ##php结尾请求全部交给fastcgi处理
77 root html;
78 fastcgi_pass 127.0.0.1:9000;
79 fastcgi_index index.php;
80 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name ;
81 include fastcgi.conf;
82 }
83 location ~ \.jsp$ { ##jsp结尾的进行负载均衡请求
84 proxy_pass http://westos; ##请求转向westos定义的服务器列表
85 }
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 conf]# nginx -s reload
测试:(负载均衡)此时以实现server2和server3之间的负载均衡,由于测试网页写入的一致,所以显示的内容相同
3.nginx+tomcat+memcache
[root@server2 ROOT]# vim test.jsp ##存储用户信息的网页
the time is: <%=new java.util.Date() %>
<%@ 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>
测试:
轮询时,在server2记录信息后后自动调到server3,这样server2填写的信息就会丢失
3.ip_hash IP Hash,根据客户端的IP,将请求分配到不同的服务器上;
Tomcat-1 (T1) 将 session 存储在 memcached-2 (T2)上。只有当 M2 不可用时,T1 才将 session 存储在 memcached-1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩溃时也不会丢失 session 会话,避免单点故障。
以下步骤在server2和server3上做相同配置,这里用server2做介绍
4.sticky算法(比ip_hash更优化)
cookie,服务器给客户端下发一个cookie,具有特定cookie的请求会分配给它的发行者。Sticky就是基于cookie的一种负载均衡解决方案,通过cookie实现客户端与后端服务器的会话保持, 在一定条件下可以保证同一个客户端访问的都是同一个后端服务器。请求来了,服务器发个cookie,并说:下次来带上,直接来找我。
Sticky工作原理 Sticky是nginx的一个模块,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上
[root@server2 ROOT]# yum install memcached -y
[root@server2 lib]# rm -fr memcached-session-manager-tc6-1.6.3.jar
[root@server2 tomcat]# vim conf/context.xml ##server3将n1改为n2
配置完成后当server2出故障时,server3会接替server2工作,减少信息丢失
[root@server2 tomcat]# bin/startup.sh
[root@server2 tomcat]# telnet localhost 11211
[root@server1 lamp]# tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz
[root@server1 lamp]# tar zxf nginx-1.10.1.tar.gz
[root@server1 lamp]# cd nginx
nginx-1.10.1/
nginx-1.10.1.tar.gz
nginx-1.12.0.tar.gz
nginx-1.8.1.tar.gz
nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d/
nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz
nginx-sticky-module-1.1.tar.gz
nginx-sticky-module-ng.tar.gz
nginx+tomcat+memcached.pdf
[root@server1 lamp]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@server1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module --add-module=/root/lamp/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d
[root@server1 nginx-1.10.1]# make && make install
[root@server1 conf]# vim nginx.conf
17 http {
18 upstream westos {
19 sticky;
20 server 172.25.66.2:8080;
21 server 172.25.66.3:8080;
22 }
68 location ~ \.jsp$ {
69 proxy_pass http://westos;
70 }
[root@server1 sbin]# ./nginx
[root@server1 sbin]# ./nginx -s stop