tomcat进阶
Tomcat是由Apache软件基金会下属的Jakarta项目开发的一个Servlet容器,按照Sun Microsystems提供的技术规范,实现了对Servlet和JavaServer Page(JSP)的支持,并提供了作为web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阈等。但是不能将Tomcat和Apache HTTP服务器混淆,Apache HTTP服务器时一个用C语言实现的HTTP web服务器,而Tomcat则是由Java编写。
LAMT环境搭建
(1)apache安装
#yum install -y httpd
#systemctl enable httpd #设置开机启动
#systemctl start httpd.service #启动服务
(2)修改配置文件httpd.conf
基于http连接
查看是否有支持http协议的代理模块
#httpd -M |grep http
proxy_http_module (shared)编辑配置文件
#vim host.conf
1
2 ServerName www.test.com
3 ProxyRequests off 关闭正向代理
4 ProxyVia on 是否记录代理
5 ProxyPreserveHost on #开启保留主机名
6
7 Require all granted
8
9 ProxyPass / http://192.168.4.61:8080/
10 ProxyPa***everse / http://192.168.4.61:8080/
11
12 Require all granted
13
14 重启服务测试
#systemctl restart httpd.service
基于AJP协议连接
查看是否加载了支持ajp协议的反向代理模块
#httpd -M |grep ajp
proxy_ajp_module (shared)编辑配置文件
#vim host.conf
1
2 ServerName www.test.com
3 ProxyRequests off
4 ProxyVia on
5 ProxyPreserveHost on
6
7 Require all granted
8
9 ProxyPass / ajp://192.168.4.61:8009/
10 ProxyPa***everse / ajp://192.168.4.61:8009/
11
12 Require all granted
13
14 重启服务测试
后续的安装和LNMT的安装方法一样,就不再累述。
8、LNMT实现Jsprun论坛
mariadb
#yum install -y mariadb-server #安装mariadb服务
#systemctl enable mariadb.service #设置为开机启动
#systemctl start mariadb #启动服务
#mysql_secure_installation #安全初始化
#mysql -uroot -p #创建使用的数据库
MariaDB [(none)]> CREATE DATABASE jsprun;
MariaDB [(none)]> GRANT ALL ON jsprun.* TO joah@'192.168.4.63' IDENTIFIED By '123456';tomcat
#yum install -y java-1.8.0-openjdk #安装java运行环境
#java -version #查看是否安装成功
#yum install -y tomcat tomcat-admin-webapp tomcat-docs-webapp tomcat-lib tomcat-webapps#安装所需要的包
#unzip JspRun\!_6.0.0_Source_UTF8.zip #解压此包
#cp JspRun\!_6.0.0_Source_UTF8/source/WebRoot/ /usr/share/tomcat/webapps/jsprun -a#将此目录下的文件复制到指定目录下
#vim config.properties#修改数据库信息
#vim server.xml #修改server.xml文件
#vim tomcat-users.xml #开启管理功能
#systemctl enable tomcat.service#设置为开机启动
#systemctl start tomcat.service#启动tomcat服务
nginx
安装并启动服务
#yum install -y nginx
#systemctl enable nginx
#systemctl start nginx
修改配置文件
#vim nginx-tomcat.conf
1 server {
2 listen 80;
3 server_name www.test.com;
4 root /usr/share/tomcat/webapps/jsprun;
5 location / {
6 index index.jsp;
7 proxy_pass http://192.168.4.63:8080;
8 }
9 location ~* \.(jsp|do)$ {
10 proxy_pass http://192.168.4.63:8080;
11 }
12 }#nginx -t #语法检测
重启服务测试
点击下一步等 会出现图中红框的内容表示不可写
修改配置文件并使其文件有写权限和执行权限
#chmod 777 config.properties templates/ attachments/ customavatars/ forumdata/ -R
图中显示数据库的内容我们已经在config.properties配置文件中修改了,所以这里不需要修改了,如果安装之前没有修改此内容要正确填写即可,点击下一步安装完成
我们去数据库查看生成的表
如何实现动静分离呢?
#vim nginx-tomcat.conf
1 server {
2 listen 80;
3 server_name www.test.com;
4 root /usr/share/tomcat/webapps/jsprun;
5 location ~* \.(gif|png|jpeg|bmp|jpg|html|htm)$ {
6 proxy_pass http://192.168.4.65:80;
7 }
8 location ~* \.(jsp|do)$ {
9 proxy_pass http://192.168.4.63:8080;
10 }
11 }#systemctl restart nginx.service
将jsprun中image拷贝到apache中/var/www/html目录下
#scp -pr images/* 192.168.4.65:/var/www/html
测试
这里的动静分离其实是将图片和html等结尾的文件专门存放在apache服务器上,而不是实现真正的分离。
五、会话保持
保证同一个用户相关的访问请求被分配到同一台服务器上。
1、会话保持的类型
(1)session sticky
与调度器有关
不同调度器实现方式
nginx:ip_hash
haproxy:source
lvs:sh(2)session cluster:delta session manager
如上图所示,就是session cluster工作原理:基于IP组播来完成session复制
Tomcat会话复制分类:
全局会话复制:利用Delta Manager复制会话中的变更信息到集群中的所有其他节点
非全局复制:使用backup Manager进行复制,它会把session复制给一个指定的备份节点此中session保持一般不超过6个节点,如果小型可以使用。
(3)session server:redis(store),memcached(cache)
如图所示为此中会话保持的工作原理,基于共享会话实现
上述两种与server端有关
2、Tomcat Cluster(Session)
(1)httpd+tomcat cluster
前提:
httpd:mod_proxy,mod_proxy_http,mod_proxy_balancer
tomcat cluster :http connector
实战
同步时间
#ntpdate 172.18.0.1
#vim /etc/chrony.conf
修改hosts文件
#vim /etc/hosts
3 192.168.4.62 tomcatA
4 192.168.4.63 tomcatB基于密钥连接
#ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:z+CBC0/eQP/sW9O+TqUXn6DCxR1j6Wt/XAyvuHMMW9E root@tomcatA
The key's randomart image is:
+-------+
| |
| .|
| . = . |
| . o. + = E|
| . + So + *.|
| = =.B. o.+oB|
| + oo=.oO+++|
| ...=+=.o|
| o..=+o.|
+---------+#ssh-copy-id -i /root/.ssh/id_rsa.pub tomcatB
tomcatB:
#ssh-keygen
#ssh-copy-id -i /root/.ssh/id_rsa.pub tomcatA
tomcatB中的其他配置都和tomcatA配置相同这里就不累述
安装jdk环境
#yum install -y java-1.8.0-openjdk
tomcat安装及配置
#yum install -y tomcat-docs-webapp tomcat-lib tomcat-webapps tomcat
#systemctl enable tomcat
#systemctl start tomcat
配置一个示例程序
#mkdir test/{WEB-INF,classes,lib} -pv
#vi index.jsp
TomcatA
TomcatA.magedu.com
Session ID
Created on
#mkdir test/{WEB-INF,classes,lib} -pv
#vi index.jsp
TomcatB
TomcatB.magedu.com
Session ID
Created on
#vim /etc/tomcat/server.xml#在host中添加如下内容
#systemctl restart tomcat.service#重启测试
至此tomcatA和tomcatB配置完成
http安装及配置
查看是否已经加载所需要的模块
#httpd -M |grep proxy
安装及配置成开启启动并启动服务
#yum install -y httpd
#systemctl enable httpd.service
#systemctl start httpd.service
修改配置文件并实现http调度功能
#vim httpd.conf
354 Include conf.d/tomcat-host.conf
#vim tomcat-host.conf
BalancerMember http://192.168.4.62:8080
BalancerMember http://192.168.4.63:8080
ProxySet lbmethod=byrequests
ServerName www.test.com
ProxyVia on
ProxyRequests off
ProxyPreserveHost on
Require all granted
ProxyPass / balancer://tcsrvs/
ProxyPa***everse / balancer://tcsrvs/
Require all granted
选项说明:
ProxyPreserveHost {on|off}:如果启用此功能,代理会将用户请求报文中的Host行发送给后端服务器,而不再使用PorxyPress指定的服务器地址。如果在反向代理中支持虚拟主机,则需开启此项。
ProxyVia:{on|off|full|block}:用于控制在http首部是否使用Via,主要用于在多级代理中控制代理请求的流向。
off:不开启此功能
on:表示每个请求和响应报文均添加Via
full:表示每个Via都会添加当前apache服务器的版本信息
block:表示每个代理请求报文中的Via都会被移除
ProxyRequests {on|off}:是否开启apache正向代理的功能;启用此项时为了代理http协议需启用mod_proxy_http模块
ProxyPass !|URL :将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径。
path:当前服务器上的某虚拟路径
URL:后端服务器上某URL路径
注意:如果path以“/”结尾,则对应的URL也必须"/"结尾。
key类型
min:连接池的最小容量
max:连接池最大容量
loadfactor:用于负载均衡集群配置中,定义对应后端服务器的权重
retry:当apache将请求发送至后端服务器得到错误响应时等待多长时间以后重试
**lbmethod类型**
byrequests:基于权重将统计请求个数进行调度
bytraffic:基于权重的流量计数调度
bybusyness:通过考量每个后端服务器的当前负载进行调度
maxattempts:放弃请求之前实现故障转义的次数,默认为1,其最大值不应该大于总的节点数
nofailover {on|off}
on:表示后端服务器故障时,用户的session将损坏
stickysession:调度器的sticky
ProxyPa***everse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。
BalancerMember url ]
status:
D:设置不可用disabled
S:设置为stopped
I:忽略错误,请求还会往此主机上调
H:如果别的主机不可用时启用相当于backup
E:设置为error状态
N:仅仅接受seesion sticky请求实现会话粘性
#vim tomcat-host.conf
1 Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
2
3 BalancerMember http://192.168.4.62:8080 route=tomcatA loadfactor=1
4 BalancerMember http://192.168.4.63:8080 route=tomcatB loadfactor=2
5 ProxySet lbmethod=byrequests
6 ProxySet stickysession=ROUTEID
7
8
9
10 ServerName www.test.com
11 ProxyVia on
12 ProxyRequests off
13 ProxyPreserveHost on
14
15 Require all granted
16
17 ProxyPass / balancer://tcsrvs/
18 ProxyPa***everse / balancer://tcsrvs/
19
20 Require all granted
21
22
至此我们就实现的session sticky
实现管理功能
2425 SetHandler balancer-manager 26 ProxyPass ! 27 Require all granted 28
这里为了测试方便我们设置成了所有用户都可以访问,在实际生产中需要指定特定IP访问。
显示状态信息
#vim tomcat-host.conf
29
30 ProxyPass !
31 SetHandler server-status
32 Require all granted
33 重启服务器测试
(2)httpd+tomcat cluster
前提:
httpd:mod_proxy,mod_proxy_ajp,mod_proxy_balancer
tomcat cluster:ajp connecotr
实战
前面的步骤都和上述配置相同
编辑tomcat-host.conf配置文件
#vim tomcat-host.conf
1 Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED
2
3 BalancerMember ajp://192.168.4.62:8009 route=tomcatA loadfactor=1
4 BalancerMember ajp://192.168.4.63:8009 route=tomcatB loadfactor=2
5 ProxySet lbmethod=byrequests
6 ProxySet stickysession=ROUTEID
7
8
9
10 ServerName www.test.com
11 ProxyVia on
12 ProxyRequests off
13 ProxyPreserveHost on
14
15 Require all granted
16
17 ProxyPass / balancer://tcsrvs/
18 ProxyPa***everse / balancer://tcsrvs/
19
20 Require all granted
21
22
23
24
25 SetHandler balancer-manager
26 ProxyPass !
27 Require all granted
28
29
30 ProxyPass !
31 SetHandler server-status
32 Require all granted
33 (3)httpd+tomcat cluster
前提:
httpd:mod_jk
tomcat cluster:ajp connector
此种方法现在不常用就不介绍了...
(4)nginx+tomcat cluster
实战
nginx安装并设置为开机启动
#yum install -y nginx
#systemctl enable nginx
#systemctl start nginx
修改配置文件实现调度功能
#vim tomcat-nginx.conf
1 upstream tomsrvs {
2 server 192.168.4.62:8080 ;
3 server 192.168.4.63:8080 ;
4 }
5
6 server {
7 listen 80;
8 server_name www.test.com;
9 location /{
10 proxy_pass http://tomsrvs;
11 }
12 }基于ip_hash实现会话粘性
#vim tomcat-nginx.conf
1 upstream tomsrvs {
2 server 192.168.4.62:8080 ;
3 server 192.168.4.63:8080 ;
4 ip_hash;
5 }
6
7 server {
8 listen 80;
9 server_name www.test.com;
10 location /{
11 proxy_pass http://tomsrvs;
12 }
13 }
基于hash key 实现
基于指定的key的hash表来实现对请求的调度
1 upstream tomsrvs {
2 server 192.168.4.62:8080 ;
3 server 192.168.4.63:8080 ;
4 hash $remote_addr consistent;
5 }
6
7
8
9 server {
10 listen 80;
11 server_name www.test.com cookie;
12 location /{
13 proxy_pass http://tomsrvs;
14 }
15 }3、tomcat session replication cluster
在tomcat服务器上进行配置
#vi server.xml
#添加jvmRoute下面的配置可以放在Host、Engine、Context中其作用效果不同
#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/test/WEB-INF/
#vim /usr/share/tomcat/webapps/test/WEB-INF/web.xml
23 #添加此内容
重启服务测试
tomcatA和tomcatB的配置相似就不累述。
如果修改/etc/tomcat/web.xml文件时全局配置。
注意的问题:
(1)官方文档上面的配置文件中:
上述少了“/”结尾,记得加上
(2)绑定地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址;建议指定IP地址。
4、session server
tomcatA和tomcatB配置相似,这里以tomcatA为例
(1)安装tomcat和memcache并进行配置
#yum install -y memcached
#systemctl enable memcached
#systemctl start memcached
(2)配置tomcat
下载所需要的jar文件
https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
http://owatlfstl.bkt.clouddn.com/2017-11-07_171315.jpg
#cp *.jar /usr/share/tomcat/lib/
#vim server.xml #编辑配置文件
131
132
138
#scp server.xml tomcatB:/etc/tomcat/server.xml
#vim index.jsp编辑index.jsp文件
1
2
3 TomcatB
4
5 TomcatB.magedu.com
6
7
8 Session ID
9
10
11
12
13 Created on
14
15
16
17
18 tomcatB同理
#systemctl restart tomcat
配置http服务器
#vim tomcat-host.conf
1
2 BalancerMember ajp://192.168.4.62:8009loadfactor=1
3 BalancerMember ajp://192.168.4.63:8009loadfactor=2
4 ProxySet lbmethod=byrequests
5
6
7
8 ServerName www.test.com
9 ProxyVia on
10 ProxyRequests off
11 ProxyPreserveHost on
12
13 Require all granted
14
15 ProxyPass / balancer://tcsrvs/
16 ProxyPa***everse / balancer://tcsrvs/
17
18 Require all granted
19
20
21
22
23 SetHandler balancer-manager
24 ProxyPass !
25 Require all granted
26
27
28 ProxyPass !
29 SetHandler server-status
30 Require all granted
31 重启http服务
#systemctl restart httpd
测试
查看是否已有缓存到本地
#memcached-tool 127.0.0.1:11211 dump
接下来停止tomcatA服务测试
重启启动tomcatA 测试
至此已经实现了session server
序列化工具lavolution实现
memcached-session-manager-tc7-2.1.1.jar
memcached-session-manager-2.1.1.jar
spymemcached-2.12.3.jar
msm-javolution-serializer-2.1.1.jar
javolution-5.4.3.1.jar
#scp /etc/tomcat/server.xml tomcatB:/etc/tomcat/server.xml
#systemctl restarst tomcat
上述创建站点及测试也相同就不累述
测试
#memcached-tool 127.0.0.1:11211 dump
在测试过程中,存在版本不兼容的情况,更换版本即可。
页:
[1]