Thursday, 1 December 2011

Hosting multiple Jetty instances each with multiple ports!


I had a requirement to fulfill.
On the web tier I needed multiple instances of Jetty.
Each of these must have each webapp running on a different port.
And on the app tier the same.

So for example:
web tier:
  /opt/jetty91xx
    web-app-1 running on port 9101
    web-app-2 running on port 9102
    web-app-3 running on port 9103
  /opt/jetty92xx
    web-app-4 running on port 9201
    web-app-5 running on port 9202
    web-app-6 running on port 9203
app tier:
  /opt/jetty94xx
    web-app-7 running on port 9401
    web-app-8 running on port 9402
    web-app-9 running on port 9403
  /opt/jetty95xx
    web-app-10 running on port 9501
    web-app-11 running on port 9502
    web-app-12 running on port 9503
It's not as fiddly as that in reality, but the reason was to have sets of apps running in the same VM that are 'associated'.

So... How to do it?

Easy.
I'll use the jetty91xx example for the explanation below.

First go get the latest release of jetty and unpack it into /opt/jetty-latest or whatever.
Then create /opt/jetty91xx and copy the entire contents of /opt/jetty-latest into it effectively creating a clone.
So now we switch the /opt/jetty91xx.
(If you're on Mac OS-X you need to do a "mkdir work" in the folder to avoid OS-X /tmp/nasties)
Edit the etc/jetty.xml.
Part the way down is an <call name="addConnector">...</call>.
Comment this out.
Above it add this:
<set name="connectors">
  <array type="org.eclipse.jetty.server.Connector">
    <item>
      <new class="org.eclipse.jetty.server.nio.SelectChannelConnector" id="conn9101">
        <set name="port">9101</set>
        <set name="maxIdleTime">30000</set>
        <set name="Acceptors">1</set>
        <set name="name">conn9101</set>
      </new>
    </item>
    <item>
      <new class="org.eclipse.jetty.server.nio.SelectChannelConnector" id="conn9102">
        <set name="port">9102</set>
        <set name="maxIdleTime">30000</set>
        <set name="Acceptors">1</set>
        <set name="name">conn9102</set>
      </new>
    </item>
    <item>
      <new class="org.eclipse.jetty.server.nio.SelectChannelConnector" id="conn9103">
        <set name="port">9103</set>
        <set name="maxIdleTime">30000</set>
        <set name="Acceptors">1</set>
        <set name="name">conn9103</set>
      </new>
    </item>
  </array>
</set>
Ok. Your connectors are in and named.
Now go off and prepare your war files.
For simple example purposes I will assume you have port.9100.war, port.9101.war and port.9102.war.
Create contexts/port.9101.xml and put this in it:
<configure class="org.eclipse.jetty.webapp.WebAppContext">
  <set name="contextPath">/port.9101</set>
  <set name="war"><systemproperty default="." name="jetty.home">/webapps/port.9101.war</systemproperty></set>
  <set name="extractWAR">true</set>
  <set name="copyWebDir">false</set>
  <set name="defaultsDescriptor"><systemproperty default="." name="jetty.home">/etc/webdefault.xml</systemproperty></set>
  <set name="connectorNames"><array type="String"><item>conn9101</item></array></set>
</configure>
Create contexts/port.9102.xml and contexts/port.9103.xml in the same manner changing the contextPath, war name and connectorNames.

Now start the instance:
/usr/bin/java -Djetty.home=/opt/jetty91xx -Djava.io.tmpdir=/tmp \
  -Xms1024M -Xmx1024M -Xmn640M -Xss128k \
  -XX:ParallelGCThreads=4 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC \
  -XX:+CMSIncrementalMode -XX:SurvivorRatio=8 \
  -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31 \
  -Dcom.sun.management.jmxremote.port=9876 \
  -Dcom.sun.management.jmxremote.ssl=false \
  -Dcom.sun.management.jmxremote.authenticate=false \
  -DOPTIONS=server,security,servlet,xml,webapp,deploy,jmx,requestlog \
  -jar /opt/jetty91xx/start.jar \
  --pre=etc/jetty-logging.xml \
  --daemon
You will need to change -Djetty.home and the '-jar' option for each instance.
Also I've given a full path here with a bunch of options.
Normally you would use /etc/init.d/jetty91xx.sh start of course.
Tip: While trying this out, leave of the --daemon and you can stop and start jetty by using ^c.

Ok. You should be able to drop the 3 war files into the webapps folder now.
And then...
http://[whatever]:9101/port.9101
  http://[whatever]:9102/port.9102
  http://[whatever]:9103/port.9103
And they'll all be running in the same VM.

You can then create a new /opt/jetty92xx and start agin.

No comments:

Post a comment