Smileyou 2014-08-08
jetty-session-redis
JettysessionclusteringwithREDIS
REDISwebsite(testedonversion2.2.1)
JEDISwebsite(testedonversion1.5.2)
Jettywebsite(testedonversion8.0.0.M2)
Buildinstruction:
gitclonegit://github.com/Ovea/jetty-session-redis.git
cdjetty-session-redis
mvnpackage
Download
Alldownloadsarehere.TheywillalsobeavailableinMavencentralrepositoryandalsoinSonatypeOSSrepositoriessoon:
Snapshots-inSonatypeOSSRepository
Releases-inSonatypeOSSRepository
Releases-inMavenCentralRepository
Seethesectionbelowtoknowwhichpackageyouneed(thejarfileorthe-allbundle).
Installation
YouneedtoputinJetty'slib/extfolder:
jedis//https://github.com/downloads/xetorthio/jedis/jedis-2.1.0.jar
commons-pool//http://mirrors.cnnic.cn/apache//commons/pool/binaries/commons-pool-1.6-bin.tar.gz
andoneofthefollowingJAR:
jetty-session-redis-X.Y.jar(ifyouaregoingtoputallserializerdependenciesalsoasindependantjarfiles)
jetty-session-redis-X.Y-all.jar(containsalreadypackaged-relocatedserializers)
Istronglyrecommandyouusethejetty-session-redis-X.Y-all.jarbecausesomeserializers(likeJBossSerializer)havebeenimprovedforperformance.
Configuration
InJettyserverconfigurationfile(i.e.jetty.xml):
<?xmlversion="1.0"?>
<!DOCTYPEConfigurePUBLIC"-//Jetty//Configure//EN""http://www.eclipse.org/jetty/configure.dtd">
<Configureid="Server"class="org.eclipse.jetty.server.Server">
<!--
Configuresessionidmanagement
-->
<Setname="sessionIdManager">
<Newclass="com.ovea.jetty.session.redis.RedisSessionIdManager">
<Arg>
<Refid="Server"/>
</Arg>
<Arg>session/redis</Arg>
<!--timeintervaltocheckforexpiredsessionsinrediscache,inmilliseconds.Defaultsto1min-->
<Setname="scavengerInterval">30000</Set>
<!--clusternodename-->
<Setname="workerName">
<SystemPropertyname="jetty.node"default="node1"/>
</Set>
</New>
</Set>
<!--
ProvidesaRedisPoolforsessionmanagementonserverandeachwebapp
-->
<Newclass="org.eclipse.jetty.plus.jndi.Resource">
<Arg>session/redis</Arg>
<Arg>
<Newclass="redis.clients.jedis.JedisPool">
<Arg>
<Newclass="org.apache.commons.pool.impl.GenericObjectPool$Config">
<Settype="int"name="minIdle">5</Set>
<Settype="int"name="maxActive">15</Set>
<Settype="boolean"name="testOnBorrow">true</Set>
</New>
</Arg>
<Arg>127.0.0.1</Arg>
<Argtype="int">6379</Arg>
</New>
</Arg>
</New>
</Configure>
Ineachwebapplicationcontextfileusingsessionclustering(i.e.inWEB-INF/jetty-env.xml):
<Configureid="webappContext"class="org.eclipse.jetty.webapp.WebAppContext">
<Setname="contextPath">/webapp1</Set>
<Setname="sessionHandler">
<Newclass="org.eclipse.jetty.server.session.SessionHandler">
<Arg>
<Newclass="com.ovea.jetty.session.redis.RedisSessionManager">
<Arg>session/redis</Arg>
<Arg>
<Newclass="com.ovea.jetty.session.serializer.JsonSerializer"/>
</Arg>
<!--settheintervalinsecondstoforcesessionpersistenceeventifitdidn'tchanged.Defaultto60seconds-->
<Setname="saveInterval">20</Set>
<!--setthecookiedomain-->
<Setname="sessionDomain">127.0.0.1</Set>
<!--setthecookiepath-->
<Setname="sessionPath">/</Set>
<!--setthecookiemaxageinseconds.Defaultis-1(nomaxage).1day=86400seconds-->
<Setname="maxCookieAge">86400</Set>
<!--settheintervalinsecondstorefreshthecookiemaxage.Defaultto0.Thisnumbershouldbelowerthanthesessionexpiritytime.-->
<Setname="refreshCookieAge">300</Set>
</New>
</Arg>
</New>
</Set>
</Configure>
Note:Jetty'sdefaultformaxCookieAgeis-1andaspermytests,settingittoatooshortvaluemaycauseissuesinsessionretrieval.
Controllingsessionserialization
Bydefault,sessionattributesareserializedusingXStream,butthisisclearlytheworstserializerandyoumustmakesurethatyouconfiguretheserializeraccordingtoyourneeds.Ifyouhavesmallsessionswithsimpletypes,considertheJsonSerializer.Ifyouhavecomplexeobjectsbutallserializable,youcanconsidertheJbossSerializer.YoucanalsocreateyourownonesbytimplementingtheSerializerclassofaprovidedskeleton(seeexampleshere).
HereisthelistofprovidedSerializer:
com.ovea.jetty.session.serializer.JsonSerializer
com.ovea.jetty.session.serializer.JdkSerializer
com.ovea.jetty.session.serializer.XStreamSerializer
com.ovea.jetty.session.serializer.JBossSerializer
session/redis
Debugging
Ifyouneedtotroobleshootsomething,youcanputJettyinDEBUGmodeandseethetracesfromRedisSessionManagerandRedisSessionIdManager.
Also,withRedisyouhavetheabilitytomonitorallthecalls.Simplyissueinacommand-line:
redis-climonitor
ToseeallRedisrequestsgoingtotheRedisserver.IfyouareusingaStringserializersuchasXStreamofJson,you'llbeabletoseeallyoursessionattributesinto.
Versions&Compatibility
Thelatestrelease(>=2.1.ga)mustbeusedwithatleastthesedependencies:
com.ovea:jetty-session-redis:2.1.ga:serialjson
org.eclipse.jetty.aggregate:jetty-all:8.0.2.v20111006
org.mortbay.jetty:servlet-api:3.0.20100224
redis.clients:jedis:2.0.0
commons-pool:commons-pool:1.5.6
Authorsandhelp
MathieuCarboumail|blog|website
https://github.com/Ovea/jetty-session-redis