华观科技
软件工匠,用心服务
Jetty集群配置Session存储到MySQL
更新日期: 2017年02月26日 17:42   来源: 本站   作者: 超级管理员   阅读: 1583

1、配置jetty.xml

 在Configure元素内部加入XML片段

<!--集群session配置开始-->
	<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://localhost:3306/jetty_session?user=root&amp;password=fsr123</Arg>
			</Call>
			<Set name="scavengeInterval">60</Set>
		</New>
	</Set>
	<Call name="setAttribute">
		<Arg>jdbcIdMgr</Arg>
		<Arg>
			<Ref id="jdbcidmgr" />
		</Arg>
	</Call>
	<!--集群session配置结束-->

2、配置context.xml

 在jetty的contexts目录下创建文件demo-v2.0.xml,配置如下

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<!-- ==================================================================
Configure and deploy the test web application in $(jetty.home)/webapps/test

Note. If this file did not exist or used a context path other that /test
then the default configuration of jetty.xml would discover the test
webapplication with a WebAppDeployer.  By specifying a context in this
directory, additional configuration may be specified and hot deployments 
detected.
===================================================================== -->

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/demo-v2.0</Set>
  <Set name="war">E:/deploy-webapps/demo-v2.0</Set>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Optional context configuration                                  -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="extractWAR">false</Set>
  <Set name="copyWebDir">false</Set>
  <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</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>

3、放置MySQL驱动程序

 在jetty的lib/ext目录放置驱动文件mysql-connector-java-5.1.24.jar


4、创建数据库jetty_session

 在mysql下新建数据库jetty_session


5、启动jetty

软件工匠  用心服务
联系方式
邮箱:farseersoft@163.com
地址:广州市天河区东莞庄一横路116号广东生产力大厦11层1101-1104
关注公众号
扫码访问微站