1 Apache+tomcat集群配置
1.1 分布式實現原理
如上圖所示,主要通過Apache-Server作為中轉服務器,實現多個tomcat服務器之間的分布式處理,用戶直接請求Apache-Server,然后Apache-Server會將請求分發到具體的tomcat-server,之后tomcat-server響應客戶請求并返回結果到Apache-Server,最后Apache-Server返回結果給用戶 |
1.2 負載均衡
文件說明: |
mod_jk.conf 主要定義mod_jk模塊的位置以及mod_jk模塊的連接日志設置,還有定義worker.properties文件的位置。 worker.properties 定義worker的參數,主要是連接tomcat主機的地址和端口信息。如果Tomcat與apache不在同一臺機器上,或者需要做多臺機器上tomcat的負載均衡只需要更改workers.properties文件中的相應定義即可。 %APACHE_HOME%為你的安裝目錄 |
環境說明: |
主要使用了一個Apache Server和兩個Tomcat,在同一臺電腦上進行測試。 |
1.3 準備軟件
Jdk1.6 |
下載地址:http://java.sun.com |
tomcat6.0.37 |
httpd-2.2.19-win64 |
mod_jk1.2.30_x64(主要作用是建立Apache Server與Tomcat之間的連接) |
下載地址:http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/ |
說明:apache-server安裝完成后,可以在瀏覽器中輸入http://localhost/來測試,如果出現” It works!”則表示安裝成功。 |
1.4 Windows下64位Apache服務器的安裝
首先需要說明的是,Apaceh服務器沒有官方的64位版本,只有民間的高手自己制作的。下面是具體的安裝步驟。 1. 在https://blackdot.be/download-archive/ 下載64位的Apache服務器。目前的版本是2.2.19,下載之后的壓縮包名為httpd-2.2.19-win64.rar; 2. 將該包解壓至一個目錄,如我的目錄為E:\Apache2.2.19; 3. 修改conf文件夾下的httpd.conf文件: 4. 執行安裝命令。通過命令行進入自己的安裝目錄下的bin文件夾,如E:\Apache2.2.19\bin,執行命令httpd.exe -k install,這樣就把apache加入了系統服務,默認服務名為Apache2.2。啟動服務的命令為httpd.exe -k start。 5. 測試安裝是否成功。開啟服務,運行Apache服務器后,打開瀏覽器輸入http://localhost:8080/,如果頁面顯示It works!,則表示安裝成功,Apache服務器可以正常使用。 要實現session復制,還需要在context.xml添加屬性distributable="true",如下: <Context distributable="true"/> 6. web.xml <distributable/>
|
1.5 修改tomcat端口號和安裝mod_jk連接模塊
1.5.1 修改tomcat8990配置文件server.xml
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="8079" shutdown="SHUTDOWN">
<!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <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>
<!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> -->
<!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <!--更改成80,則不用寫端口號--> <Connector port="8990" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8991" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
<!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8992" protocol="AJP/1.3" redirectPort="8991" />
<!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!—集群配置--> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> <!-- The request dumper valve dumps useful debugging information about the request and response data received and sent by Tomcat. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.RequestDumperValve"/> -->
<!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> --> <!-- E:\versionManager\sources\java\zj-utils\message_solr_1\WebRoot --> <!-- reloadable="true"--> <!--配置java-web項目與tomcat整合--> <!-- <Context path="/solr" docBase="E:\versionManager\sources\java\zj-web\zjsystem-searcher\zj-solr-app-4.7.1\WebRoot" reloadable="true" crossContext="true" override="true"> </Context>--> <!-- <Context path="/msg_solr" docBase="E:\versionManager\sources\java\zj-web\zjsystem-my\zj-msg-solr\WebRoot" debug="0" reloadable="true"> </Context> <Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr\service\solr" debug="0" reloadable="true"> <Environment name="solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr\home" override="true"/> </Context> --> <!--配置solr項目與tomcat整合path="/solr"寫成path="",則默認根目錄--> <!-- <Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr" reloadable="true"> <Environment name="/solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr4\home" override="true"/> </Context> -->
<!--<Context path="/iqc" docBase="e:\MyEclipse-8.6.1\iqc\WebRoot" crossContext="true" reloadable="false"/> <Context path="/iqc" docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\shanghai-bank-iqc\WebRoot" crossContext="true" reloadable="true"></Context>
--> <!--<Context path="/iqc" docBase="E:\MyEclipse8.6.1\iqc-ccb\WebRoot" crossContext="true" reloadable="false" />
<Context path="/solr" docBase="e:\solr\server" reloadable="false"> <Environment name="solr/home" type="java.lang.String" value="e:\solr\home" override="true"></Environment> </Context>-->
<!--<Context path="/iqc" docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\jianshe-bank-iqc\WebRoot" crossContext="true" reloadable="false"></Context>--> <!--<Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr" reloadable="false"> <Environment name="solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr4\home" override="true"/> </Context>--> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> -->
</Host> </Engine> </Service> </Server> |
1.5.2 修改tomcat8990配置文件context.xml
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <!--同步session--> <Context distributable="true">
<!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!--<Loader delegate="true" /> --> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> -->
<!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> -->
</Context> |
1.5.3 修改tomcat8999配置文件server.xml
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- Note: A "Server" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/server.html --> <Server port="8998" shutdown="SHUTDOWN">
<!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html --> <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <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>
<!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> -->
<!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <!--更改成80,則不用寫端口號--> <Connector port="8999" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8997" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
<!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8996" protocol="AJP/1.3" redirectPort="8997" />
<!-- An Engine represents the entry point (within Catalina) that processes every request. The Engine implementation for Tomcat stand alone analyzes the HTTP headers included with the request, and passes them on to the appropriate Host (virtual host). Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie : <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"> --> <Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference documentation) --> <!-- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> --> <!—集群配置--> <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> <!-- The request dumper valve dumps useful debugging information about the request and response data received and sent by Tomcat. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.RequestDumperValve"/> -->
<!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
<!-- Define the default virtual host Note: XML Schema validation will not work with Xerces 2.2. --> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> --> <!-- E:\versionManager\sources\java\zj-utils\message_solr_1\WebRoot --> <!-- reloadable="true"--> <!--配置java-web項目與tomcat整合--> <!-- <Context path="/solr" docBase="E:\versionManager\sources\java\zj-web\zjsystem-searcher\zj-solr-app-4.7.1\WebRoot" reloadable="true" crossContext="true" override="true"> </Context>--> <!-- <Context path="/msg_solr" docBase="E:\versionManager\sources\java\zj-web\zjsystem-my\zj-msg-solr\WebRoot" debug="0" reloadable="true"> </Context> <Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr\service\solr" debug="0" reloadable="true"> <Environment name="solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr\home" override="true"/> </Context> --> <!--配置solr項目與tomcat整合path="/solr"寫成path="",則默認根目錄--> <!-- <Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr" reloadable="true"> <Environment name="/solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr4\home" override="true"/> </Context> -->
<!--<Context path="/iqc" docBase="e:\MyEclipse-8.6.1\iqc\WebRoot" crossContext="true" reloadable="false"/> <Context path="/iqc" docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\shanghai-bank-iqc\WebRoot" crossContext="true" reloadable="true"></Context>
--> <!--<Context path="/iqc" docBase="E:\MyEclipse8.6.1\iqc-ccb\WebRoot" crossContext="true" reloadable="false" />
<Context path="/solr" docBase="e:\solr\server" reloadable="false"> <Environment name="solr/home" type="java.lang.String" value="e:\solr\home" override="true"></Environment> </Context>-->
<!--<Context path="/iqc" docBase="E:\versionManager\sources\java\zj-web\zjsystem-haoyi\jianshe-bank-iqc\WebRoot" crossContext="true" reloadable="false"></Context>--> <!--<Context path="/solr" docBase="E:\bigfile\services\java\apache-lucene-solr4\service\solr" reloadable="false"> <Environment name="solr/home" type="java.lang.String" value="E:\bigfile\services\java\apache-lucene-solr4\home" override="true"/> </Context>--> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->
<!-- Access log processes all example. Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> -->
</Host> </Engine> </Service> </Server> |
1.5.4 修改tomcat8999配置文件context.xml
<?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <!--同步session--> <Context distributable="true">
<!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!--<Loader delegate="true" /> --> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> -->
<!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> -->
</Context> |
1.5.5 創建文件
安裝好Jdk、tomcat、apache后,加入mod_jk連接模塊,把mod_jk1.2.30_x64\modules\ mod_jk.so文件拷貝到%APACHE_HOME%\modules下,把jk模塊的配置放到單獨的文件中來,在%APACHE_HOME%\conf目錄新建workers.properties文件。 |
1.5.6 在httpd.conf最后加上
#加載mod_jk Module LoadModule jk_module modules/mod_jk.so #指定 workers.properties文件路徑 JkWorkersFile conf/workers.properties #指定那些請求交給tomcat處理,"controller"為在workers.propertise里指定的負載分配控制器 JkMount /*.jsp controller |
創建mod_jk.conf文件,也可用Include conf/mod_jk.conf包含 |
1.5.7 修改workers.properties文件
#server 列表 worker.list = controller,tomcat1,tomcat2 #========tomcat1======== #ajp13 端口號,在tomcat下server.xml配置,默認8009 worker.tomcat1.port=8992 #tomcat的主機地址,如不為本機,請填寫ip地址 worker.tomcat1.host=localhost worker.tomcat1.type=ajp13 #server的加權比重,值越高,分得的請求越多 worker.tomcat1.lbfactor=1 #========tomcat2======== #ajp13 端口號,在tomcat下server.xml配置,默認8009 worker.tomcat2.port=8996 #tomcat的主機地址,如不為本機,請填寫ip地址 worker.tomcat2.host= localhost worker.tomcat2.type=ajp13 #server的加權比重,值越高,分得的請求越多 worker.tomcat2.lbfactor=1
#========controller,負載均衡控制器======== worker.controller.type=lb #指定分擔請求的tomcat worker.controller.balanced_workers=tomcat1,tomcat2 worker.controller.sticky_session=1 |
1.6 編寫一個測試頁面testsession.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Object mark = session.getAttribute("mark"); if (mark==null){ session.setAttribute("mark",new Date()); } System.out.println("hello:"+mark); %> |
控制臺打印端口號(8990和8999) |
hello hello:Thu Aug 13 00:48:18 CST 2015 |
2 Ehcache分布式緩存
2.1 手動進行成員發現
進行手動成員配置要知道每個監聽器的IP地址和端口。成員不能在運行時動態地添加 和移除。在技術上很難使用廣播的情況下就可以手動成員發現,例如在集群的服務器之間有 一個不能傳送廣播報文的路由器。你也可以用手動成員發現進行單向的數據復制,只讓 server2知道server1而server1不知道server |
配置手動成員發現,需要指定ehcache配置文件中cacheManagerPeerProviderFactory的 properties屬性,像下面這樣: peerDiscovery=manual rmiUrls=//server:port/cacheName, ... rmiUrls配置的是服務器cache peers的列表。注意不要重復配置 |
Server1節點IP:localhost:8990 |
Server2節點IP:localhost:8999 |
2.2 示例
假設你在集群中有兩臺服務器。你要同步sampleCache1和sampleCache2。下面是每個 服務器需要的配置 |
2.2.1 配置server1的ehcache.xml
#各節點對應不同的ehcache.xml文件 cons.cache.file.path=E:/bigfile/services/java/cluster/cache-files/8990/ehcache.xml |
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" dynamicConfig="false"> <diskStore path="E:/bigfile/services/java/cluster/cache-files/8990/cache" /> <!-- <diskStore path="java.io.tmpdir" /> --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls=//localhost:40002/zj.page.User|//localhost:40002/userCache" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=localhost,port=40001, socketTimeoutMillis=3000" /> <defaultCache maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0" /> <cache name="zj.page.User" diskPersistent="true" maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=false, replicatePuts=true, replicatePutsViaCopy=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> <cache name="userCache" diskPersistent="true" maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=false, replicatePuts=true, replicatePutsViaCopy=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> </ehcache> |
2.2.2 配置server2的ehcache.xml
#各節點對應不同的ehcache.xml文件 cons.cache.file.path=E:/bigfile/services/java/cluster/cache-files/8999/ehcache.xml |
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" dynamicConfig="false"> <diskStore path="E:/bigfile/services/java/cluster/cache-files/8999/cache" /> <!-- <diskStore path="java.io.tmpdir" /> --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual, rmiUrls=//localhost:40001/zj.page.User|//localhost:40001/userCache" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=localhost,port=40002, socketTimeoutMillis=3000" /> <defaultCache maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0" /> <cache name="zj.page.User" diskPersistent="true" maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=false, replicatePuts=true, replicatePutsViaCopy=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> <cache name="userCache" diskPersistent="true" maxElementsInMemory="10000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="true" diskExpiryThreadIntervalSeconds="0"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=false, replicatePuts=true, replicatePutsViaCopy=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" /> </cache> </ehcache> |
2.2.3 不同地址訪問頁面效果如下
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://m.eyofj.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
