0
点赞
收藏
分享

微信扫一扫

Tomcat集群

骑在牛背上看书 2022-10-18 阅读 183


  • 介绍

把web应用运行在单个Tomcat上,在应付一些对稳定性要求不是很高的场合还是可以的。但试想,Tomcat或者是服务器停掉的话,可能会带来不必要甚至是很大的损失,这时候就需要多个Tomcat协同工作来保证应用永远工作。

首先,Tomcat本身是支持集群的。本文介绍了以apache和nginx作为前端代理的负载均衡。

以apache作为前端代理,需要jk模块的支持。而nginx则天生就有代理的功能,不需要添加其他模块。由此看来,nginx可以代理任何web服务器,而apache要和Tomcat联合的话必须要有jk。从这一点来看,nginx是占有优势的。

  • 安装

apt-get install libapache2-mod-jk

libapache2-mod-jk - Apache 2 connector for the Tomcat Java servlet engine(这个是让apache和Tomcat合作的一个模块,很重要哦)

Tomcat的安装很简单,​​点击这里下载​​后解压即可。

  • apache配置

/etc/apache2/mods-enabled/jk.conf

[codesyntax lang="xml"]


<IfModule jk_module>

JkWorkersFile /etc/libapache2-mod-jk/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel debug
JkShmFile /var/log/apache2/jk-runtime-status

JkWatchdogInterval 60

<Location /jk-status>
JkMount jk-status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
<Location /jk-manager>
JkMount jk-manager
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
JkMount /*.html tomcat1
JkMount /*.js tomcat1
JkMount /*.css tomcat1
</IfModule>


[/codesyntax]

JkMount /*.html tomcat1这个配置,可以使用通配符将某些请求分发到指定的Tomcat中。

JkMount /* loadbalancer这样就能把请求分发了。

/etc/libapache2-mod-jk/workers.properties

[codesyntax lang="ini"]


workers.tomcat_home=/home/suren/apache-tomcat-6.0.37/
workers.java_home=/usr/lib/jvm/default-java
ps=/
worker.list=ajp13_worker, tomcat1, tomcat2, loadbalancer
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13
worker.ajp13_worker.lbfactor=1

worker.tomcat2.port=8009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1

worker.tomcat1.port=8009
worker.tomcat1.host=10.0.32.4
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1

worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=ajp13_worker,tomcat1,tomcat2
worker.loadbalancer.sticky_session=false
worker.loadbalancer.sticky_session_force=1


[/codesyntax]

sticky_sesion是黏性session的意思。

  • Tomcat配置

config/server.xml

[codesyntax lang="xml"]


<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">

<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">

<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="utf-8"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
</Server>


[/codesyntax]

上面配置中,最重要的是jvmRoute="tomcat1"。如果你有多个Tomcat的话,每个jvmRoute的值都已经不一样,因为这个是jk模块用于区分Tomcat的唯一标识。

如果多个Tomcat在同一个服务器中的话,注意要把端口修改成不一样的,不然Tomcat会无法启动。

  • 测试

在Tomcat目录中添加index.html文件:webapps/test/index.html

重启apache服务(service apache2 restart)以及所有的Tomcat。

然后,访问apache所在的地址(192.168.1.12:80)就可以看到效果了。

​​http://192.168.1.12/test/index.html​​

  • nginx负载均衡配置

安装:apt-get install nginx

配置文件:/etc/nginx/sites-available/default

[codesyntax lang="ini"]


upstream localhost {
server 10.0.32.39:80 weight=1;
}

server {
listen 8012 default_server;
listen [::]:8012 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

server_name localhost;

location / {
# F displaying a 404.
try_files $uri $uri/ =404;

}

location /test {
index index.html;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}


[/codesyntax]

 

proxy_pass会把请求转发到对应的服务器。

proxy_set_header这些配置都是用于转发http协议头的。

  • 参考

​​Tomcat的基本配置,请看这里。​​

​​Tomcat原理分分析。​​

​​http://www.iteye.com/topic/1125301​​​

举报

相关推荐

0 条评论