Network-Attached Memory(HAM)terracotta笔记(二)

Network-Attached Memory(HAM)terracotta笔记(二)

spring bean webapplication demo

1.starting the Terracotta Server
to start the server:
$TC_HOME/bin/start-tc-server.sh

to stop the server:
$TC_HOME/bin/stop-tc-server.sh

in my computer use:
/usr/local/terracotta-2.6.4/bin/start-tc-server.sh &
/usr/local/terracotta-2.6.4/bin/stop-tc-server.sh


2.config the web application to run under tomcat

my local windows server 放了一个bat启动文件到tomcat的bin路径下,文件start-tomcat1.bat
@echo off

setlocal
cd %~d0%~p0
set TC_INSTALL_DIR=..\..\..
set TC_INSTALL_DIR="%TC_INSTALL_DIR:"=%"

set CATALINA_HOME=%TC_INSTALL_DIR%\vendors\tomcat5.5

if not defined JAVA_HOME set JAVA_HOME="%TC_INSTALL_DIR%\jre"
set JAVA_HOME="%JAVA_HOME:"=%"

set TC_CONFIG_PATH=tc-config.xml
call %TC_INSTALL_DIR%\bin\dso-env.bat -q "%TC_CONFIG%"
set OPTS=%TC_JAVA_OPTS% -Dcom.sun.management.jmxremote
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.port=8091
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.authenticate=false
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.ssl=false
set JAVA_OPTS=%OPTS% %JAVA_OPTS%
set CATALINA_BASE=tomcat1

set JAVA_HOME=%JAVA_HOME:"=%
start "terracotta for easySearch sample: 8081" %CATALINA_HOME%\bin\catalina.bat run
endlocal

另外有个类似的start-tomcat2.bat
@echo off

setlocal
cd %~d0%~p0
set TC_INSTALL_DIR=..\..\..
set TC_INSTALL_DIR="%TC_INSTALL_DIR:"=%"

set CATALINA_HOME=%TC_INSTALL_DIR%\vendors\tomcat5.5

if not defined JAVA_HOME set JAVA_HOME="%TC_INSTALL_DIR%\jre"
set JAVA_HOME="%JAVA_HOME:"=%"

set TC_CONFIG_PATH=tc-config.xml
call %TC_INSTALL_DIR%\bin\dso-env.bat -q "%TC_CONFIG%"
set OPTS=%TC_JAVA_OPTS% -Dcom.sun.management.jmxremote
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.port=8092
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.authenticate=false
set OPTS=%OPTS% -Dcom.sun.management.jmxremote.ssl=false
set JAVA_OPTS=%OPTS% %JAVA_OPTS%
set CATALINA_BASE=tomcat2

set JAVA_HOME=%JAVA_HOME:"=%
start "terracotta for easySearch sample: 8082" %CATALINA_HOME%\bin\catalina.bat run
endlocal

在terracotta的安装文件夹下面D:\tool\Terracotta\terracotta-2.6.4\samples\easy\search
放置上面的两个文件,
同时新建文件夹tomcat1和tomcat2,两个文件夹的内容,参考terracotta的其他例子
另外将里面的配置文件做一个修改,指向自己的easySearch.war
D:\tool\Terracotta\terracotta-2.6.4\samples\easy\search\tomcat1\conf\Catalina\localhost
的easySearch.xml文件:
<Context path="/easySearch" docBase="D:/tool/Terracotta/terracotta-2.6.4/samples/easy/search/webapps/easySearch.war"
        debug="0" privileged="true">
</Context>

拷贝tc-config.xml文件到该目录
<?xml version="1.0" encoding="UTF-8"?>
<!--
All content copyright (c) 2003-2008 Terracotta, Inc.,
except as may otherwise be noted in a separate copyright notice.
All rights reserved.
-->
<tc:tc-config xmlns:tc="http://www.terracotta.org/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd">

<servers>
    <server name="www.sillycat.com" host="www.sillycat.com">
        <dso-port>9510</dso-port>
    <jmx-port>9520</jmx-port>
    <data>terracotta/server-data</data>
    <logs>terracotta/server-logs</logs>
    </server>
    <update-check>
      <enabled>true</enabled>
    </update-check>
</servers>

<clients>
    <logs>terracotta/client-logs/easy/easySearch/%D</logs>
    <statistics>terracotta/client-statistics/easy/easySearch/%D</statistics>
</clients>

<application>
    <spring>

      <jee-application name="easySearch">

        <application-contexts>
          <application-context>

            <paths>
              <path>*/applicationContext.xml</path>
            </paths>

            <beans>
              <bean name="clusterManager">
              <!--
              <non-distributed-field>analyzer</non-distributed-field>
              <non-distributed-field>indexPath</non-distributed-field>
              -->
              </bean>
            </beans>

          </application-context>
        </application-contexts>
 
 
   <instrumented-classes>
    <!--
       <include>
          <class-expression>org.apache.lucene.store.RAMDirectory</class-expression>
       </include>
       <include>
          <class-expression>org.apache.lucene.store.Directory</class-expression>
       </include>
       <include>
          <class-expression>org.apache.lucene.store.RAMFile</class-expression>
       </include>
       -->
     
   </instrumented-classes>
 
   <!--
   <locks>
   </locks>
   -->
 
   <transient-fields>
       <field-name>com.sillycat.api.commons.base.BaseManager.logger</field-name>
   </transient-fields>
 
</jee-application>
    </spring>

</application>
</tc:tc-config>

其中
<server name="www.sillycat.com" host="www.sillycat.com">
        <dso-port>9510</dso-port>
    <jmx-port>9520</jmx-port>
    <data>terracotta/server-data</data>
    <logs>terracotta/server-logs</logs>
    </server>
是指向了自己ubuntu下的服务,www.sillycat.com是在HOSTS中修改的映射向自己的UBUNTU的IP

配置文件中对applicationContext.xml的clusterManager类进行JVM cluster
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-lazy-init="false">
<bean id="clusterManager" class="com.sillycat.manager.impl.ClusterManagerImpl"/>
</beans>

ClusterManagerImpl类如下:
package com.sillycat.manager.impl;

import com.sillycat.api.commons.base.BaseManager;
import com.sillycat.manager.ClusterManager;

public class ClusterManagerImpl extends BaseManager implements ClusterManager {

private final Object lock = new Object();

private int count = 0;

public int getCount() {
   return count;
}

public void addCount() {
   synchronized (lock) {
    count++;
   }
}
}

简单示例了一个count,不论TOMCAT1还是TOMCAT2的addCount被触发,都会增加count

3.trouble shooting

find error here:
2008-10-07 17:35:06,000 [main] ERROR com.tc.object.bytecode.Manager - Exception thrown
com.tc.exception.TCNonPortableObjectError:
*******************************************************************************
Attempt to share an instance of a non-portable class referenced by a portable class. This
unshareable class has not been included for sharing in the configuration.

For more information on this issue, please visit our Troubleshooting Guide at:
http://terracotta.org/kit/troubleshooting

Referring class     : org.apache.log4j.spi.RootLogger
Referring field     : org.apache.log4j.Category.level
Thread              : main
JVM ID              : VM(5)
Non-included classes: org.apache.log4j.Level, org.apache.log4j.Priority

Action to take:

1) Reconfigure to include the unshareable classes
   * edit your tc-config.xml file
   * locate the <dso> element
   * add this snippet inside the <dso> element

       <instrumented-classes>
         <include>
           <class-expression>org.apache.log4j.Level</class-expression>
         </include>
         <include>
           <class-expression>org.apache.log4j.Priority</class-expression>
         </include>
       </instrumented-classes>

   * if there is already an <instrumented-classes> element present, simply add
     the new includes inside it

这个问题是因为在manager的基类里面我定义了一句
public Log logger = LogFactory.getLog(getClass());
这个logger其实是不需要做cluster的,所以修改了tc-config.xml配置文件,增加了
<transient-fields>
       <field-name>com.sillycat.api.commons.base.BaseManager.logger</field-name>
</transient-fields>
修复了这个问题

get another error message:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.tc.object.tx.UnlockedSharedObjectException:
*******************************************************************************
Attempt to access a shared object outside the scope of a shared lock.
All access to shared objects must be within the scope of one or more shared locks defined in your Terracotta configuration.
Please alter the locks section of your Terracotta configuration so that this access is auto-locked or protected by a named lock.

For more information on this issue, please visit our Troubleshooting Guide at:
http://terracotta.org/kit/troubleshooting


    Caused by Thread: http-8081-Processor4 in VM(2)
    Shared Object Type: com.sillycat.manager.impl.ClusterManagerImpl
*******************************************************************************

org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:408)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:360)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:78)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)

大致是没有加锁,所以在ClusterManagerImpl.java里面增加了
synchronized (lock) {}
这里还要仔细看看文档,不是很清楚。不过这个例子可以跑了。启动了TOMCAT1和TOMCAT2,不论在那个TOMCAT下调用了addCount方法,所有的TOMCAT的
getCount得到的都是增加了的值

你可能感兴趣的:(java,apache,tomcat,log4j,Lucene)