在Web开发中,Session表示HTTP服务器与客户端(例如浏览器)的“会话”,每一个客户端会有其对应的Session保存在服务器端,一般用来保存和客户端关联的一些信息,例如是否登陆、购物车等。java
Session通常状况下是保存在服务器内存中。若是服务器重启,Session就会丢失。另外,若是是集群环境,Web应用部署在多台服务器上,Session若是保存在各自的服务器上,就没法共享了。mysql
针对这个问题,Jetty服务器提供了用于集群环境下的Session实现方式,即经过多台Jetty服务器链接到同一个Session数据库来实现Session共享。web
配置jetty.xml:sql
打开Jetty目录下的etc/jetty.xml,在Configure元素内部加入XML片断:mongodb
<Set name="sessionIdManager"> <New id="jdbcidmgr" class="org.eclipse.jetty.server.session.JDBCSessionIdManager"> <Arg> <Ref id="Server" /> </Arg> <Set name="workerName">fred</Set> <Call name="setDriverInfo"> <Arg>com.mysql.jdbc.Driver</Arg> <Arg>jdbc:mysql://192.168.20.1:3306/jetty_session?user=root&password=123</Arg> </Call> <Set name="scavengeInterval">60</Set> </New> </Set> <Call name="setAttribute"> <Arg>jdbcIdMgr</Arg> <Arg> <Ref id="jdbcidmgr" /> </Arg> </Call>
修改上面XML片断中的数据库链接的URL,若是用其余关系数据库,还要修改数据库驱动。要注意在XML中&符号要转义成&。数据库
配置context xml:浏览器
在Jetty的webapps目录下,新建一个XML文件,例如test.xml,这个XML用于配置一个web应用:服务器
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/test</Set> <Set name="war">D:\test.war</Set> <Ref name="Server" id="Server"> <Call id="jdbcIdMgr" name="getAttribute"> <Arg>jdbcIdMgr</Arg> </Call> </Ref> <Set name="sessionHandler"> <New class="org.eclipse.jetty.server.session.SessionHandler"> <Arg> <New id="jdbcmgr" class="org.eclipse.jetty.server.session.JDBCSessionManager"> <Set name="sessionIdManager"> <Ref id="jdbcIdMgr" /> </Set> </New> </Arg> </New> </Set> </Configure>
其中,<Set name="war">D:\test.war</Set>配置web应用的war包,<Set name="contextPath">/test</Set>配置web应用的contextPath,例如http://localhost:8080/test。session
启动Jetty测试:app
在启动Jetty以前,还须要把MySQL驱动jar包放在Jetty的lib/ext目录下。此外,还要建立好数据库,准备好war包。
一切就绪之后,就能够启动Jetty服务器测试Session是否已经保存在数据库中。
经过java -jar start.jar命令启动Jetty服务器,打开浏览器访问页面。能够看到在数据库中会生成两个表:jettysessionids、jettysessions,分别用于存储session id和session的信息。若是重启Jetty服务器,因为Session已经持久化到数据库中,因此Session不会丢失。
须要注意的是,因为Session保存的是Java对象,会经过Java的序列化写入数据库,也就是Session中的对象必须支持序列化和反序列化,即实现Serializable接口。
配置文件说明:
若是看不懂上面两段XML配置的话,这里作一个简单的说明。
上面的这些XML其实是Jetty的IOC配置文件,说到IOC首先会想到Spring框架,实际上Jetty的IOC和Spring的IOC解决的问题是相似的,只是XML的格式有些区别。
Jetty的IOC配置也很好理解,例如<Set name="contextPath">/test</Set>即调用setContextPath("/test"),<Call name="getAttribute"><Arg>jdbcIdMgr</Arg></Call>即调用getAttribute("jdbcIdMgr"),<New class="org.eclipse.jetty.server.session.SessionHandler"></New>即为Java实例化对象new SessionHandler()。
将上面的两段XML“翻译”成Java代码:
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.session.JDBCSessionIdManager; import org.eclipse.jetty.server.session.JDBCSessionManager; import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.webapp.WebAppContext; public class Main { public static void main(String[] args) throws Exception { Server server = new Server(8080); // 如下对应jetty.xml的配置 JDBCSessionIdManager sessionIdManager = new JDBCSessionIdManager(server); sessionIdManager.setWorkerName("fred"); sessionIdManager.setDriverInfo("com.mysql.jdbc.Driver", "jdbc:mysql://192.168.20.1:3306/jetty_session?user=root&password=123"); sessionIdManager.setScavengeInterval(60); server.setAttribute("jdbcIdMgr", sessionIdManager); // 如下对应context xml的配置 WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/test"); webapp.setWar("D:\\test.war"); JDBCSessionIdManager jdbcIdMgr = (JDBCSessionIdManager) server.getAttribute("jdbcIdMgr"); JDBCSessionManager sessionManager = new JDBCSessionManager(); sessionManager.setSessionIdManager(jdbcIdMgr); SessionHandler sessionHandler = new SessionHandler(sessionManager); webapp.setSessionHandler(sessionHandler); // 启动服务器 server.setHandler(webapp); server.start(); server.join(); } }
运行Java代码,一样能够启动服务器,将Session存入数据库,实现一样的效果。
配置jetty.xml:
打开etc/jetty.xml,在Configure元素内部加入XML片断:
<New id="mongodb" class="com.mongodb.MongoClient"> <Arg type="java.lang.String">192.168.20.1</Arg> <Arg type="int">27017</Arg> <Call name="getDB"> <Arg>jetty_session</Arg> <Call id="sessionDocument" name="getCollection"> <Arg>jetty_session_collection</Arg> </Call> </Call> </New> <Set name="sessionIdManager"> <New id="mongoIdMgr" class="org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager"> <Arg> <Ref id="Server" /> </Arg> <Arg> <Ref id="sessionDocument" /> </Arg> <Set name="workerName">fred</Set> <Set name="scavengePeriod">60</Set> </New> </Set> <Call name="setAttribute"> <Arg>mongoIdMgr</Arg> <Arg> <Ref id="mongoIdMgr" /> </Arg> </Call>
修改上面XML片断中的MongoDB的IP、端口号、数据库名、Collection名。
配置context xml:
在Jetty的webapps目录下,新建一个XML文件,例如test.xml,这个XML用于配置一个web应用:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext"> <Set name="contextPath">/test</Set> <Set name="war">D:\test.war</Set> <Ref name="Server" id="Server"> <Call id="mongoIdMgr" name="getSessionIdManager"/> </Ref> <Set name="sessionHandler"> <New class="org.eclipse.jetty.server.session.SessionHandler"> <Arg> <New id="mongoMgr" class="org.eclipse.jetty.nosql.mongodb.MongoSessionManager"> <Set name="sessionIdManager"> <Ref id="mongoIdMgr"/> </Set> </New> </Arg> </New> </Set> </Configure>
启动Jetty测试:
测试前,首先要吧两个jar包放在Jetty的lib/ext目录下,一个是MongoDB的驱动包,另外一个是jetty-nosql的jar包,在http://repo1.maven.org/maven2/org/eclipse/jetty/jetty-nosql/下载对应版本的jar包。此外,在MongoDB中建立好对应的数据库。
经过java -jar start.jar命令启动Jetty服务器,打开浏览器打开页面。能够看到在MongoDB中建立了配置的Collection并插入了数据:
“翻译”成Java代码:
将上面的两段Jetty的IOC配置文件转成Java代码,直接运行能够实现一样的功能:
import org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager; import org.eclipse.jetty.nosql.mongodb.MongoSessionManager; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.webapp.WebAppContext; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public class Main { public static void main(String[] args) throws Exception { Server server = new Server(8080); // 如下对应jetty.xml的配置 MongoClient mongoClient = new MongoClient("192.168.20.1", 27017); DB db = mongoClient.getDB("jetty_session"); DBCollection collection = db.getCollection("jetty_session_collection"); MongoSessionIdManager sessionIdManager = new MongoSessionIdManager(server, collection); sessionIdManager.setWorkerName("fred"); sessionIdManager.setScavengePeriod(60); server.setAttribute("mongoIdMgr", sessionIdManager); // 如下对应context xml的配置 WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/test"); webapp.setWar("D:\\test.war"); MongoSessionIdManager mongoIdMgr = (MongoSessionIdManager) server.getAttribute("mongoIdMgr"); MongoSessionManager sessionManager = new MongoSessionManager(); sessionManager.setSessionIdManager(mongoIdMgr); SessionHandler sessionHandler = new SessionHandler(sessionManager); webapp.setSessionHandler(sessionHandler); // 启动服务器 server.setHandler(webapp); server.start(); server.join(); } }