概述:
1.tomcat简介
Tomcat服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。
对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。
因为Tomcat 技术先进、性能稳定,成为目前比较流行的Web 应用服务器。Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展,但它是独立运行的。
Tomcat不是一个完整意义上的Jave EE服务器,它甚至都没有提供对哪怕是一个主要Java EE API的实现;
但由于遵守apache开源协议,tomcat却又为众多的java应用程序服务器嵌入自己的产品中构建商业的java应用程序服务器,
如JBoss和JOnAS。尽管Tomcat对Jave EE API的实现并不完整,然而很企业也在渐渐抛弃使用传统的Java EE技术(如EJB)转而采用一些开源组件来构建复杂的应用。
这些开源组件如Structs、Spring和Hibernate,而Tomcat能够对这些组件实现完美的支持。
Tomcat很受广大程序员的喜欢,因为它运行时占用的系统资源小,扩展性好,支持负载平衡与邮件服务等开发应用系统常用的功能;
而且它还在不断的改进和完善中,任何一个感兴趣的程序员都可以更改它或在其中加入新的功能。
2.Memcached简介
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
3.nginx简介
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强。
Nginx作为负载均衡服务:既可以在内部直接支持 Rails和PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。
Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好很多。 处理静态文件,索引文件以及自动索引,打开文件描述符缓冲。
无缓存的反向代理加速,简单的负载均衡和容错。
4.tomcat与nginx的区别
nginx: 常用做静态内容服务和代理服务器(不是你FQ那个代理),直面外来请求转发给后面的应用服务(tomcat,django什么的),一般处理静态页面
tomcat:更多用来做做一个应用容器,让java web app跑在里面的东西,对应同级别的有jboss,jetty等东西。一般处理动态页面,比如jsp等
实验前提:
已经完成nginx的源码编译与添加存储
点击此处即可查看详细配置(lnmp架构—4.整合 Nginx + PHP + MySQL—2.添加存储—2.给nginx添加缓存)
1.tomcat的安装与基础配置
安装包:
apache-tomcat-7.0.37.tar.gz
jdk-7u79-linux-x64.tar.gz
1.下载tomcat与jdk(java)并解压
#1.在官网上下载tomcat和jdk
[root@server1 ~]# ls
apache-tomcat-7.0.37.tar.gz nginx-1.14.0
cmake-2.8.12.2-4.el6.x86_64.rpm nginx-1.14.0.tar.gz
Discuz_X3.2_SC_UTF8.zip openresty-1.13.6.1
jdk-7u79-linux-x64.tar.gz openresty-1.13.6.1.tar.gz
memcache-2.2.5 package.xml
memcache-2.2.5.tgz php-5.6.35
mysql-5.7.17 php-5.6.35.tar.bz2
mysql-boost-5.7.17.tar.gz
#2.解压
[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
2.制作软链接
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ls
apache-tomcat-7.0.37 etc include lib libexec openresty share
bin games jdk1.7.0_79 lib64 lnmp sbin src
#制作软链接(便于访问)
[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
3.更改环境变量
#1.更改环境变量
[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
#2.刷新配置
[root@server1 local]# source /etc/profile5.测试tomcat
#1.启动tomcat
[root@server1 ~]# cd /usr/local/tomcat/bin/
[root@server1 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
6.更改nginx配置文件
#1.更改配置文件
[root@server1 bin]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
####################
location ~ .jsp$ {
proxy_pass http://127.0.0.1:8080; #访问本机的8080端口
}
#2.重新加载
[root@server1 conf]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx -s reload
7.网页测试
1.在网页上输入:172.25.66.1:8080,即可访问到正常的tomcat默认发布网页
为了使得界面更好看,做如下设定:
[root@server1 nginx]# cd /usr/local/openresty/nginx/conf/
#1.更改配置文件
[root@server1 conf]# vim nginx.conf
####################
location / {
#root html;
root /usr/local/tomcat/webapps/ROOT;
index index.php index.html index.htm;
}
[root@server1 conf]# cd /usr/local/openresty/nginx/sbin
#2.检测语法
[root@server1 sbin]# ./nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
#3.重新加载
[root@server1 sbin]# ./nginx -s reload
刷新页面:
2. tomcat + nginx 实现负载均衡
1.配置server2结点(同server1结点)
#1.拷贝java和tomcat目录
[root@server1 sbin]# scp -r /usr/local/tomcat/ root@172.25.66.2:/usr/local/
[root@server1 ~]# scp -r /usr/local/java/ root@172.25.66.2:/usr/local/
1
2
3
4
5
6
7
8
9
10
11
12 1#2.可查看到java和tomcat目录
2[root@server2 ~]# cd /usr/local/
3[root@server2 local]# ls
4bin etc games include java lib lib64 libexec sbin share src tomcat
5#3.更改环境变量
6[root@server2 local]# vim /etc/profile
7#####################
8export JAVA_HOME=/usr/local/java
9export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
10export PATH=$PATH:$JAVA_HOME/bin
11
12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 1#4.刷新配置
2[root@server2 local]# source /etc/profile
3
4#5.启动tomcat
5[root@server2 ROOT]# cd /usr/local/tomcat/bin
6[root@server2 bin]# ./startup.sh
7Using CATALINA_BASE: /usr/local/tomcat
8Using CATALINA_HOME: /usr/local/tomcat
9Using CATALINA_TMPDIR: /usr/local/tomcat/temp
10Using JRE_HOME: /usr/local/java
11Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
12#查看端口
13[root@server2 bin]# netstat -tnlp
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 12.更改nginx配置文件
2
3#1.更改配置文件
4[root@server1 ~]# cd /usr/local/openresty/nginx/conf/
5[root@server1 conf]# vim nginx.conf
6####################
7http {
8
9 upstream tomcat {
10 server 172.25.66.1:8080;
11 server 172.25.66.2:8080;
12 }
13
14 include mime.types;
15 default_type application/octet-stream;
16
17 location ~ \.jsp$ {
18 proxy_pass http://tomcat;
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 1[root@server1 conf]# cd ..
2[root@server1 nginx]# cd sbin/
3[root@server1 sbin]# pwd
4/usr/local/openresty/nginx/sbin
5#2.检测语法
6[root@server1 sbin]# ./nginx -t
7nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
8nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
9#3.重新加载
10[root@server1 sbin]# ./nginx -s reload
11
12
133.编写jsp页面
14
15[root@server1 sbin]# cd /usr/local/tomcat/webapps/ROOT/
16[root@server1 ROOT]# ls
17asf-logo.png bg-nav-item.png favicon.ico tomcat.gif WEB-INF
18asf-logo-wide.gif bg-nav.png index.jsp tomcat.png
19bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat-power.gif
20bg-middle.png build.xml tomcat.css tomcat.svg
21#编写jsp页面
22[root@server1 ROOT]# vim test.jsp
23####################
24server1 the time is: <%=new java.util.Date() %>
25
26
#发送发布文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1[root@server1 ROOT]# scp test.jsp root@172.25.66.2:/usr/local/tomcat/webapps/ROOT/
2
3[root@server2 local]# cd /usr/local/tomcat/webapps/ROOT/
4[root@server2 ROOT]# ls
5asf-logo.png bg-nav-item.png favicon.ico tomcat.css tomcat.svg
6asf-logo-wide.gif bg-nav.png index.jsp tomcat.gif WEB-INF
7bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png
8bg-middle.png build.xml test.jsp tomcat-power.gif
9#更改发布文件
10[root@server2 ROOT]# vim test.jsp
11####################
12server2 the time is: <%=new java.util.Date() %>
13
14
- tomcat + nginx + memcached 实现Session共享
1.重新编写测试页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 1[root@server1 ~]# cd /usr/local/tomcat/webapps/ROOT/
2[root@server1 ROOT]# ls
3asf-logo.png bg-nav.png RELEASE-NOTES.txt tomcat-power.gif
4asf-logo-wide.gif bg-upper.png test.jsp tomcat.svg
5bg-button.png build.xml tomcat.css WEB-INF
6bg-middle.png favicon.ico tomcat.gif
7bg-nav-item.png index.jsp tomcat.png
8#1.编写测试页面
9[root@server1 ROOT]# vim test.jsp
10###################
11<%@ page contentType="text/html; charset=GBK" %>
12<%@ page import="java.util.*" %>
13<html><head><title>Cluster App Test</title></head>
14<body>
15Server Info:
16<%
17out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
18<%
19out.println("<br> ID " + session.getId()+"<br>");
20String dataName = request.getParameter("dataName");
21if (dataName != null && dataName.length() > 0) {
22String dataValue = request.getParameter("dataValue");
23session.setAttribute(dataName, dataValue);
24}
25out.print("<b>Session list</b>");
26Enumeration e = session.getAttributeNames();
27while (e.hasMoreElements()) {
28String name = (String)e.nextElement();
29String value = session.getAttribute(name).toString();
30out.println( name + " = " + value+"<br>");
31System.out.println( name + " = " + value);
32}
33%>
34<form action="test.jsp" method="POST">
35name:<input type=text size=20 name="dataName">
36<br>
37key:<input type=text size=20 name="dataValue">
38<br>
39<input type=submit>
40</form>
41</body>
42</html>
43
44
45
46#2.发送给server2结点
47[root@server1 ROOT]# scp test.jsp root@172.25.66.2:/usr/local/tomcat/webapps/ROOT/
48
49
50发现问题:每提交依次用户信息,uid和服务结点都在改变,即轮询接收请求,这样会造成数据丢失
516.启动nginx
52
53[root@server1 conf]# cd /usr/local/lnmp/nginx/sbin
54#1.检测语法
55[root@server1 sbin]# ./nginx -t
56nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
57nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
58#2.启动nginx
59[root@server1 sbin]# ./nginx
60#3.查看端口
61[root@server1 sbin]# netstat -tnlp
624.关闭tomcat服务(模拟后端服务器出现故障)
63
64[root@server1 logs]# cd /usr/local/tomcat/bin/
65[root@server1 bin]# ./shutdown.sh
66Using CATALINA_BASE: /usr/local/tomcat
67Using CATALINA_HOME: /usr/local/tomcat
68Using CATALINA_TMPDIR: /usr/local/tomcat/temp
69Using JRE_HOME: /usr/local/java
70Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
71
726.查看日志:发现之前的信息没有同步,造成了数据的丢失
73
74[root@server2 bin]# cd /usr/local/tomcat/logs
75[root@server2 logs]# tail -f catalina.out
76
77
解决方案:利用mamcache存储与tomcat的jar包使用交叉存储的方式实现存储分离,进而实现sission共享
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 11.开启tomcat
2
3[root@server1 logs]# cd /usr/local/tomcat/bin/
4[root@server1 bin]# ./startup.sh
5Using CATALINA_BASE: /usr/local/tomcat
6Using CATALINA_HOME: /usr/local/tomcat
7Using CATALINA_TMPDIR: /usr/local/tomcat/temp
8Using JRE_HOME: /usr/local/java
9Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
10
11
122.安装并开启memcached服务
13
14[root@server2 ~]# yum install -y memcached
15[root@server2 bin]# /etc/init.d/memcached start
16Starting memcached: [ OK ]
17
183.下载jar包
19
20[root@server1 bin]# cd /usr/local/tomcat/lib/
21[root@server1 lib]# ls
22asm-3.2.jar
23kryo-1.04.jar
24kryo-serializers-0.10.jar
25memcached-session-manager-1.6.3.jar
26memcached-session-manager-tc7-1.6.3.jar
27minlog-1.2.jar
28msm-kryo-serializer-1.6.3.jar
29reflectasm-1.01.jar
30spymemcached-2.7.3.jar
31
32[root@server1 lib]# scp *.jar root@172.25.66.2:/usr/local/tomcat/lib/
33
34 4.更改配置文件
35
36 [root@server1 lib]# cd /usr/local/tomcat/conf/
37 [root@server1 conf]# ls
38 Catalina catalina.properties logging.properties tomcat-users.xml
39 catalina.policy context.xml server.xml web.xml
40 [root@server1 conf]# vim context.xml
41 ##################
42 写入:
43 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
44 memcachedNodes="n1:172.25.66.1:11211,n2:172.25.66.2:11211"
45 failoverNodes="n1"
46 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
47 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
48 />
49
50
1
2
3
4
5
6
7
8
9
10
11 1[root@server1 conf]# scp context.xml root@172.25.66.2:/usr/local/tomcat/conf/
2root@172.25.66.2's password:
3context.xml 100% 1696 1.7KB/s 00:00
4
5[root@server2 lib]# cd /usr/local/tomcat/conf/
6[root@server2 conf]#
7[root@server2 conf]# vim context.xml
8####################
9只需将node1更改为node2即可
10
11
5.重启tomcat
注意:由于tomcat没有restart命令,所以只能先关闭再开启
[root@server1 conf]# cd /usr/local/tomcat/bin/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 1#1.关闭tomcat
2[root@server1 bin]# ./shutdown.sh
3Using CATALINA_BASE: /usr/local/tomcat
4Using CATALINA_HOME: /usr/local/tomcat
5Using CATALINA_TMPDIR: /usr/local/tomcat/temp
6Using JRE_HOME: /usr/local/java
7Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
8#2.开启tomcat
9[root@server1 bin]# ./startup.sh
10Using CATALINA_BASE: /usr/local/tomcat
11Using CATALINA_HOME: /usr/local/tomcat
12Using CATALINA_TMPDIR: /usr/local/tomcat/temp
13Using JRE_HOME: /usr/local/java
14Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
15
16
17[root@server2 conf]# cd /usr/local/tomcat/bin/
18[root@server2 bin]# ./shutdown.sh
19Using CATALINA_BASE: /usr/local/tomcat
20Using CATALINA_HOME: /usr/local/tomcat
21Using CATALINA_TMPDIR: /usr/local/tomcat/temp
22Using JRE_HOME: /usr/local/java
23Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
24[root@server2 bin]# ./startup.sh
25Using CATALINA_BASE: /usr/local/tomcat
26Using CATALINA_HOME: /usr/local/tomcat
27Using CATALINA_TMPDIR: /usr/local/tomcat/temp
28Using JRE_HOME: /usr/local/java
29Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
30
31
测试:
1.提交信息
4.查看日志
[root@server1 logs]# cd /usr/local/tomcat/logs
[root@server1 logs]# vim catalina.out