Java

ApacheのVirtualHostとResinのVirtualHostの組み合わせ。


クラスタリングとは異なり冗長性に欠けますが、ApacheのVirtualHostと同じように運用することによって、APサーバーの台数を減らせま す。
まぁ、1台のホストが落ちた場合、芋づる式に全部落ちるわけですが。
一応別々のプロセスに分けて起動するので、干渉はしません。

以下、参考までに。


[ Apache httpd.conf -VirtualHost- ]
----------------------
# im4
<VirtualHost *:80>
    ServerAdmin webmaster@giga-works.com
    DocumentRoot "/home/www/c4"
    ServerName im4.giga-works.com

    ErrorLog "/home/www/c4/logs/demo_error_log"
    CustomLog "/home/www/c4/logs/demo_access_log" common

    <Directory "/home/www/demo">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

    ResinConfigServer 192.168.102.156 6804
    CauchoConfigCacheDirectory /tmp
    CauchoStatus yes
</VirtualHost>
----------------------
必要台数分記述のこと。


ここからResin。

[root@c1 ~]# ls /usr/local/resin/
WEB-INF/  bin/  conf/  extra_conf/  keys/  lib/  libexec/  log/  logs/  php/  run/  webapps/

VirtualHostとして運用する台数分のconfファイルを用意して、
[root@c1 ~]# ls /usr/local/resin/extra_conf/
c1.conf  c2.conf  c3.conf  c4.conf

それぞれの該当箇所を変更。
[root@c1 ~]# cat /usr/local/resin/extra_conf/c4.conf

[ c4.conf変更箇所のみ ]
-----------------------
    <cluster>
      <!--
      - <srun server-id="" host="127.0.0.1" port="6802"/>
      -->
     <srun id="im4" host="192.168.102.156" port="6804" index="1"/>
    </cluster>

    <!-- configures the default host, matching any host name -->
    <host id="im4.giga-works.com" root-directory=".">
      <!--
         - configures an explicit root web-app matching the
         - webapp's ROOT
        -->

      <web-app id="/" document-directory="/home/www/c4/">
        <class-loader>
            <compiling-loader path='WEB-INF/classes' source='WEB-INF/src' batch='true'/>
            <library-loader path='WEB-INF/lib'/>
        </class-loader>
        <config-file>WEB-INF/web.xml</config-file>
      </web-app>
    </host>
[ EOF ]
-----------------------
<srun/>と<host/>でApacheのVirtualHost に対応させる。

c1.conf, c2.conf, c3.confは
<srun id="im-demo" host="192.168.102.156" port="6804" index="1"/>
<host id="im-demo.giga-works.com" root-directory=".">
<web-app id="/" document-directory="/home/www/c4/">
がそれぞれ違うだけ。


[ c4.conf全文 ]
-----------------------
<!--
   - Resin 3.0 configuration file.
  -->
<resin xmlns="http://caucho.com/ns/resin"
       xmlns:resin="http://caucho.com/ns/resin/core">
  <!--
     - Logging configuration for the JDK logging API.
    -->
  <log name="" path="stdout:" timestamp="[%H:%M:%S.%s] "/>

  <logger name="com.caucho.java" level="config"/>
  <logger name="com.caucho.loader" level="config"/>

  <!--
     - For production sites, change dependency-check-interval to something
     - like 600s, so it only checks for updates every 10 minutes.
    -->
  <dependency-check-interval>2s</dependency-check-interval>

  <!--
     - You can change the compiler to "javac" or jikes.
     - The default is "internal" only because it's the most
     - likely to be available.
    -->
  <javac compiler="internal" args=""/>

  <!-- Security providers.
     - <security-provider>
     -    com.sun.net.ssl.internal.ssl.Provider
     - </security-provider>
    -->

  <!--
     - If starting bin/resin as root on Unix, specify the user name
     - and group name for the web server user.
     -
     - <user-name>resin</user-name>
     - <group-name>resin</group-name>
    -->

  <!--
     - Configures threads shared among all HTTP and SRUN ports.
    -->
  <thread-pool>
    <!-- Maximum number of threads. -->
    <thread-max>1024</thread-max>

    <!-- Minimum number of spare connection threads. -->
    <spare-thread-min>10</spare-thread-min>
  </thread-pool>

  <!--
     - Configures the minimum free memory allowed before Resin
     - will force a restart.
    -->
  <min-free-memory>1M</min-free-memory>

  <server>
    <!-- adds all .jar files under the resin/lib directory -->
    <class-loader>
      <tree-loader path="${resin.home}/lib"/>
      <tree-loader path="${server.root}/lib"/>
    </class-loader>

    <!-- Configures the keepalive -->
    <keepalive-max>500</keepalive-max>
    <keepalive-timeout>120s</keepalive-timeout>

    <resin:if test="${resin.isProfessional()}">
      <select-manager enable="true"/>
    </resin:if>

    <!-- listen to the http ports only after the server has started. -->
    <bind-ports-after-start/>

    <!-- The http port -->
    <!--
    - <http server-id="" host="*" port="8080"/>
    -->

    <!--
      - SSL port configuration:
      -
      - <http port="8443">
      -   <openssl>
      -     <certificate-file>keys/gryffindor.crt</certificate-file>
      -     <certificate-key-file>keys/gryffindor.key</certificate-key-file>
      -     <password>test123</password>
      -   </openssl>
      - </http>
    -->
    <!--
    - <http port="443">
    -    <jsse-ssl>
    -        <key-store-type>jks</key-store-type>
    -        <key-store-file>keys/server.key</key-store-file>
    -        <password>password</password>
    -    </jsse-ssl>
    - </http>
    -->

    <!--
       - The local cluster, used for load balancing and distributed
       - backup.
      -->
    <cluster>
      <!--
      - <srun server-id="" host="127.0.0.1" port="6802"/>
      -->
     <srun id="im4" host="192.168.102.156" port="6804" index="1"/>
    </cluster>

    <!--
       - Configures the persistent store for single-server or clustered
       - in Resin professional.
      -->
    <resin:if test="${resin.isProfessional()}">
      <persistent-store type="cluster">
        <init path="session"/>
      </persistent-store>
    </resin:if>

    <!--
       - Enables/disables exceptions when the browser closes a connection.
      -->
    <ignore-client-disconnect>true</ignore-client-disconnect>

    <!--
       - For security, use a different cookie for SSL sessions.
       - <ssl-session-cookie>SSL_JSESSIONID</ssl-session-cookie>
      -->

    <!--
       - Enables the cache (available in Resin Professional)
      -->
    <resin:if test="${isResinProfessional}">
      <cache path="cache" memory-size="8M"/>
    </resin:if>

    <!--
       - Enables periodic checking of the server status.
       -
       - With JDK 1.5, this will ask the JDK to check for deadlocks.
       - All servers can add <url>s to be checked.
      -->
    <resin:if test="${isResinProfessional}">
      <ping>
        <!-- <url>http://localhost:8080/test-ping.jsp</url> -->
      </ping>
    </resin:if>

    <!--
       - Defaults applied to each web-app.
      -->
    <web-app-default>
      <!--
         - Extension library for common jar files.  The ext is safe
         - even for non-classloader aware jars.  The loaded classes
         - will be loaded separately for each web-app, i.e. the class
         - itself will be distinct.
        -->
      <class-loader>
        <tree-loader path="${server.root}/ext-webapp"/>
      </class-loader>

      <!--
         - Sets timeout values for cacheable pages, e.g. static pages.
        -->
      <cache-mapping url-pattern="/" expires="5s"/>
      <cache-mapping url-pattern="*.gif" expires="60s"/>
      <cache-mapping url-pattern="*.jpg" expires="60s"/>

      <!--
         - Servlet to use for directory display.
        -->
      <servlet servlet-name="directory"
              servlet-class="com.caucho.servlets.DirectoryServlet"/>

      <!--
         - Enable EL expressions in Servlet and Filter init-param
        -->
      <allow-servlet-el/>

      <!--
         - for security, disable session URLs by default.
        -->
      <session-config>
        <enable-url-rewriting>false</enable-url-rewriting>
      </session-config>

      <!--
         - For security, set the HttpOnly flag in cookies.
         - <cookie-http-only/>
        -->
    </web-app-default>

    <!--
       - Sample database pool configuration
       -
       - The JDBC name is java:comp/env/jdbc/test
         <database>
           <jndi-name>jdbc/mysql</jndi-name>
           <driver type="org.gjt.mm.mysql.Driver">
             <url>jdbc:mysql://localhost:3306/test</url>
             <user></user>
             <password></password>
            </driver>
            <prepared-statement-cache-size>8</prepared-statement-cache-size>
            <max-connections>20</max-connections>
            <max-idle-time>30s</max-idle-time>
          </database>
      -->
    <!--
       - Default host configuration applied to all virtual hosts.
      -->
    <host-default>
      <class-loader>
        <compiling-loader path="webapps/WEB-INF/classes"/>
        <library-loader path="webapps/WEB-INF/lib"/>
      </class-loader>

      <!--
         - With another web server, like Apache, this can be commented out
         - because the web server will log this information.
        -->
      <!--
      - <access-log path="logs/access.log"
      -      format='%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'
      -      rollover-period="1W"/>
      -->

      <access-log path="logs/access.log"
           format='%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"'
           rollover-size="-1"/>



      <!-- creates the webapps directory for .war expansion -->
      <web-app-deploy path="webapps"/>

      <!-- creates the deploy directory for .ear expansion -->
      <ear-deploy path="deploy">
        <ear-default>
          <!-- Configure this for the ejb server
             -
             - <ejb-server>
             -   <config-directory>WEB-INF</config-directory>
             -   <data-source>jdbc/test</data-source>
             - </ejb-server>
            -->
        </ear-default>
      </ear-deploy>

      <!-- creates the deploy directory for .rar expansion -->
      <resource-deploy path="deploy"/>

      <!-- creates a second deploy directory for .war expansion -->
      <web-app-deploy path="deploy"/>
    </host-default>

    <!-- includes the web-app-default for default web-app behavior -->
    <resin:import path="${resin.home}/conf/app-default.xml"/>

    <!-- configures a deployment directory for virtual hosts -->
    <host-deploy path="hosts">
      <host-default>
        <resin:import path="host.xml" optional="true"/>
      </host-default>
    </host-deploy>

    <!-- configures the default host, matching any host name -->
    <host id="im4.giga-works.com" root-directory=".">
      <!--
         - configures an explicit root web-app matching the
         - webapp's ROOT
        -->

      <web-app id="/" document-directory="/home/www/c4/">
        <class-loader>
            <compiling-loader path='WEB-INF/classes' source='WEB-INF/src' batch='true'/>
            <library-loader path='WEB-INF/lib'/>
        </class-loader>
        <config-file>WEB-INF/web.xml</config-file>
      </web-app>


      <resin:if test="${java.isJava5()}">
        <!--
           - Administration application /resin-admin
           -
           - password is the md5 hash of the password.
           - localhost is true to limit access to the localhost
          -->
        <resin:set var="resin_admin_password"  default=""/>
        <resin:set var="resin_admin_localhost" default="true"/>

        <web-app id="/resin-admin" document-directory="${resin.home}/php/admin"/>
       </resin:if>
    </host>
  </server>
</resin>
[EOF]
-----------------

Resinの起動用Script。
[root@c1 ~]# cat /etc/rc.d/init.d/resin
-----------------
#!/bin/sh
#
# Linux startup script for Resin
#
# chkconfig: 345 85 15
# description: Resin is a Java Web server.
# processname: wrapper.pl
#
# To install, configure this file as needed and copy init.resin
# to /etc/rc.d/init.d as resin.  Then use "# /sbin/chkconfig resin reset"
#
JAVA_HOME=/usr/local/jdk1.6.0
RESIN_HOME=/usr/local/resin
PID=$RESIN_HOME/run/resin.pid

class=com.caucho.server.http.HttpServer

#export CLASSPATH
export JAVA_HOME RESIN_HOME
#
# If you want to start the entire Resin process as a different user,
# set this to the user name.  If you need to bind to a protected port,
# e.g. port 80, you can't use USER, but will need to use bin/resin.
#
USER=
#
# You can change this to $RESIN_HOME/bin/resin if you need Resin to
# bind to port 80, but run as a different user.
#
EXE=$RESIN_HOME/bin/httpd.sh
#
# Sets the commandline arguments.
#
ARGS="-java_home ${JAVA_HOME} -resin_home ${RESIN_HOME}"

case "$1" in
  start)
        echo -n "Starting resin: "
        echo
        #if test -n "$USER"; then
        #  su $USER -c "$EXE -pid $PID start $ARGS"
        #else
        #  $EXE -pid $PID start $ARGS
        #fi
        $EXE -server im1 -conf $RESIN_HOME/extra_conf/c1.conf -pid $RESIN_HOME/run/im1.pid start $ARGS
        $EXE -server im2 -conf $RESIN_HOME/extra_conf/c2.conf -pid $RESIN_HOME/run/im2.pid start $ARGS
        $EXE -server im3 -conf $RESIN_HOME/extra_conf/c3.conf -pid $RESIN_HOME/run/im3.pid start $ARGS
        $EXE -server im4 -conf $RESIN_HOME/extra_conf/c4.conf -pid $RESIN_HOME/run/im4.pid start $ARGS
        echo
        ;;
  stop)
        echo -n "Shutting down resin: "
        echo
        #$EXE -pid $PID stop
        $EXE -server im1 -conf $RESIN_HOME/extra_conf/c1.conf -pid $RESIN_HOME/run/im1.pid stop
        $EXE -server im2 -conf $RESIN_HOME/extra_conf/c2.conf -pid $RESIN_HOME/run/im2.pid stop
        $EXE -server im3 -conf $RESIN_HOME/extra_conf/c3.conf -pid $RESIN_HOME/run/im3.pid stop
        $EXE -server im4 -conf $RESIN_HOME/extra_conf/c4.conf -pid $RESIN_HOME/run/im4.pid stop
        echo
        #rm -f $PID
        rm -f $RESIN_HOME/run/im1.pid
        rm -f $RESIN_HOME/run/im2.pid
        rm -f $RESIN_HOME/run/im3.pid
        rm -f $RESIN_HOME/run/im4.pid
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0
[EOF]
-----------------

ただ、メッチャメモリー食うので、それだけ注意。
まぁ1GBか2GBあれば足りるだろうけど、どのくらいの数を運用出来るかは...やってみないと分かりませんね...。

以上。

このページの先頭へ