天天看點

Coherence(2)配置

前一篇文章主要簡單介紹了coherence的搭建和用戶端的使用,這章主要介紹coherence的配置檔案,以及如何配置相關參數.

在之前我們是通過jvm參數來指定一些coherence的初始化參數,更好的方法是使用配置檔案,也可以達到相同的效果。

  1. Operationalconfiguration

運作時配置是用于配置運作時參數,比如,日志,叢集,服務,權限以及授權資訊等

coherence預設附帶一個配置檔案:tangosol-coherence.xml解壓coherence.jar檔案可以找到它,它允許coherence能夠用最少的變化即用。這些檔案隻是為了示範的目的,也是為了能夠根據應用程式或解決方案的需求來重用或更改。然而,推薦的方法是提供一個配置檔案來覆寫預設的配置檔案。如果你有仔細觀察控制台資訊你會在第一行中看到

2013-11-25 09:17:11.530/0.657 Oracle Coherence 12.1.2.0.0 Loaded operational configuration from "jar:file:/home/yanglin/apps/Oracle/Middleware/Oracle_Home/coherence/lib/coherence.jar!/tangosol-coherence.xml"
           

如果你想修改相關參數,通常不直接地對該檔案進行操作,你可以重寫其中部門成員檔案,然後将其放在編譯路徑中,以下的檔案可以進行配置:

1:如果你想進行全局修改并且忽略運作模式,你可以使用angosol-coherence-override.

xml來進行修改相應的配置。

2:如果你想你的改變隻影響到開發環境,你可以修改配置tangosol-coherence-override-dev.xml檔案

3:如果你想你的修改隻适用于生産環境,你可以修改配置tangosol-coherence-override-prod.xml.  為了從這個檔案設定應用在生産伺服器上,你應該設定tangosol.coherence.mode系統屬性。 4:coherence-cache-config.xml 

    這個檔案用來指定在cluster中使用的各種各樣的緩存類型。

5:  coherence-pof-config.xml

當使用Portable Object Format(POF)來序列化對象時,這個檔案用來指定自定義的資料類型。這個檔案被代表性的稱為POF configuration deployment descriptor.這個檔案的架構是coherence-pof-config.xsd檔案。

6:Managementconfiguration files

一個用來配置coherence management reports的檔案集合。這些檔案在coherence.jar庫的/reports目錄裡。檔案包含了一組配置檔案的報告(report-group.xml。預設的),參考了許多的報告定義檔案。每個報告定義檔案的結果是建立一個報告檔案,該檔案顯示管理資訊基于特定的一組度量。 這些檔案的架構分coherence-report-config.xsd 和 coherence-report-group-config.xsd file.

你也可以建立以上所有的檔案,他們會按照順序生效。比如,如果你建立了 tangosol-coherence-override-dev.xml andtangosol-coherence-override.xml

他們将會覆寫原有的預設配置,同時後者将會被前者的配置項所取代。

1:使用預設的operational override file

使用預設的operational override file

将名為tangosol-coherence-override.xml的檔案放在運作環境的classpath中,預設的tangosol-coherence.xml檔案中設定的元素會被覆寫。覆寫檔案的結構和tangosol-coherence.xml檔案是一樣的,唯一不同的是tangosol-coherence-override.xml中的元素是可選的。覆寫檔案包含的隻是變換的元素。未配置的設定項會從tangosol-coherence.xml檔案加載。

eg:

1.建立一個檔案,名為tangosol-coherence-override.xml

2.編輯檔案,增加任意operational 元素用于覆寫原有設定項。

下面的例子,覆寫了預設的cluster名:

<?xml version='1.0'?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
   xsi:schemaLocation="http://xmlns.oracle.com/coherence/
   coherence-operational-config coherence-operational-config.xsd">
   <cluster-config>
      <member-identity>
         <cluster-name system-property="tangosol.coherence.cluster">
            MyCluster</cluster-name>
      </member-identity>
   </cluster-config>
</coherence>
           

2:指定一個Operational OverrideFile

tangosol.coherence.override系統屬性用來指定operational override file,取代預設的tangosol-coherence-override.xml檔案(也就是可以換成自定義的名稱的overrride檔案)。覆寫檔案的結構和operational deployment descriptor 是一樣的,除了,所有的元素是可選的。未配置的項會從tangosol-coherence.xml檔案加載。

tangosol.coherence.override系統屬性提供了一個簡單的方式在不同operational configuration之間切換,在開發和測試時候很友善。

指定operational override file:

1.建立一個文本。名字任意

2.編輯檔案,增加operational 元素,用來覆寫

下面的例子配置了多個端口号:

<?xml version='1.0'?>
<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
   xsi:schemaLocation="http://xmlns.oracle.com/coherence/
   coherence-operational-config coherence-operational-config.xsd">
   <cluster-config>
      <multicast-listener>
         <port system-property="tangosol.coherence.clusterport">3059</port>
      </multicast-listener>
   </cluster-config>
</coherence>
           

3.儲存檔案為XML格式。本例中吧檔案儲存在了COHERENCE_HOME目錄下.

4.指定一個operational override file的名字,作為tangosol.coherence.override系統屬性的值。如果檔案沒有在classpath裡,鍵入檔案的完整的路徑和名字。系統屬性頁支援使用URL,當指定operational override file的位置。

下面的例子在啟動cacheServer時指定先将COHERENCE_HOME目錄下面的cluster檔案拷貝到coherence.jar檔案中在啟動程式,這樣可以指定自己定義的operational override file

java -Dtangosol.coherence.override=cluster.xml -cp COHERENCE_HOME;COHERENCE_HOME\lib\coherence.jar com.tangosol.net.DefaultCacheServer

3:為指定operational元素定義override file

你可以為具體的一個元素指定一個override file.

1:拷貝一份前面例子的tangosol-coherence-override.xml檔案

2:加入

<?xmlversion='1.0'?>
<coherencexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
  xsi:schemaLocation="http://xmlns.oracle.com/coherence/
   coherence-operational-configcoherence-operational-config.xsd">
   <cluster-configxml-override="/cluster-config.xml">
   ...
   </cluster-config>
</coherence>
           

其中xml-override="/cluster-config.xml指定了自定義的元素檔案配置

3:建立cluster-config.xml檔案

<?xmlversion='1.0'?>
<cluster-configxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
  xsi:schemaLocation="http://xmlns.oracle.com/coherence/
   coherence-operational-configcoherence-operational-config.xsd">
   <multicast-listener>
     <join-timeout-milliseconds>4000</join-timeout-milliseconds>
   </multicast-listener>
</cluster-config>
           

4:確定以上檔案在運作環境中

4:檢視目前加載的opertional overridefiles

在啟動的時候,coherence節點的輸出訓示了位置和operational configuration files。opertionalconfiguration 消息是啟動時第一條發出的。在使用多個overrides file和當開發和測試coherence 應用、解決方案時,輸出資訊是特别有用的。

下面的例子輸出 展示了通常發出的消息。

Coherence(2)配置

以上的輸出表示了在conherence.jar中的operational deployment descriptor被加載了,這個檔案中設定被兩個加載了的override files覆寫了:tangosol-coherence-overide-dev.xml和tangosol-coherence-overrid.xml。此外,兩個overridefiles被定義了指定的operational 元素,但是在運作時沒有找到。

5.指定CacheConfiguration File

在cluster中,coherence-cache-config.xml  cache configuration deploymentdescriptor 檔案被用來指定使用各種緩存的類型。在運作時,coherence 使用在classpath中找到的第一個coherence-cache-config.xml檔案。一個預設的coherence-cache-config.xml檔案包含在了coherence中,可以再coherence.jar的根目錄下找到。這個簡單的檔案提供了示範的目的。能夠根據需求重用和改變。不過建議通過建立一個來覆寫預設配置

5.1使用預設的cache configuration file

   Coherence 被配置了即用配置,使用在classpath中找到的第一個coherence-cache-config.xml檔案。為了使用coherence-cache-config.xml,這個檔案必須在classpath中并且限于coherence.jar庫。否則,位于coherence.jar庫中的coherence-cache-config.xml将會被使用。

使用預設的cache configurationfile:

1.複制一份例子的coherence-cache-config.xml,位于conherence.jar中,儲存在一個不同的位置。緩存定義包含在例子檔案中,以便示範的目的,被用來作為建立一個指定解決方案的cache configuration的起點。

2.确定coherence-cache-config.xml的儲存位置在運作時是在classpath中,并且優先于classpath中的coherence.jar檔案。

下面的例子示範了使用位于Coherence_Home中的coherence-cache-config.xml cache configuration檔案來啟動緩存伺服器。

java -cp COHERENCE_HOME;COHERENCE_HOME\lib\coherence.jarcom.tangosol.net.DefaultCacheServer

5.2覆寫預設的cacheconfiguration file

    cache configuration deployment descriptor 的預設的名字和位置被指定在operational deployment descriptor(tangosol-coherence.xml)的<configurable-cache-factory-config>元素中。這個元素能夠使用不同位置的名字覆寫預設的cache configuration file.

覆寫預設的cache configuration file:      
1.複制一份coherence.jar庫中的預設的coherence-cache-config.xml cache configuration file,用一個不同的名字儲存在别的位置。      
2.建立一個         tangosol-coherence-override.xml              檔案。
           
3.              編輯opertional override file ,鍵入一個<configurable-cache-factory-config>                 節點,指定一個配置檔案的名字,在step1中建立的。如果cache configuration file沒有在classpath中,鍵入完整的路徑也可以。元素支援使用URL來定位一個cache configuration file.      
下面的例子指定了cache configuration deployment descriptor ,名為MyConfig.xml      
<?xml version='1.0'?>

<coherence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://xmlns.oracle.com/coherence/coherence-operational-config"
   xsi:schemaLocation="http://xmlns.oracle.com/coherence/
   coherence-operational-config coherence-operational-config.xsd">
   <configurable-cache-factory-config>
      <init-params>
         <init-param>
            <param-type>java.lang.String</param-type>
            <param-value system-property="tangosol.coherence.cacheconfig">
               MyConfig.xml</param-value>
         </init-param>
      </init-params>
   </configurable-cache-factory-config>
</coherence>
           
4.儲存關閉檔案。 5.確定operational override file 在運作時是在classpath中。 下面的例子示範了使用位于Coherence_Home 中的operational override file和一個自定義的cache configuration file 啟動cache server。
java -cp COHERENCE_HOME;COHERENCE_HOME\lib\coherence.jar com.tangosol.net.DefaultCacheServer      
通常,使用operational overridefile提供了大多的廣泛的方法來配置operational run time,并可以用在開發和生産環境。      
Coherence(2)配置
5.3  使用CacheConfiguration File系統屬性

	tangosol.coherence.cacheconfig系統屬性用來指定自定義的cache configuration deployment descriptor,替代預設的cache configuration deployment descriptor,系統屬性提供了一個簡單的方式切換不同配置檔案,友善開發和測試。

指定自定義的cache configuration file,鍵入檔案名作為tangosol.coherence.cacheconfig系統屬性的值。這是一個通常做法,當啟動一個Coherence節點時候,使用一個-D的參數。如果這個檔案不再classpath中,鍵入完整的路徑和檔案名。系統屬性頁支援使用一個URL來定位一個指定的cache configuration file.

下面的例子啟動了一個cache servier ,并且指定了名為MyConfig.xml的位于COHERENCE_HOME 的cache configuration deployment descriptor。

java -Dtangosol.coherence.cacheconfig=MyConfig.xml -cpCOHERENCE_HOME;COHERENCE_HOME\lib\coherence.jarcom.tangosol.net.DefaultCacheServer

5.4 檢視目前加載的Cache Confguration FIle

Coherence節點的輸出訓示了在啟動時, cache configuration deployment descriptor的位置和名字。這個配置資訊是緊跟着版權資訊輸出後的第一條消息。開發和測試時候,輸出是特别有用的。

下面的例子出書展示了cacheconfiguration 消息表明 名為MyConfig.xml的 cache configuration deployment descriptor 被加載。

Loaded cache configuration from resource"file:/D:/coherence/Myconfig.xml"

6.指定POFConguration File

    pof-config.xml 當使用Portable ObjectFormat (POF) 來序列化的時候,POF configuration deploymentdescriptor file 用來指定自定義的使用者類型,在運作時,Coherence 使用在classpath中找到的第一個pof-config.xml執行個體。

預設的POF configurationdeployment descriptor 被分布在coherence.jar庫中的的根目錄下。這個檔案是為部分應用和解決方案定制的、代替的或者是擴充的。預設情況下deployment descriptor 引用了coherence-pof-config.xml檔案。這是Coherence用來定義使用者類型的地方,并且當擴充或者建立一個POFconfiguration file時應該必須包含該檔案内容

6.1 使用POFCongifurationFile 系統屬性 

	tangosol.pof.config系統屬性用來指定使用自定義的POF configuration deployment descriptor ,代替預設的pof-config.xml。系統屬性提供了一個簡單的方式來切換不同的配置,友善開發和測試。和之前的系統屬性使用情況類似,你可以使用該屬性将pof預設檔案名稱進行改變:比如建立一個xml檔案名稱,如mypof.xml

<?xml version="1.0"?>
       
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
 xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config
   coherence-pof-config.xsd">
   <user-type-list>
      <include>coherence-pof-config.xml</include>
   </user-type-list>
</pof-config>
           
設定屬性為 java -Dtangosol.pof.config=MyPOF.xml -cp COHERENCE_HOME;COHERENCE_HOME\lib\coherence.jar com.tangosol.net.DefaultCacheServer 6.2合并多個POFconfiguration file <include> 元素在POFconfiguration deployment descriptor 中被使用,用來包含定義在不同POFconfiguration deployment descriptor中的使用者類型。這允許了用有意義的方式來組織使用者類型,如應用和開發的分組。 比如自己定義了hr-pof-config.xml和crm-pof-config.xml檔案,可以加入他們在pof cf檔案中
<?xml version='1.0'?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config coherence-pof-config.xsd">
   <user-type-list>
      <include>coherence-pof-config.xml</include>
      <include>hr-pof-config.xml</include>
      <include>crm-pof-config.xml</include>
   </user-type-list>
</pof-config>
           
java -cpCOHERENCE_HOME;COHERENCE_HOME\lib\coherence.jar com.tangosol.net.DefaultCacheServer 注:預設情況下控制台不會輸出load pof file的資訊.我們可以通過設定系統參數來使得效果更直覺: -Dtangosol.pof.config=my-pof-config.xml -Dtangosol.pof.enabled=true  也可以通過配置serializer為ConfigurablePofContext
<serializer>
      <instance>
  <class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
         <init-params>
            <init-param>
              <param-type>String</param-type>
               <param-value>my-pof-config.xml</param-value>
            </init-param>
         </init-params>
      </instance>
  </serializer>
           
Coherence(2)配置
7.指定ManagementConfiguration Files 有幾種不同的配置檔案用來配置管理。這些檔案包括: ·        report group configuration file  report groupconfiguration 檔案用來列出報告定義檔案和報告寫入的輸出目錄的名字和位置。這檔案的名字和位置定義在operational deployment descriptor.預設的,report-group.xml檔案被使用,位置在coherence.jar的/reports目錄。另外還提供了report group configuration 檔案和更具需要來建立自定義reportgroup file. ·        report configuration files  report configurationfile定義了報告,報告的結果在建立一個檔案更具一組特定的度量來顯示管理資訊。report configuration file必須在運作時被一個reportgroup configuration file來引用使用。預設的report configurationfile維護coherence.jar中的/reports檔案夾内,被預設的report group configuration file引用。自定義的reportconfiguration file能夠更具需要來建立。 ·        custom-mbeasn.xml 這個檔案是預設的MBean configurationoverride file,用來在Coherence JMX管理和監控架構中定義自定義的MBeans。這允許任何application -level MBean在cluster中的任何節點來管理和監控。自定義的MBeans 能夠在operational override file中定義。然後,MBeanconfiguration override file通常被替換。 補充:

日志:

預設情況下coherence使用的是它自己的日志輸出架構,你可以配置它成為你希望使用的日志架構,比如log4j等

比如你可以修改使用long4j,修改配置檔案中的lggoing-config配置項

<coherence>

<logging-config>

<destination>log4j</destination>

</logging-config>

</coherence>

你可能會希望日志架構适用于生産和開發環境,是以你應該吧上面的代碼複制到tangosol-coherence-override.xml同時確定該檔案和日志的property檔案在類編譯路徑下面。

log4j.logger.Coherence=DEBUG,CONSOLE, FILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.threshold=INFO

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=%m%n

log4j.appender.FILE=org.apache.log4j.RollingFileAppender

log4j.appender.FILE.File=logs/coherence.log

log4j.appender.FILE.MaxFileSize=10MB

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=%m%n

将多個節點的日志輸出到一個檔案中:?

配置一個名字叫做coherence 的記錄器

使用配置檔案配置私人的叢集環境

首先,因為我們想在開發環境中使用私人叢集,是以我們需要建立一個tangosol-coherence-override-dev.xml檔案

下一步取決于你想使用的是多點傳播還是wka

<coherencexml-override="/tangosol-coherence-override.xml">

<cluster-config>

<multicast-listener>

<time-to-live>0</time-to-live>

<join-timeout-milliseconds>1000</join-timeout-milliseconds>

</multicast-listener>

</cluster-config>

</coherence>

如果你使用wka,你可以進行如下配置:

<coherencexml-override="/tangosol-coherence-override.xml">

<cluster-config>

<unicast-listener>

<well-known-addresses>

<socket-addressid="1">

<address>localhost</address>

</socket-address>

</well-known-addresses>

</unicast-listener>

<multicast-listener>

<join-timeout-milliseconds>1000</join-timeout-milliseconds>

</multicast-listener>

</cluster-config>

</coherence>

注意:

這使用wka中,雖然我們沒有使用多點傳播,但是依然配置了join-timeout-milliseconds這是因為this settingstill controls how long the member will wait for evidence of an existingcluster before starting a new cluster and electing itself as a senior member,so you need to specify it in order to avoid a 30-second delay when starting thefirst member

Using the CoherenceAPI