Tomcat是由Apache软件基金会下属的Jakarta项目开发的一个Servlet容器,按照Sun Microsystems提供的技术规范,实现了对Servlet和JavaServer Page(JSP)的支持,并提供了作为web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阈等。但是不能将Tomcat和Apache HTTP服务器混淆,Apache HTTP服务器时一个用C语言实现的HTTP web服务器,而Tomcat则是由Java编写。
(1)apache安装
[root@web~]#yum install -y httpd
[root@web~]#systemctl enable httpd
#设置开机启动
[root@web~]#systemctl start httpd.service
#启动服务
(2)修改配置文件httpd.conf
基于http连接
查看是否有支持http协议的代理模块
[root@web/etc/httpd/conf]#httpd -M |grep http
proxy_http_module (shared)
编辑配置文件
[root@web/etc/httpd/conf.d]#vim host.conf
1 <VirtualHost *:80> 2 ServerName www.test.com 3 ProxyRequests off 关闭正向代理 4 ProxyVia on 是否记录代理 5 ProxyPreserveHost on #开启保留主机名 6 <Proxy *> 7 Require all granted 8 </Proxy> 9 ProxyPass / http://192.168.4.61:8080/ 10 ProxyPassReverse / http://192.168.4.61:8080/ 11 <Location /> 12 Require all granted 13 </Location> 14 </VirtualHost>
重启服务测试
[root@web/etc/httpd/conf.d]#systemctl restart httpd.service
基于AJP协议连接
查看是否加载了支持ajp协议的反向代理模块
[root@web/etc/httpd/conf]#httpd -M |grep ajp
proxy_ajp_module (shared)
编辑配置文件
[root@web/etc/httpd/conf.d]#vim host.conf
1 <VirtualHost *:80> 2 ServerName www.test.com 3 ProxyRequests off 4 ProxyVia on 5 ProxyPreserveHost on 6 <Proxy *> 7 Require all granted 8 </Proxy> 9 ProxyPass / ajp://192.168.4.61:8009/ 10 ProxyPassReverse / ajp://192.168.4.61:8009/ 11 <Location /> 12 Require all granted 13 </Location> 14 </VirtualHost>
重启服务测试
后续的安装和LNMT的安装方法一样,就不再累述。
[root@mysql~]#yum install -y mariadb-server
#安装mariadb服务
[root@mysql~]#systemctl enable mariadb.service
#设置为开机启动
[root@mysql~]#systemctl start mariadb
#启动服务
[root@mysql~]#mysql_secure_installation
#安全初始化
[root@mysql~]#mysql -uroot -p
#创建使用的数据库
MariaDB [(none)]> CREATE DATABASE jsprun; MariaDB [(none)]> GRANT ALL ON jsprun.* TO joah@‘192.168.4.63‘ IDENTIFIED By ‘123456‘;
tomcat
[root@tomcat~]#yum install -y java-1.8.0-openjdk
#安装java运行环境
[root@tomcat~]#java -version
#查看是否安装成功
[root@tomcat~]#yum install -y tomcat tomcat-admin-webapp tomcat-docs-webapp tomcat-lib tomcat-webapps
#安装所需要的包
[root@tomcat~]#unzip JspRun\!_6.0.0_Source_UTF8.zip
#解压此包
[root@tomcat~]#cp JspRun\!_6.0.0_Source_UTF8/source/WebRoot/ /usr/share/tomcat/webapps/jsprun -a
#将此目录下的文件复制到指定目录下
[root@tomcat/usr/share/tomcat/webapps/jsprun]#vim config.properties
#修改数据库信息
[root@tomcat/etc/tomcat]#vim server.xml
#修改server.xml文件
[root@tomcat/etc/tomcat]#vim tomcat-users.xml
#开启管理功能
[root@tomcat/etc/tomcat]#systemctl enable tomcat.service
#设置为开机启动
[root@tomcat/etc/tomcat]#systemctl start tomcat.service
#启动tomcat服务
nginx
安装并启动服务
[root@nginx~]#yum install -y nginx
[root@nginx~]#systemctl enable nginx
[root@nginx~]#systemctl start nginx
修改配置文件
[root@nginx/etc/nginx/conf.d]#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 }
[root@nginx/etc/nginx/conf.d]#nginx -t
#语法检测
重启服务测试
点击下一步等 会出现图中红框的内容表示不可写
修改配置文件并使其文件有写权限和执行权限
[root@tomcat/usr/share/tomcat/webapps/jsprun]#chmod 777 config.properties templates/ attachments/ customavatars/ forumdata/ -R
图中显示数据库的内容我们已经在config.properties
配置文件中修改了,所以这里不需要修改了,如果安装之前没有修改此内容要正确填写即可,点击下一步安装完成
我们去数据库查看生成的表
如何实现动静分离呢?
[root@nginx/etc/nginx/conf.d]#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 }
[root@nginx/etc/nginx/conf.d]#systemctl restart nginx.service
将jsprun中image拷贝到apache中/var/www/html
目录下
[root@tomcat/usr/share/tomcat/webapps/jsprun]#scp -pr images/* 192.168.4.65:/var/www/html
测试
这里的动静分离其实是将图片和html等结尾的文件专门存放在apache服务器上,而不是实现真正的分离。
保证同一个用户相关的访问请求被分配到同一台服务器上。
(1)session sticky
与调度器有关
nginx:ip_hash haproxy:source lvs:sh
(2)session cluster:delta session manager
如上图所示,就是session cluster工作原理:基于IP组播来完成session复制
全局会话复制:利用Delta Manager复制会话中的变更信息到集群中的所有其他节点 非全局复制:使用backup Manager进行复制,它会把session复制给一个指定的备份节点
此中session保持一般不超过6个节点,如果小型可以使用。
(3)session server:redis(store),memcached(cache)
如图所示为此中会话保持的工作原理,基于共享会话实现
上述两种与server端有关
前提:
httpd:mod_proxy,mod_proxy_http,mod_proxy_balancer
tomcat cluster :http connector
同步时间
[root@tomcatA~]#ntpdate 172.18.0.1
[root@tomcatA~]#vim /etc/chrony.conf
修改hosts文件
[root@tomcatA~]#vim /etc/hosts
3 192.168.4.62 tomcatA 4 192.168.4.63 tomcatB
基于密钥连接
[root@tomcatA~]#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: +---[RSA 2048]----+ | | | . | | . = . | | . o . + = E| | . + S o + *.| | = =.B. o.+oB| | + oo=.oO+++| | ...=+=.o| | o..=+o.| +----[SHA256]-----+
[root@tomcatA~]#ssh-copy-id -i /root/.ssh/id_rsa.pub tomcatB
tomcatB:
[root@tomcatB~]#ssh-keygen
[root@tomcatB~]#ssh-copy-id -i /root/.ssh/id_rsa.pub tomcatA
tomcatB中的其他配置都和tomcatA配置相同这里就不累述
安装jdk环境
[root@tomcatB~]#yum install -y java-1.8.0-openjdk
tomcat安装及配置
[root@tomcatA~]#yum install -y tomcat-docs-webapp tomcat-lib tomcat-webapps tomcat
[root@tomcatA~]#systemctl enable tomcat
[root@tomcatA~]#systemctl start tomcat
配置一个示例程序
[root@tomcatA/usr/share/tomcat/webapps]#mkdir test/{WEB-INF,classes,lib} -pv
[root@tomcatA/usr/share/tomcat/webapps/test]#vi index.jsp
<%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
[root@tomcatB/usr/share/tomcat/webapps]#mkdir test/{WEB-INF,classes,lib} -pv
[root@tomcatB/usr/share/tomcat/webapps/test]#vi index.jsp
<%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
[root@tomcatA/usr/share/tomcat/webapps]#vim /etc/tomcat/server.xml
#在host中添加如下内容
[root@tomcatA/usr/share/tomcat/webapps]#systemctl restart tomcat.service
#重启测试
至此tomcatA和tomcatB配置完成
http安装及配置
查看是否已经加载所需要的模块
[root@httpd/etc/httpd/conf.d]#httpd -M |grep proxy
安装及配置成开启启动并启动服务
[root@httpd~]#yum install -y httpd
[root@httpd~]#systemctl enable httpd.service
[root@httpd~]#systemctl start httpd.service
修改配置文件并实现http调度功能
[root@httpd/etc/httpd/conf]#vim httpd.conf
354 Include conf.d/tomcat-host.conf
[root@httpd/etc/httpd/conf.d]#vim tomcat-host.conf
<proxy balancer://tcsrvs> BalancerMember http://192.168.4.62:8080 BalancerMember http://192.168.4.63:8080 ProxySet lbmethod=byrequests </Proxy> <VirtualHost *:80> ServerName www.test.com ProxyVia on ProxyRequests off ProxyPreserveHost on <Proxy *> Require all granted </Proxy> ProxyPass / balancer://tcsrvs/ ProxyPassReverse / balancer://tcsrvs/ <Location /> Require all granted </Location> </VirtualHost>
选项说明:
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 [path] !|URL [key=value ...]:将后端服务器某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 ProxyPassReverse:用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。 BalancerMember [balancerurl] url [key=value [key=value...]] status: D:设置不可用disabled S:设置为stopped I:忽略错误,请求还会往此主机上调 H:如果别的主机不可用时启用相当于backup E:设置为error状态 N:仅仅接受seesion sticky请求
实现会话粘性
[root@httpd/etc/httpd/conf.d]#vim tomcat-host.conf
1 Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED 2 <proxy balancer://tcsrvs> 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 </Proxy> 8 9 <VirtualHost *:80> 10 ServerName www.test.com 11 ProxyVia on 12 ProxyRequests off 13 ProxyPreserveHost on 14 <Proxy *> 15 Require all granted 16 </Proxy> 17 ProxyPass / balancer://tcsrvs/ 18 ProxyPassReverse / balancer://tcsrvs/ 19 <Location /> 20 Require all granted 21 </Location> 22 </VirtualHost>
至此我们就实现的session sticky
实现管理功能
24 <Location /balancer-manager> 25 SetHandler balancer-manager 26 ProxyPass ! 27 Require all granted 28
这里为了测试方便我们设置成了所有用户都可以访问,在实际生产中需要指定特定IP访问。
显示状态信息
[root@httpd/etc/httpd/conf.d]#vim tomcat-host.conf
29 <Location /status> 30 ProxyPass ! 31 SetHandler server-status 32 Require all granted 33 </Location>
重启服务器测试
前提:
httpd:mod_proxy,mod_proxy_ajp,mod_proxy_balancer
tomcat cluster:ajp connecotr
前面的步骤都和上述配置相同
编辑tomcat-host.conf配置文件
[root@httpd/etc/httpd/conf.d]#vim tomcat-host.conf
1 Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e;path=/" env=BALANCER_ROUTE_CHANGED 2 <proxy balancer://tcsrvs> 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 </Proxy> 8 9 <VirtualHost *:80> 10 ServerName www.test.com 11 ProxyVia on 12 ProxyRequests off 13 ProxyPreserveHost on 14 <Proxy *> 15 Require all granted 16 </Proxy> 17 ProxyPass / balancer://tcsrvs/ 18 ProxyPassReverse / balancer://tcsrvs/ 19 <Location /> 20 Require all granted 21 </Location> 22 </VirtualHost> 23 24 <Location /bm> 25 SetHandler balancer-manager 26 ProxyPass ! 27 Require all granted 28 </Location> 29 <Location /status> 30 ProxyPass ! 31 SetHandler server-status 32 Require all granted 33 </Location>
前提:
httpd:mod_jk
tomcat cluster:ajp connector
此种方法现在不常用就不介绍了...
nginx安装并设置为开机启动
[root@httpd~]#yum install -y nginx
[root@httpd~]#systemctl enable nginx
[root@httpd~]#systemctl start nginx
修改配置文件实现调度功能
[root@httpd/etc/nginx/conf.d]#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实现会话粘性
[root@httpd/etc/nginx/conf.d]#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 }
在tomcat服务器上进行配置
[root@tomcatB/etc/tomcat]#vi server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcatA"> #添加jvmRoute
下面的配置可以放在Host、Engine、Context中其作用效果不同
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.100.100.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" #这里设定为tomcat真实IP port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"> </Cluster>
[root@tomcatA/etc/tomcat]#cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/test/WEB-INF/
[root@tomcatA/etc/tomcat]#vim /usr/share/tomcat/webapps/test/WEB-INF/web.xml
23 <distributable/> #添加此内容
重启服务测试
tomcatA和tomcatB的配置相似就不累述。
如果修改/etc/tomcat/web.xml文件时全局配置。
(1)官方文档上面的配置文件中:
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
上述少了“/”结尾,记得加上
(2)绑定地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址;建议指定IP地址。
tomcatA和tomcatB配置相似,这里以tomcatA为例
(1)安装tomcat和memcache并进行配置
[root@tomcatA/etc/tomcat]#yum install -y memcached
[root@tomcatA/etc/tomcat]#systemctl enable memcached
[root@tomcatA/etc/tomcat]#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
[root@tomcatA~]#cp *.jar /usr/share/tomcat/lib/
[root@tomcatA/etc/tomcat]#vim server.xml
#编辑配置文件
131 <Context> 132 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 133 memcachedNodes="n1:192.168.4.62:11211,n2:192.168.4.63:11211" 134 failoverNodes="n1" 135 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" 136 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 137 /> 138 </Context>
[root@tomcatA/etc/tomcat]#scp server.xml tomcatB:/etc/tomcat/server.xml
[root@tomcatB/usr/share/tomcat/webapps/test]#vim index.jsp
编辑index.jsp文件
1 <%@ page language="java" %> 2 <html> 3 <head><title>TomcatB</title></head> 4 <body> 5 <h1><font color="blue">TomcatB.magedu.com</font></h1> 6 <table align="centre" border="1"> 7 <tr> 8 <td>Session ID</td> 9 <% session.setAttribute("magedu.com","magedu.com"); %> 10 <td><%= session.getId() %></td> 11 </tr> 12 <tr> 13 <td>Created on</td> 14 <td><%= session.getCreationTime() %></td> 15 </tr> 16 </table> 17 </body> 18 </html>
tomcatB同理
[root@tomcatA~]#systemctl restart tomcat
配置http服务器
[root@httpd/etc/httpd/conf.d]#vim tomcat-host.conf
1 <proxy balancer://tcsrvs> 2 BalancerMember ajp://192.168.4.62:8009 loadfactor=1 3 BalancerMember ajp://192.168.4.63:8009 loadfactor=2 4 ProxySet lbmethod=byrequests 5 </Proxy> 6 7 <VirtualHost *:80> 8 ServerName www.test.com 9 ProxyVia on 10 ProxyRequests off 11 ProxyPreserveHost on 12 <Proxy *> 13 Require all granted 14 </Proxy> 15 ProxyPass / balancer://tcsrvs/ 16 ProxyPassReverse / balancer://tcsrvs/ 17 <Location /> 18 Require all granted 19 </Location> 20 </VirtualHost> 21 22 <Location /bm> 23 SetHandler balancer-manager 24 ProxyPass ! 25 Require all granted 26 </Location> 27 <Location /status> 28 ProxyPass ! 29 SetHandler server-status 30 Require all granted 31 </Location>
重启http服务
[root@httpd/etc/httpd/conf.d]#systemctl restart httpd
测试
查看是否已有缓存到本地
[root@tomcatA~]#memcached-tool 127.0.0.1:11211 dump
接下来停止tomcatA服务测试
重启启动tomcatA 测试
至此已经实现了session server
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
[root@memcache~]#scp /etc/tomcat/server.xml tomcatB:/etc/tomcat/server.xml
[root@memcache~]#systemctl restarst tomcat
上述创建站点及测试也相同就不累述
[root@memcache/usr/share/tomcat/lib]#memcached-tool 127.0.0.1:11211 dump
在测试过程中,存在版本不兼容的情况,更换版本即可。
本文出自 “Joah” 博客,请务必保留此出处http://merit.blog.51cto.com/10757694/1979763
原文地址:http://merit.blog.51cto.com/10757694/1979763