2012年10月18日星期四

WLS_083:WebLogic Server高级管理之十二:与Coherence*Web集成

开发运行环境:WebLogic Server 12c开发版(12.1.1.1.0)+  Coherence 3.7.1.0 Build 27797

WebLogic Server与Coherence*Web集成之后有如下好处:
(1)Session对象将托管给Coherence Cache Server管理,你可以根据需要使用不同的缓存拓扑结构。
(2)Session对象不再与WLS Server共享同一个JVM,使WLS Server有更多的Heap空间。
(3)即使重启WLS Server,也不会造成Session丢失。
这样的分工可以让WLS和Coherence在各自擅长的领域做各自专业的事情。

1. session-cache-server.cmd
这是Coherence Cache Server,用于保存所有Session对象。脚本如下:
setlocal

set COHERENCE_HOME=C:\Oracle\coherence

set COH_OPTS=%COH_OPTS% -server -cp %COHERENCE_HOME%\lib\coherence.jar;%COHERENCE_HOME%\lib\coherence-web.jar;

set COH_OPTS=%COH_OPTS% -Dtangosol.coherence.management.remote=true -Dtangosol.coherence.cacheconfig=session-cache-config.xml -Dtangosol.coherence.distributed.localstorage=true -Dtangosol.coherence.clusterport=7777 -Dtangosol.coherence.clusteraddress=231.1.1.1 -Dtangosol.coherence.session.localstorage=true -Dtangosol.coherence.cluster=CoherenceCluster

java %COH_OPTS% -Xms512m -Xmx512m -XX:MaxPermSize=256m com.tangosol.net.DefaultCacheServer

:exit

其中,session-cache-config.xml在coherence-web.jar中,其内容如下:

<?xml version="1.0"?>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!--                                                                       -->
<!--      Cache configuration descriptor for Coherence*Web                 -->
<!--                                                                       -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
              xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config coherence-cache-config.xsd">

  <caching-scheme-mapping>
    <!--
    The clustered cache used to store Session management data.
    -->
    <cache-mapping>
      <cache-name>session-management</cache-name>
      <scheme-name>replicated</scheme-name>
    </cache-mapping>

    <!--
    The clustered cache used to store ServletContext attributes.
    -->
    <cache-mapping>
      <cache-name>servletcontext-storage</cache-name>
      <scheme-name>replicated</scheme-name>
    </cache-mapping>

    <!--
    The clustered cache used to store Session attributes.
    -->
    <cache-mapping>
      <cache-name>session-storage</cache-name>
      <scheme-name>session-near</scheme-name>
    </cache-mapping>

    <!--
    The clustered cache used to store the "overflowing" (split-out due to size)
    Session attributes. Only used for the "Split" model.
    -->
    <cache-mapping>
      <cache-name>session-overflow</cache-name>
      <scheme-name>session-distributed</scheme-name>
    </cache-mapping>

    <!--
    The clustered cache used to store IDs of "recently departed" Sessions.
    -->
    <cache-mapping>
      <cache-name>session-death-certificates</cache-name>
      <scheme-name>session-certificate</scheme-name>
    </cache-mapping>

    <!--
    The local cache used to store Sessions that are not yet distributed (if
    there is a distribution controller).
    -->
    <cache-mapping>
      <cache-name>local-session-storage</cache-name>
      <scheme-name>unlimited-local</scheme-name>
    </cache-mapping>

    <!--
    The local cache used to store Session attributes that are not distributed
    (if there is a distribution controller or attributes are allowed to become
    local when serialization fails).
    -->
    <cache-mapping>
      <cache-name>local-attribute-storage</cache-name>
      <scheme-name>unlimited-local</scheme-name>
    </cache-mapping>
  </caching-scheme-mapping>

  <caching-schemes>
    <!--
    Replicated caching scheme used by the Session management and ServletContext
    attribute caches.
    -->
    <replicated-scheme>
      <scheme-name>replicated</scheme-name>
      <service-name>ReplicatedSessionsMisc</service-name>
      <request-timeout>30s</request-timeout>
      <backing-map-scheme>
        <local-scheme>
          <scheme-ref>unlimited-local</scheme-ref>
        </local-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </replicated-scheme>

    <!--
    Near caching scheme used by the Session attribute cache. The front cache
    uses a Local caching scheme and the back cache uses a Distributed caching
    scheme.
    -->
    <near-scheme>
      <scheme-name>session-near</scheme-name>
      <front-scheme>
        <local-scheme>
          <scheme-ref>session-front</scheme-ref>
        </local-scheme>
      </front-scheme>
      <back-scheme>
        <distributed-scheme>
          <scheme-ref>session-distributed</scheme-ref>
        </distributed-scheme>
      </back-scheme>
      <invalidation-strategy>present</invalidation-strategy>
    </near-scheme>

    <local-scheme>
      <scheme-name>session-front</scheme-name>
      <eviction-policy>HYBRID</eviction-policy>
      <high-units>1000</high-units>
      <low-units>750</low-units>
    </local-scheme>

    <distributed-scheme>
      <scheme-name>session-distributed</scheme-name>
      <scheme-ref>session-base</scheme-ref>
      <backing-map-scheme>
        <local-scheme>
          <scheme-ref>unlimited-local</scheme-ref>
        </local-scheme>
        <!-- for disk overflow use this backing scheme instead:
        <overflow-scheme>
          <scheme-ref>session-paging</scheme-ref>
        </overflow-scheme>
        -->
      </backing-map-scheme>
    </distributed-scheme>

    <!--
    Distributed caching scheme used by the "recently departed" Session cache.
    -->
    <distributed-scheme>
      <scheme-name>session-certificate</scheme-name>
      <scheme-ref>session-base</scheme-ref>
      <backing-map-scheme>
        <local-scheme>
          <eviction-policy>HYBRID</eviction-policy>
          <high-units>4000</high-units>
          <low-units>3000</low-units>
          <expiry-delay>86400</expiry-delay>
        </local-scheme>
      </backing-map-scheme>
    </distributed-scheme>

    <!--
    "Base" Distributed caching scheme that defines common configuration.
    -->
    <distributed-scheme>
      <scheme-name>session-base</scheme-name>
      <service-name>DistributedSessions</service-name>
      <thread-count>0</thread-count>
      <lease-granularity>member</lease-granularity>
      <local-storage system-property="tangosol.coherence.session.localstorage">false</local-storage>
      <partition-count>257</partition-count>
      <backup-count>1</backup-count>
      <backup-storage>
        <type>on-heap</type>
      </backup-storage>
      <request-timeout>30s</request-timeout>
      <backing-map-scheme>
        <local-scheme>
          <scheme-ref>unlimited-local</scheme-ref>
        </local-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>

    <!--
    Disk-based Session attribute overflow caching scheme.
    -->
    <overflow-scheme>
      <scheme-name>session-paging</scheme-name>
      <front-scheme>
        <local-scheme>
          <scheme-ref>session-front</scheme-ref>
        </local-scheme>
      </front-scheme>
      <back-scheme>
        <external-scheme>
          <bdb-store-manager/>
        </external-scheme>
      </back-scheme>
    </overflow-scheme>

    <!--
    Local caching scheme definition used by all caches that do not require an
    eviction policy.
    -->
    <local-scheme>
      <scheme-name>unlimited-local</scheme-name>
      <service-name>LocalSessionCache</service-name>
    </local-scheme>

    <!--
    Clustered invocation service that manages sticky session ownership.
    -->
    <invocation-scheme>
      <service-name>SessionOwnership</service-name>
      <request-timeout>30s</request-timeout>
    </invocation-scheme>
  </caching-schemes>
</cache-config>

2. 在WebLogic Console中配置
(1)创建两个Managed Server:ServerA(端口8081)和ServerB(端口8083)
这里我并没有把ServerA和ServerB组成为一个集群,只是两个各自独立的Server。
(2)发布两个Shared Library到ServerA和ServerB上:coherence-web-spi.war和active-cache-1.0.jar。
其中coherence-web-spi.war位于[coherence_home]\lib目录下,active-cache-1.0.jar位于[wls_server]\common\deployable-libraries目录下。
(3)创建Coherence Cluster,Target到ServerA和ServerB上。


3. 创建应用:counter.war
(1)counter.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
    <body>
        <h3>
            Counter :
            <%
                Integer counter = new Integer(1);
                HttpSession httpsession = request.getSession(true);
                if (httpsession.isNew()) {
                    httpsession.setAttribute("count", counter);
                    out.println(counter);
                } else {
                    int count = ((Integer) httpsession.getAttribute("count")).
                    intValue();
                    httpsession.setAttribute("count", new Integer(++count));
                    out.println(count);
                }
            %>
        </h3>
    </body>
</html>
(2)weblogic.xml
<?xml version="1.0" encoding="UTF-8" ?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app http://www.oracle.com/technology/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd">
    <library-ref>
        <library-name>coherence-web-spi</library-name>
    </library-ref>
    <coherence-cluster-ref>
        <coherence-cluster-name>CoherenceCluster</coherence-cluster-name>
    </coherence-cluster-ref>
</weblogic-web-app>
(3)manifest.mf
Extension-List: active-cache
active-cache-Extension-Name: active-cache
active-cache-Specification-Version: 1.0
active-cache-Implementation-Version: 1.0
(4)复制coherence.jar到WEB-INF/lib目录下
(5)counter应用目录结构如下:
/
/counter.jsp
/META-INF/manifest.mf
/WEB-INF/web.xml
/WEB-INF/weblogic.xml
/WEB-INF/lib/coherence.jar
(6)发布counter.war到ServerA和ServerB上。

4. 测试
(1)运行session-cache-server.cmd,启动Coherence Cache Server。
(2)启动Admin Server
(3)启动Managed Server:ServerA和ServerB。
(4)启动counter应用
这时,在ServerA和ServerB的Console中会输出如下信息:
2012-10-18 15:39:09.907/108.408 Oracle Coherence 3.7.1.0 <Info> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', member=n
/a): Loaded operational configuration from "zip:C:/Oracle/wls1211_dev/user_projects/domains/dev_domain/servers/ServerA/tmp/_WL_user/counter/m4egcu/war/WEB-INF/l
ib/coherence.jar!/tangosol-coherence.xml"
2012-10-18 15:39:10.143/108.644 Oracle Coherence 3.7.1.0 <Info> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', member=n
/a): Loaded operational overrides from "zip:C:/Oracle/wls1211_dev/user_projects/domains/dev_domain/servers/ServerA/tmp/_WL_user/counter/m4egcu/war/WEB-INF/lib/c
oherence.jar!/tangosol-coherence-override-dev.xml"
2012-10-18 15:39:10.145/108.646 Oracle Coherence 3.7.1.0 <D5> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', member=n/a
): Optional configuration override "/tangosol-coherence-override.xml" is not specified
2012-10-18 15:39:10.213/108.714 Oracle Coherence 3.7.1.0 <D5> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', member=n/a
): Optional configuration override "/custom-mbeans.xml" is not specified

Oracle Coherence Version 3.7.1.0 Build 27797
 Grid Edition: Development mode
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

2012-10-18 15:39:11.114/109.615 Oracle Coherence GE 3.7.1.0 <Info> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', membe
r=n/a): Loaded cache configuration from "zip:C:/Oracle/wls1211_dev/user_projects/domains/dev_domain/servers/ServerA/tmp/_WL_user/coherence-web-spi/14iuoc/WEB-IN
F/lib/coherence-web.jar!/session-cache-config.xml"
2012-10-18 15:39:12.659/111.160 Oracle Coherence GE 3.7.1.0 <Info> (thread=[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)', membe
r=n/a): Loaded Reporter configuration from "zip:C:/Oracle/wls1211_dev/user_projects/domains/dev_domain/servers/ServerA/tmp/_WL_user/counter/m4egcu/war/WEB-INF/l
ib/coherence.jar!/reports/report-group.xml"
<Oct 18, 2012 3:39:14 PM CST> <Warning> <com.oracle.wls> <BEA-000000> <2012-10-18 15:39:14.624/113.125 Oracle Coherence GE 3.7.1.0 <Warning> (thread=Cluster, me
mber=n/a): This member is configured with a multicast TTL of 0; the senior Member(Id=1, Timestamp=2012-10-18 15:36:33.028, Address=10.191.10.83:8088, MachineId=
46446, Location=site:,machine:PMMA-CN,process:6384, Role=CoherenceServer) is configured with a TTL of 4. It is strongly recommended to use the same TTL setting
for all cluster members.>
<Oct 18, 2012 3:39:15 PM CST> <Warning> <WorkManager> <BEA-002919> <Unable to find a Work Manager with name wm/CoherenceWorkManager. Dispatch policy wm/Coherenc
eWorkManager will map to the default Work Manager for the application counter>
Oct 18, 2012 3:39:16 PM com.tangosol.coherence.servlet.AbstractHttpSessionCollection configure
INFO: Configured session model "SplitHttpSessionCollection":
  Clustered Session Cache Name=session-storage
  Local Session Cache Name=local-session-storage
  Local Session Attribute Cache Name=local-attribute-storage
  Death Certificate Cache Name=session-death-certificates
  SessionDistributionController Class Name=
  AttributeScopeController Class Name=com.tangosol.coherence.servlet.AbstractHttpSessionCollection$ApplicationScopeController
  Maximum Session Inactive Seconds=3600
  Session ID Character Length=52
  Session Locking Enforced=false
  Member Session Locking Enforced=false
  Application Session Locking Enforced=false
  Thread Session Locking Enforced=false
  Session Get Lock Timeout=300
  Suspect Attribute Detection=true
  Strict "Servlet Specification" Exception Handling=true
  Sticky Session Ownership=false
  Sticky Session Ownership Service Name=SessionOwnership
  Assume Session Locality for Reaping=false
  Parallel Session Reaping=true
  Allow Local Attributes=false
  Use Default Session ID Decoding=true
  Use Default Session ID Encoding=false
  Session ID Affinity Token=null
  Session Expiry Filter Factory=
  Session Access Debug Logging Enabled=false
  Session Access Debug Logging Filter=
Oct 18, 2012 3:39:16 PM com.tangosol.coherence.servlet.SessionHelper registerMBean
INFO: Registering MBean using object name "type=WebLogicHttpSessionManager,nodeId=2,appId=countercounter.war"

同时,在session-cache-server.cmd的Console会输出如下信息:
2012-10-18 15:39:14.624/164.404 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member(Id=2, Timestamp=2012-10-18 15:39:14.632, Address=10.191.10.8
3:8090, MachineId=46446, Location=site:,machine:PMMA-CN,process:7608, Role=WeblogicServer) joined Cluster with senior member 1
2012-10-18 15:39:14.749/164.529 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member 2 joined Service Management with senior member 1
2012-10-18 15:39:15.217/164.997 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member(Id=3, Timestamp=2012-10-18 15:39:15.014, Address=10.191.10.8
3:8092, MachineId=46446, Location=site:,machine:PMMA-CN,process:296, Role=WeblogicServer) joined Cluster with senior member 1
2012-10-18 15:39:15.326/165.106 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member 3 joined Service Management with senior member 1
2012-10-18 15:39:15.904/165.684 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member 3 joined Service DistributedSessions with senior member 1
2012-10-18 15:39:15.966/165.746 Oracle Coherence GE 3.7.1.0 <D5> (thread=Cluster, member=1): Member 2 joined Service DistributedSessions with senior member 1

如果看到如上信息说明ServerA和ServerB已经作为Client成员加入到Coherence集群中了。
(5)访问http://localhost:8081/counter/counter.jsp,显示Counter:1。刷新页面数字会跟着增长。
(6)访问http://localhost:8083/counter/counter.jsp,会接着显示下一个增长的数字。
这说明ServerA和ServerB上的counter应用的Session是共享的,尽管我没有把ServerA和ServerB组成一个集群。
(7)重启ServerA和ServerB。
(8)再次访问http://localhost:8081/counter/counter.jsp,会接着显示下一个增长的数字。
这说明Session对象是保留在Coherence Cache Server中的,所以即使停掉ServerA和ServerB,Session对象也不会丢失。

Project 下载:counter(coherence).war

参考文献:
1. Tutorial for Oracle Coherence Release 3.7.1 之 Caching Sessions with Coherence and WebLogic Server
2. http://blog.ipnweb.com/2012/02/member-specified-cluster-name-which-did.html

没有评论: