Coherence(2)配置

来源:互联网 发布:2017淘宝双十一规则 编辑:程序博客网 时间:2024/04/25 07:54

前一篇文章主要简单介绍了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 应用、解决方案时,输出信息是特别有用的。

下面的例子输出 展示了通常发出的消息。

以上的输出表示了在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,并可以用在开发和生产环境。

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>


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

0 0