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添加缓存)
安装包:
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/profile
4.测试java
#1.编写java代码,输出Hello Word!
[root@server1 ~]# vim test.java
#################
public class test{
public static void main(String[] args){
System.out.println("Hello Word!");
}
}
#2.检测java语法
[root@server1 ~]# javac test.java
#3.执行代码
[root@server1 ~]# java test
Hello Word!
5.测试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
#2.查看端口
[root@server1 bin]# netstat -tnlp
#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默认发布网页
2.在网页上输入:172.25.66.1/index.jsp ,即可访问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
1.配置server2结点(同server1结点)
#1.拷贝java和tomcat目录
[root@server1 sbin]# scp -r /usr/local/tomcat/ [email protected]:/usr/local/
[root@server1 ~]# scp -r /usr/local/java/ [email protected]:/usr/local/
#2.可查看到java和tomcat目录
[root@server2 ~]# cd /usr/local/
[root@server2 local]# ls
bin etc games include java lib lib64 libexec sbin share src tomcat
#3.更改环境变量
[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
#4.刷新配置
[root@server2 local]# source /etc/profile
#5.启动tomcat
[root@server2 ROOT]# cd /usr/local/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 -tnlp
#1.更改配置文件
[root@server1 ~]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
####################
http {
upstream tomcat {
server 172.25.66.1:8080;
server 172.25.66.2:8080;
}
include mime.types;
default_type application/octet-stream;
location ~ \.jsp$ {
proxy_pass http://tomcat;
}
[root@server1 conf]# cd ..
[root@server1 nginx]# cd sbin/
[root@server1 sbin]# pwd
/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
3.编写jsp页面
[root@server1 sbin]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# ls
asf-logo.png bg-nav-item.png favicon.ico tomcat.gif WEB-INF
asf-logo-wide.gif bg-nav.png index.jsp tomcat.png
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat-power.gif
bg-middle.png build.xml tomcat.css tomcat.svg
#编写jsp页面
[root@server1 ROOT]# vim test.jsp
####################
server1 the time is: <%=new java.util.Date() %>
#发送发布文件
[root@server1 ROOT]# scp test.jsp [email protected]:/usr/local/tomcat/webapps/ROOT/
[root@server2 local]# cd /usr/local/tomcat/webapps/ROOT/
[root@server2 ROOT]# ls
asf-logo.png bg-nav-item.png favicon.ico tomcat.css tomcat.svg
asf-logo-wide.gif bg-nav.png index.jsp tomcat.gif WEB-INF
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png
bg-middle.png build.xml test.jsp tomcat-power.gif
#更改发布文件
[root@server2 ROOT]# vim test.jsp
####################
server2 the time is: <%=new java.util.Date() %>
在网页上输入:172.25.66.1/test.jsp ,发现server1和server2交替变更,即实现了负载均衡
1.重新编写测试页面
[root@server1 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 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
#1.编写测试页面
[root@server1 ROOT]# vim test.jsp
###################
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster App Test
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%>
<%
out.println("
ID " + session.getId()+"
");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("Session list");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"
");
System.out.println( name + " = " + value);
}
%>
#2.发送给server2结点
[root@server1 ROOT]# scp test.jsp [email protected]:/usr/local/tomcat/webapps/ROOT/
发现问题:每提交依次用户信息,uid和服务结点都在改变,即轮询接收请求,这样会造成数据丢失
1.关闭原来的nginx
[root@server1 nginx-1.10.1]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx -s stop
2.下载旧版本的nginx(因为旧版本才支持添加sticky模块)
安装包:
nginx-1.10.1.tar.gz
#在官网上下载nginx并解压
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
3.隐藏模块与关闭debug日志
[root@server1 ~]# cd /root/nginx-1.10.1/src/core
#1.隐藏模块
[root@server1 core]# vim nginx.h
#2关闭debug日志
[root@server1 core]# cd /root/nginx-1.10.1/auto/cc
[root@server1 cc]# vim gcc
由于需要添加sticky模块,所以需要先下载此模块,便于编译时直接添加
安装包:
nginx-sticky-module-ng.tar.gz
(1).下载并解压模块
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
(2)configure配置(添加模块)
[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 clean
rm -rf Makefile objs
(3)编译与安装
[root@server1 nginx-1.10.1]# make && makeinstall
5.更改配置文件
[root@server1 sbin]# cd /usr/local/lnmp/nginx/conf
[root@server1 conf]# cp /usr/local/openresty/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? y
#添加sticky模块
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# cd /usr/local/lnmp/nginx/sbin
#1.检测语法
[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
#2.启动nginx
[root@server1 sbin]# ./nginx
#3.查看端口
[root@server1 sbin]# netstat -tnlp
此时再提交请求时,便不会交替接收,这样数据便不容易丢失
不足之处:但如果某个后端服务器挂了,可是客户端并不知晓,当你再提交请求时,便会由另一个后端服务器接管,虽然还是可以提交数据,但你之前提交的数据便会丢失
1.清空日志
[root@server1 logs]# pwd
/usr/local/tomcat/logs
[root@server1 logs]# ls
catalina.2019-02-18.log localhost.2019-02-19.log
catalina.2019-02-19.log localhost_access_log.2019-02-18.txt
catalina.out localhost_access_log.2019-02-19.txt
host-manager.2019-02-18.log manager.2019-02-18.log
host-manager.2019-02-19.log manager.2019-02-19.log
localhost.2019-02-18.log
#清空日志
[root@server1 logs]# > catalina.out
[root@server1 logs]# tail -f catalina.out
[root@server1 logs]# cd /usr/local/tomcat/bin/
[root@server1 bin]# ./shutdown.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
5.再次提交信息
6.查看日志:发现之前的信息没有同步,造成了数据的丢失
[root@server2 bin]# cd /usr/local/tomcat/logs
[root@server2 logs]# tail -f catalina.out
解决方案:利用mamcache存储与tomcat的jar包使用交叉存储的方式实现存储分离,进而实现sission共享
1.开启tomcat
[root@server1 logs]# 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
2.安装并开启memcached服务
[root@server2 ~]# yum install -y memcached
[root@server2 bin]# /etc/init.d/memcached start
Starting memcached: [ OK ]
3.下载jar包
[root@server1 bin]# cd /usr/local/tomcat/lib/
[root@server1 lib]# ls
asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar
minlog-1.2.jar
msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar
[root@server1 lib]# scp *.jar [email protected]:/usr/local/tomcat/lib/
4.更改配置文件
[root@server1 lib]# cd /usr/local/tomcat/conf/
[root@server1 conf]# ls
Catalina catalina.properties logging.properties tomcat-users.xml
catalina.policy context.xml server.xml web.xml
[root@server1 conf]# vim context.xml
##################
写入:
[root@server1 conf]# scp context.xml [email protected]:/usr/local/tomcat/conf/
[email protected]'s password:
context.xml 100% 1696 1.7KB/s 00:00
[root@server2 lib]# cd /usr/local/tomcat/conf/
[root@server2 conf]#
[root@server2 conf]# vim context.xml
####################
只需将node1更改为node2即可
注意:由于tomcat没有restart命令,所以只能先关闭再开启
[root@server1 conf]# cd /usr/local/tomcat/bin/
#1.关闭tomcat
[root@server1 bin]# ./shutdown.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
#2.开启tomcat
[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
[root@server2 conf]# cd /usr/local/tomcat/bin/
[root@server2 bin]# ./shutdown.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]# ./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
测试:
2.关闭tomcat服务(模拟后端服务器出现故障)
[root@server2 bin]# pwd
/usr/local/tomcat/bin
[root@server2 bin]# ./shutdown.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@server1 logs]# cd /usr/local/tomcat/logs
[root@server1 logs]# vim catalina.out