Jetty cluster configuration session stored in MySQL, MongoDB

Source: Internet
Author: User
Tags mongoclient mongodb driver session id serialization server memory

In web development, the session represents the "sessions" of the HTTP server and the client (for example, the browser), each client has its corresponding session saved on the server side, usually used to hold some information associated with the client, such as whether to login, shopping cart, etc.

The session is typically saved in server memory. If the server restarts, the session will be lost. In addition, if a clustered environment, a Web application deployed on multiple servers, a user multiple requests may be handled by a different server, session if saved on the respective server, it can not be shared.

To solve this problem, the jetty server provides a session implementation mode for the cluster environment, that is, through multiple jetty servers connected to the same session database to achieve session sharing.

1. Configure session storage to relational database (MySQL for example):

Configuration jetty.xml:

Open Etc/jetty.xml and add an XML fragment inside the Configure element:

<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&amp;password=123</arg></ Call><set name= "Scavengeinterval" >60</set></new></set><call name= "SetAttribute" ><arg>jdbcidmgr</arg><arg><ref id= "Jdbcidmgr"/></arg></call>
Modify the URL of the database connection in the XML fragment above, and modify the database driver if you are using a different relational database. Be aware that in XML & symbols are escaped into &amp;.

To configure the context xml:

Under Jetty's WebApps directory, create a new XML file, such as Test.xml, which is used to configure a Web application:

<?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> 
Among them, <set name= "war" >D:\test.war</Set> Configure the war package for the Web app, <set name= "ContextPath" >/test</Set> Configure ContextPath for your web app, such as Http://localhost:8080/test.

To start the jetty test:

Before starting jetty, you also need to put the MySQL driver jar package in the Lib/ext directory. Also, create a good database and prepare the war package.

After everything is ready, you can start the Jetty Server test session is saved in the database.

Start the jetty server by Java-jar Start.jar command to open the browser access page. You can see that two tables are generated in the database: Jettysessionids, jettysessions, respectively, for storing session ID and session information. If the jetty server is restarted, the session is not lost because the session is persisted to the database.


It is important to note that because the session holds Java objects, it is written to the database via Java serialization, that is, the objects in the session must support serialization and deserialization, that is, implementing the Serializable interface .

Configuration file Description:

If you don't understand the above two XML configurations, here's a simple explanation.

The XML above is actually jetty's IOC profile, and when it comes to the IOC's first thought of the spring framework, the fact that the jetty IOC and the spring IOC solve the problem is similar, except that there are some differences in the format of XML.

Jetty's IOC configuration is also well understood, such as <set name= "ContextPath" >/test</Set> called Setcontextpath ("/test"), <call name= " GetAttribute "><Arg>jdbcIdMgr</Arg></Call> called GetAttribute (" Jdbcidmgr "), <new class=" Org.eclipse.jetty.server.session.SessionHandler "></New> is the Java instantiation Object New Sessionhandler ().

"Translate" the above two paragraphs of XML into Java code:

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);//The following configuration for 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);//The following corresponds to the configuration of the 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);// Start Server Server.sethandler (WebApp); Server.start (); Server.join ();}}

Running Java code, you can also start the server, the session into the database to achieve the same effect.

2. Configure session storage to MongoDB

Configuration jetty.xml:

Open Etc/jetty.xml and add an XML fragment inside the Configure element:

<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>

Modify the IP, port number, database name, and collection name of MongoDB in the XML fragment above.

To configure the context xml:

Under Jetty's WebApps directory, create a new XML file, such as Test.xml, which is used to configure a Web application:

<?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>
To start the jetty test:

Before testing, the first thing to do is to put two jar packages in the Lib/ext directory of jetty, one is MongoDB driver package, the other is Jetty-nosql jar package, in http://repo1.maven.org/maven2/org/ eclipse/jetty/jetty-nosql/download the corresponding version of the jar package. In addition, create the corresponding database in MongoDB.

Start the jetty server by Java-jar Start.jar command and open the browser open page. You can see that you have created the configured collection in MongoDB and inserted the data:


"Translate" into Java code:

By turning the IOC configuration file of the above two jetty into Java code, running directly can achieve the same functionality:

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);//The following configuration for jetty.xml mongoclient mongoclient = new Mo Ngoclient ("192.168.20.1", 27017);D b DB = Mongoclient.getdb ("jetty_session");D bcollection collection = Db.getcollection ("Jetty_session_collection"); Mongosessionidmanager SessionIDManager = new Mongosessionidmanager (server, collection); Sessionidmanager.setworkername ("Fred"); Sessionidmanager.setscavengeperiod, Server.setattribute ("MongoIdMgr" , sessionidmanager);//The following corresponds to the configuration of the 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);// Start Server Server.sethandler (WebApp); Server.start (); Server.join ();}}



Fork Brother reproduced please indicate the source: http://blog.csdn.net/xiao__gui/article/details/43271509



Jetty cluster configuration session stored in MySQL, MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.