tomcat及jdk安装
1)tomcat要在java环境下,安装jdk和tomcat
tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
2)做好软链接便于访问
cd /usr/local
ln -s jdk1.7.0_79/ java
ln -s apache-tomcat-7.0.37/ tomcat
3)配置环境变量(将java和javac写入全局变量)
vim /etc/profile
末尾添加
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
#测试
[root@server1 local]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin:/usr/local/java/bin
[root@server1 local]# echo $CLASSPATH
.:/usr/local/java/lib:/usr/local/java/jre/lib
[root@server1 local]# echo $JAVA_HOME
/usr/local/java
[root@server1 local]# which java
/usr/local/java/bin/java
[root@server1 local]# which javac
/usr/local/java/bin/javac
vim test.java
public class test
{
public static void main(String[] args)
{
System.out.println("Hello World1");
}
}
javac test.java #编译test.java
java test #执行
cd /usr/local/tomcat
bin/startup.sh #启动tomcat
同样在server2(172.25.40.2)安装tomcat,jdk
网页访问:172.25.40.1:8080
网页访问:172.25.40.2:8080
tomcat 安装完成。
nginx实现对tomcat负载均衡
编写页面测试:
server1:
cd /usr/local/tomcat/webapps/ROOT/
vim test.jsp
server1-The time is: <%=new java.util.Date() %>
cd /usr/local/tomcat/
bin/startup.sh ##打开tomcat服务
server2:
cd /usr/local/tomcat/webapps/ROOT/
vim test.jsp ##编写初始化配置界面
server2-The time is: <%=new java.util.Date() %>
cd /usr/local/tomcat/
bin/startup.sh ##打开tomcat服务
netstat -antlp
测试nginx对tomcat的负载均衡:
在server1 的 /usr/local/openresty/nginx/conf编辑nginx.conf 以jsp结尾的会去匹配本机的8080端口
vim nginx.conf
upstream tomcat {
server 172.25.40.1:8080;
server 172.25.40.2:8080;
}
location / {
root /usr/local/tomcat/webapps/ROOT;
index index.php index.html index.htm;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx
http://172.25.40.1/test.jsp
4)增加测试页面:
vim /usr/local/apache-tomcat-7.0.37/webapps/ROOT/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>
但是发现每次请求数据时,后端服务器server1,server2轮流提供服务,不能保证数据的稳定性和安全性。
什么是memcache
memcache是一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。
MemCache的工作流程如下:
先检查客户端的请求数据是否在memcached中,如有,直接把请求数据返回,不再对数据库进行任何操作;如果请求的数据不在memcached中,就去查数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcached中(memcached客户端不负责,需要程序明确实现);每次更新数据库的同时更新memcached中的数据,保证一致性;当分配给memcached内存空间用完之后,会使用LRU(Least Recently Used,最近最少使用)策略加上到期失效策略,失效数据首先被替换,然后再替换掉最近未使用的数据。
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。
Memcached是以守护程序(监听)方式运行于一个或多个服务器中,随时会接收客户端的连接和操作。
session,memcache交叉缓存
5)现在为了实现session共享,在nginx配置文件中增加tomcat模块,同时需要支持sticky(粘滞)模块,nginx-1.14不支持sticky,所以使用nginx-1.10版本,重新编译nginx。
注意:将server1的 /usr/local/lnmp/nginx 目录全部删除 再次编译
tar zxf nginx-sticky-module-ng.tar.gz -C /usr/local/
cd nginx-1.10.3
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/usr/local/nginx-sticky-module-ng
make && make install
ln -s /usr/local/lnmp/nginx/sbin/nginx /sbin/
vim /usr/local/lnmp/nginx/conf/nginx.conf
upstream tomcat {
sticky;
server 172.25.40.1:8080;
server 172.25.40.2:8080;
}
gzip on; #开启gzip
location / {
root /usr/local/tomcat/webapps/ROOT;
index index.html index.htm;
}
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
nginx #开启nginx
再次刷新页面,访问的ID号不会变,实现了sticky