tomcat相關實驗 1.實現LNT 同主機實現 1、安裝並啟動tomcat 2、安裝nginx並配置 2.實現LAT 同主機(靜態網頁) 1、安裝並啟動tomcat 2、安裝httpd服務並確保有ajp_module和http_module 3、與後端tomcat使用http協議連接時配置 4、與 ...
tomcat相關實驗
1.實現LNT
同主機實現
1、安裝並啟動tomcat
1)OpenJDK的安裝
yum install java-1.8.0-openjdk-devel.x86_64
確定JDK是否安裝可以使用如下命令
java -version ##可以看到版本,說明安裝成功
2)tomcat安裝
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
service tomcat start
ss -ntl
監聽的埠有三個:8080為http協議的介面,8009為ajp協議的介面,8005為管理介面
2、安裝nginx並配置
vim /etc/nginx/nginx.conf
在server段增加如下內容
root /usr/local/tomcat/webapps/ROOT; ##表示訪問靜態文件就訪問nginx的這個目錄,此目錄是tomcat預設根目錄。
index index.jsp;
location ~* \.(jsp|do)$ { ##訪問的是jsp或者do結尾的動態文件就調度到後端主機tomcat
proxy_pass http://192.168.213.251:8080;
}
nginx -t
service nginx restart
http://192.168.213.251/ ##這裡直接訪問80埠就可以了看到tomcat預設主頁面了
2.實現LAT
同主機(靜態網頁)
1、安裝並啟動tomcat
1)OpenJDK的安裝
yum install java-1.8.0-openjdk-devel.x86_64
確定JDK是否安裝可以使用如下命令
java -version ##可以看到版本,說明安裝成功
2)tomcat安裝
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
service tomcat start
ss -ntl
監聽的埠有三個:8080為http協議的介面,8009為ajp協議的介面,8005為管理介面
2、安裝httpd服務並確保有ajp_module和http_module
httpd -M |grep "proxy" ##查看模塊是否已經載入,如果沒有載入,要在配置文件中載入這些模塊
proxy_module (shared)
proxy_ajp_module (shared) ##表示反向代理時後端伺服器是aip協議
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared) ##表示反向代理時後端主機時http協議
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
3、與後端tomcat使用http協議連接時配置
vim /etc/httpd/conf.d/tomcat.conf
<virtualhost *:80>
documentroot "/usr/share/tomcat/webapps/ROOT"
<directory "usr/local/tomcat/webapps/ROOT">
require all granted
</directory>
proxyrequests off ##關閉正向代理,不加可以
proxyvia on
proxypreservehost on
proxypass "/" "http://127.0.0.1:8080/"
proxypassreverse "/" "http://127.0.0.1:8080/"
</virtualhost>
httpd -t
systemctl start httpd
測試:http://172.18.21.107/
4、與後tomcat連接時使用的ajp協議時的配置
vim /etc/httpd/conf.d/tomcat.conf
<virtualhost *:80>
documentroot "/usr/local/tomcat/webapps/ROOT"
<directory "usr/local/tomcat/webapps/ROOT">
require all granted
</directory>
proxyrequests off
proxyvia on
proxypreservehost on
proxypass "/" "ajp://127.0.0.1:8009/"
proxypassreverse "/" "ajp://127.0.0.1:8009/"
</virtualhost>
httpd -t
systemctl reload httpd
5、測試
http://172.18.21.107/
3.實現lnmt的zrlog博客搭建
環境:
A主機:tomcat,nginx,ip地址為192.168.213.251
B主機:資料庫,ip地址為192.168.213.253
1)在A上操作
1、安裝openjdk
yum install java-1.8.0-openjdk-devel.x86_64
java -version ---確認是否安裝成功
2、安裝tomcat
yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
systemctl start tomcat
http://192.168.213.251:8080 ##可以訪問tomcat的預設網站
3、將zrlog放到tomcat的根目錄下
mv app/zrlog-1.7.1-release.war /usr/share/tomcat/webapps
systemctl restart tomcat
ls ##發現文件被自動解開
docs host-manager manager zrlog-1.7.1-release
examples logs ROOT zrlog-1.7.1-release.war
[root@centos7 webapps]#ln -s zrlog-1.7.1-release zrlog
4、安裝nginx並修改配置
vim /etc/nginx/nginx.conf
在server段增加如下內容
root /usr/local/tomcat/webapps; ##表示訪問靜態文件就訪問nginx的這個目錄,此目錄是tomcat預設根目錄。
index index.jsp;
location / { ##訪問的是jsp或者do結尾的動態文件就調度到後端主機tomcat,此處為本地
proxy_pass http://192.168.213.251:8080;
}
nginx -t
service nginx restart
http://192.168.213.251/ ##這裡直接訪問80埠就可以了看到tomcat預設主頁面了
2)在B上的設置
安裝資料庫
yum install mariadb-server
systemctl start mariadb
mysql
create database zrlog;
grant all on zrlog.* to zrloguser@'%' identified by "centos";
3)測試:
登錄網站
http://192.168.213.251/zrlog/
4.實現nginx和httpd反代tomcat集群
環境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1》nginx反代
1、在B和C上的設置
mkdir -pv /usr/share/tomcat/webapps/myapp/WEB-INF
vim /usr/share/tomcat/webapps/myapp/index.jsp
<%@ page language="java" %>
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.com</font></h1> ##在C上將顏色改為green,Tomcat改為B
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("shen.com","shen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
http://172.18.213.251:8080/myapp/
http://172.18.213.254:8080/myapp/
2、A上設置
vim /etc/nginx/nginx.conf
在http中配置
upstream tomcatsrv {
server 192.168.213.251:8080 ;
server 192.168.213.254:8080 ;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.jsp ;
root /usr/share/tomcat/webapps/app;
location / {
proxy_pass http://tomcatsrv ;
}
service nginx restart
http://172.18.213.253/myapp/
2》httpd反代
1、在B和C上的設置同上
2、在調度器上的設置
vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs> ##定義一個後端伺服器組
BalancerMember http://192.168.213.251:8080 ##如果和後端伺服器連接的協議為ajp協議,把http改為ajp並且把埠改為8009即可
BalancerMember http://192.168.213.254:8080
ProxySet lbmethod=byrequests
</proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<Directory /app>
Require all granted
</Directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
httpd -t
service httpd start
http://172.18.213.253/app/
httpd的負載集群功能具有健康狀態檢查功能
可以把後端的一個tomcat停掉
然後訪問http://172.18.213.253/app/
會發現不往關閉的tomcat主機調度了
5.實現httpd和nginx的會話粘性綁定
環境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1)httpd會話綁定
在B和C上設置
vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmroute="tomcatA"> ##在B上此行增加一個jvmroute="tomcatA,在C上此行增加jvmroute="tomcatB"
systemctl restart tomcat
在A上設置
vim /etc/httpd/conf.d/tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tomcatsrvs>
BalancerMember http://172.18.213.251:8080 route=tomcatA
BalancerMember http://172.18.213.254:8080 route=tomcatB
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
service httpd reload
http://172.18.213.253/app/
2)nginx會話綁定
在B和C上操作同上
在A上配置
vim /etc/nginx/nginx.conf
upstream tomcatsrv {
server 192.168.213.251:8080 ;
server 192.168.213.254:8080 ;
hash $request_uri consistent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.jsp ;
root /usr/share/tomcat/webapps;
location / {
proxy_pass http://tomcatsrv ;
}
}
service nginx restart
http://172.18.213.253/app
發現只能調度至第一次訪問的後端伺服器,實現會話綁定。
6.兩個tomcat伺服器保存有相同的會話
環境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1)在B和C上的設置
訪問tomcat的官方文檔
http://192.168.213.254:8080/docs/cluster-howto.html ,Document---->Clustering
將官方文檔中的如下內容複製到tomcat配置文件的<engine>或<host>中,此實驗放到Engine中
vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="localhost">
<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.74.74.74"
port="45564"
frequency="500" ##表示每0.5s發送一次心跳信息告訴其他成員自己還活著
dropTime="3000"/> ##表示3s沒有發送信息就證明壞了
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.18.213.251" ##另外一臺主機修改為172.18.213.254
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>
cp /etc/tomcat/web.xml /usr/share/tomcat/webapps/app/WEB-INF/
cd /usr/share/tomcat/webapps/app/WEB-INF/
vim web.xml
在此文件內部沒有註釋的地方加如下內容
<distributable/> ##註意一定要在這個文件的內部,就在<web.app下加上就可
systemctl restart tomcat
註意:
CentOS 7上的tomcat自帶的文檔中的配置示例有語法錯誤;沒有加最後的/
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
2)在A的設置
vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs>
BalancerMember http://172.18.213.251:8080
BalancerMember http://172.18.213.254:8080
ProxySet lbmethod=byrequests
</Proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
service httpd reload
http://172.18.213.253/app/
6.實現session會話保持到memcache伺服器
要想將會話保存至後端memcache中,並且每個memcache都保存相同的會話,需要一個管理項目memcached-session-manager,項目地址:https://github.com/magro/memcached-session-manager,找到SetupAndConfiguration wiki page.並點擊進去。其中
在Add memcached-session-manager jars to tomcat處下載(根據所需進行下載)
下載.jar文件至各tomcat的/usr/share/tomcat/lib/目錄中,其中的${version}要換成你所需要的版本號,tc${6,7,8}要選與tomcat版本相同的版本號。
memcached-session-manager-2.1.1.jar
memcached-session-manager-tc7-2.1.1.jar ##要根據tomcat的版本,實驗時是7版本,所以這裡要下載tc7
spymemcached-2.9.1.jar
在Add custom serializers to your webapp (optional)處下載(根據所需進行下載)
這裡下載的是kryo-serializer,有如下jar文件需要下載
msm-kryo-serializer-2.1.1.jar
kryo-serializers-0.42.jar
kryo-4.0.1.jar
minlog-1.3.0.jar
reflectasm-1.11.3-shaded.jar
reflectasm-1.11.3.jar
asm-5.2.jar
objenesis-2.6.jar
實現過程如下
1、在director上實現nginx或者httpd的反向代理至tomcat集群,本實驗用的是httpd
vim /etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs>
BalancerMember http://172.18.213.251:8080
BalancerMember http://172.18.213.254:8080
ProxySet lbmethod=byrequests
</Proxy>
namevirtualhost *:80
<VirtualHost *:80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer://tomcatsrvs/
ProxyPassReverse / balancer://tomcatsrvs/
</VirtualHost>
service httpd start
2、在兩個後端伺服器上的設置
安裝tomcat和memcache並啟動服務
yum install memcached -y
systemctl start memcached
ls /usr/share/tomcat/lib/ ##複製.jar文件到此目錄
vim /etc/tomcat/server.xml ##將官方文檔中的此段內容複製到tomcat的配置文件中
<Context path="/app" docBase="/usr/share/tomcat/webapps/app" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.18.213.254:11211,n2:172.18.213.254:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
</Context>
systemctl restart tomcat
3、測試
安裝客戶端工具
yum install -y libmemcached ##不安裝此軟體包無法使用memdump等客戶端工具
http://172.18.213.253/app/ ##訪問發現被調度到不同的tomcat主機,但會話是一樣的
memdump --server 172.18.21.107:11211 ##此命令可以查看到memcache中緩存的值
systemctl stop memcached ##關閉一臺memcached
http://172.18.213.253/app/ ##繼續訪問發現會話仍然不變,說明會話在兩台memcache中都緩存了