Apache Tomcat能夠說是當下最受歡迎的開源Java Web服務器。Tomcat的單個實例恐怕沒法知足訪問量日增的須要。這種狀況下,須要考慮在「集羣」環境下運行Tomcat;在這種環境下,Web服務器的工做負載分配到多個Tomcat實例。前端
負載均衡:前端服務器(經常名爲「負載均衡器」、「代理均衡器」或「反向代理」)收到HTTP請求後,前端服務器將請求分發到後端的不止一個「worker」Web服務器,由它們實際處理請求。負載均衡能夠消除後端的單一故障點,而且能夠爲任何Web服務實現高可用性、高擴展性以及更合理的資源優化。java
會話複製:會話複製是一種機制,將客戶端會話的整個狀態原本來本複製到集羣中的兩個或多個服務器實例,以實現容錯和故障切換功能。一般狀況下,分發的狀態服務可以跨集羣中的多個不一樣服務器實例,複製客戶端會話的狀態。linux
集羣:集羣由兩個或多個Web服務器實例組成,這些服務器實例步調一致地工做,透明地處理客戶端請求。客戶端將一組服務器實例認爲是單一實體服務。集羣的目的是,爲客戶端提供高可用性服務,同時儘可能高效地利用全部的可用計算資源。git
下面是搭建Tomcat集羣的具體要求。我在本教程中假設有4臺Ubuntu服務器。github
Apache服務器#1:(106)帶mod_jk的Apache HTTP Web服務器(充當代理均衡器)redis
Tomcat服務器#2:(107-109)Java運行時6.x或更高版本,以及Apache Tomcat 7.x(充當worker Web服務器)。apache
Apache Web服務器充當代理均衡器。Apache Web服務器是客戶端惟一看得見的那臺服務器,全部的Tomcat實例都被隱藏起來,客戶端看不見它們。mod_jk插件被激活後,Apache Web服務器將任何入站的HTTP請求轉發到集羣中的Tomcat worker實例。vim
Tomcat Connectors讓你能夠將Tomcat鏈接到其餘開源Web服務器。對Apache Web服務器而言,Tomcat Connectors以一種名爲mod_jk的Apache模塊而出現。裝有mod_jk的Apache Web服務器能夠將Ubuntu服務器變成代理均衡器。 windows
$ sudo apt-get install apache2 libapache2-mod-jk
1)Jdk安裝後端
$ apt-cache search openjdk After checking the latest version of OpenJDK, install it with apt-get $ sudo apt-get install openjdk-7-jdk
$ vi ~/.bashrc設置環境變量
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
2)Tomcat 安裝
Copy tomcat to server
$ sudo tar xvzf apache-tomcat-7.0.35.tar.gz -C /opt
CATALINA_HOME=/opt/apache-tomcat-7.0.35
Reload .bashrc file as follows.
$ source ~/.bashrc
Now enable Tomcat by running the following startup script.
$ sudo $CATALINA_HOME/bin/startup.sh
2.4 apache 服務器配置
在Ubuntu上,mod_jk配置文件位於/etc/apache2/mods-enabled/jk.conf。更新以下:
<IfModule jk_module> # We need a workers file exactly once # and in the global server JkWorkersFile /etc/libapache2-mod-jk/workers.properties # JK error log # You can (and should) use rotatelogs here JkLogFile /var/log/apache2/mod_jk.log # JK log level (trace,debug,info,warn,error) JkLogLevel info JkShmFile /var/log/apache2/jk-runtime-status JkWatchdogInterval 60 JkMount /* loadbalancer JkMount /jk-status jkstatus # Configure access to jk-status and jk-manager # If you want to make this available in a virtual host, # either move this block into the virtual host # or copy it logically there by including "JkMountCopy On" # in the virtual host. # Add an appropriate authentication method here! <Location /jk-status> # Inside Location we can omit the URL in JkMount JkMount jk-status Order deny,allow Deny from all Allow from 127.0.0.1 </Location> <Location /jk-manager> # Inside Location we can omit the URL in JkMount JkMount jk-manager Order deny,allow Deny from all Allow from 127.0.0.1 </Location> </IfModule>
若是實在虛擬機下,還須要在下面/etc/apache2/sites-available/000-default.conf文件加入:
JkMount /* loadbalancer JkMount /jk-status jkstatus
使用下列內容,建立或編輯etc/libapache2-mod-jk/workers.properties:
worker.list=loadbalancer,jkstatus # 爲192.168.1.100配置Tomcat實例 worker.tomcat1.type=ajp13 worker.tomcat1.host=192.168.1.100 worker.tomcat1.port=8081 # worker「tomcat1」 worker.tomcat1.connection_pool_size=200 worker.tomcat1.connection_pool_timeout=600 # worker「tomcat1」將要求操做系統發送關於鏈接的KEEP-ALIVE信號。 worker.tomcat1.socket_keepalive=1 # 爲192.168.1.200配置Tomcat實例 worker.tomcat2.type=ajp13 worker.tomcat2.host=192.168.1.200 worker.tomcat2.port=8082 worker.tomcat2.connection_pool_size=200 worker.tomcat2.connection_pool_timeout=600 # worker「tomcat2」將要求操做系統發送關於鏈接的KEEP-ALIVE信號。 worker.tomcat2.socket_keepalive=1 worker.jkstatus.type=status worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=tomcat1,tomcat2
2.5.1 tomcat組播方式設置session store
Sudo vim /opt/apache-tomcat-7.0.30/conf/server.xml
<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.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="you ip address" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <InterceptorclassName="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"/> <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"> </Cluster>
tomcat集羣和負載均衡的實現(session同步)
因 爲tomcat的session同步功能須要用到組播,windows默認狀況下是開通組播服務的,可是linux默認狀況下並無開通。
能夠經過指令打 開
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
若是須要服務器啓動時即開通組播需在/etc/sysconfig/static-routes文件內加入
eht0 net 224.0.0.0 netmask 240.0.0.0
2.5.2 redis共享方式設置session store
redis已經有組件支持直接在tomcat7中設置下將redis做爲tomcat默認的session存儲器,下面介紹下配置過程
1.從http://redis.io/下載redis,按照redis服務端
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make
2.啓動redis
cd RedisDirectory/src ./redis-server --port 6379
3.從http://tomcat.apache.org/download-70.cgi下載tomcat7,按照tomcat7
4.從https://github.com/xetorthio/jedis/downloads下載jedis做爲java的redis客戶端,從https://github.com/jcoleman/tomcat-redis-session-manager/downloads下載tomcat的redis session manager插件,從http://commons.apache.org/proper/commons-pool/download_pool.cgi下載apache的common pool包,將這幾個jar包拷貝到tomcat7的lib目錄下
5.修改tomcat的conf下的context.xml文件,添加或者修改下面的配置
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /> <Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="localhost" <!-- optional: defaults to "localhost" --> port="6379" <!-- optional: defaults to "6379" --> database="0" <!-- optional: defaults to "0" --> maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />
6.重啓tomcat後就能夠看到session存儲到redis上了。