Tomcat cluster setup and implementation details

Source: Internet
Author: User
Tags create directory file upload pack sessions nginx server port number tomcat tomcat server
File Download Address:
https://download.csdn.net/download/rainyspring4540/10351699

background

The tomcat cluster is load balanced and has 2 types of build:

There are documents in the Tomcat installation package:

File:///D:/test/apache-tomcat-8.0-x32-8109/webapps/docs/balancer-howto.html

The first type:

Apache+tomcat+mod_jk

The second Kind

Apache+tomcat+mod_proxy

Target

Our application software Java Development Management system, Server TOMCAT8, database MySQL, including JDK are all 32-bit; to load balance A large number of Web requests, session sharing

Apache2.4 as a request forwarder, TOMCAT1,TOMCAT2 for load balancing, session sharing, one outage, business uptime

Target 1: Build Apache+tomcat cluster to achieve request load balancing

Target 2: Maintain session state data in the same conversation, using sticky session. (Here are the differences between sticky session and copy session, described below) preparation

Since the 64-bit situation is not considered, unification is Win32 machine configuration; jdk/tomcat8/apache2.4 are 32-bit

where JDK and tomcat8 download and installation no longer repeat;

Apache Unified Download Address:

Everything asked Baidu:

It's beautiful.

Select the latest Apache, click Download


(This figure is a post-fill)

The following page Red line font indicates that Apache official only provide source code, do not provide binary, big website is cattle. If you need a binary, you need a third-party address in the box below

I chose the Apache Lounge (Apache Haus inside I did not find the solution 1 used in the MOD_JK, so chose the Lounge)

!!!!!!! Above the Scarlet letter said: If not installed VC14 here will be an error. If your selves here, the best download is loaded (anyway I have installed)

The following is the mod_jk.so to build the scheme 1 o'clock (download zip, unzip the mod_jk.so that is)

Make sure you have V15 or V14 installed

As already mentioned above, here is just an emphasis on installing Apache2.4

After decompression as shown below:

It is recommended to copy the Apache24 directly to the C packing directory, as shown below:

Apache default even if the C packing directory,

Keywords: ServerAdmin admin@example.com This is the Apache administrator email address, change does not matter.

Key words: #ServerNamewww. example.com:80 This is the server address of Apache, this place must remove the front "#", Change "www.example.com" to "localhost" or 127.0.0.1.

Effect as shown:

If you put it here, the others do not have to change, using the default, directly run Bin/httpd.exe, do not close the window (a black window with nothing); open browser input localhost, it works!

Ok!!

Note 2 points:

1 if you double-click the Httpd.exe, you cannot keep the Black window state, that is, the seconds off or stop one will disappear. There is a problem with the conf/httpd.conf configuration

2 If you modify the path, can not take Chinese, as to the specific modification of the path of resolution, see below to modify the Apache path (path can not take Chinese ...) Pro-Test)

Since the Apache binary package is made by a third party, read the next third party's Readme.txt


Note that the directory is a/(forward slash); not a backslash \

Specific in:

Httpd.exe-k install Apache with default service name

Httpd.exe-k Uninstall Uninstall

Httpd.exe-k install-n "Apache24" to specify the service name of the installation

Httpd.exe-k uninstall-n "Apache24" uninstalls the Apache service with the specified name

net start Apache24 start Windows services

net stop Apache24 stop service Registration window service

Httpd–k install–npipewelding

Httpd–k uninstall–npipewelding

Httpd–k start–npipewelding

Httpd–k stop–npipewelding Scenario 1 using MOD_JK to build apache2.4, TOMCAT8 cluster The first step: Stop Apache services;

Step Two: Configure Mod_jk.so

Put the downloaded mod_jk.so into {apache_home}/modules/down

Modify httpd.conf to append the following sentence to the end

Include conf/mod_jk.conf

As shown in figure:

Step Three: Configure Mod_jk.conf

Http.conf the same directory, create the mod_jk.conf file with the following contents:

#加载 mod_jk.so Module

LoadModule Jk_module modules/mod_jk.so

#加载mod_jk. So configuration file

Jkworkersfile conf/workers.properties

#指定那些请求交给tomcat处理, "Controller" is the load distribution control # (e.g. JSP file: Jkmount/*.jspcontroller) specified in Workers.propertise

Jkmount/* Controller

Fourth Step: Configure Workers.properties

Http.conf the same directory, create the Workers.properties file with the following contents:

#===server List

Worker.list = Controller,tomcat1,tomcat2

#========tomcat1 need to note that this name corresponds to Tomcat in Server.xml<engine name= "Catalina" #defaultHost = "localhost" jvmroute= "TOMCAT1" > ========

#==ajp13 port number, server.xml configuration under Tomcat, default 8009

worker.tomcat1.port=8009

worker.tomcat1.host=192.168.0.21

Worker.tomcat1.type=ajp13

#server的加权比重, the higher the value, the more requests to be divided

Worker.tomcat1.lbfactor=1

#========tomcat2========

worker.tomcat2.port=8009

worker.tomcat2.host=192.168.0.22

Worker.tomcat2.type=ajp13

Worker.tomcat2.lbfactor=1

#======== Load Balancer controller========

Worker.controller.type=lb

#指定分担请求的tomcat

Worker.controller.balance_workers=tomcat1,tomcat2

Worker.controller.sticky_session=1

Fifth Step: Configure TOMCAT1

(That is, modify the Tomcat configuration file server.xml)

Open Tomcat1/conf/server.xml File

And then:

The content location is in the doc in the Tomcat installation package as follows:

Sixth step: Configure TOMCAT2

The same as the TOMCAT1 configuration, just

Jvmroute= "TOMCAT1" in the jvmroute= "TOMCAT2" Seventh step: Create a test project

Create catalog Test

Create directory test/web-inf

Create File test/index.jsp

Create File Test/web-inf/web.xml

Web. XML content:

<?xml version= "1.0" encoding= "UTF-8"?>

<web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

Xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee

Http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "

Version= "3.1"

Metadata-complete= "true" >

<display-name>test</display-name>

<!-clusters must-

<distributable/>

</web-app>

Index.jsp content:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtdxhtml 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<% @pageimport = "java.util.*"%>

<title>cluster Test-share session</title>

<meta http-equiv= "Pragma" content= "No-cache" >

<meta http-equiv= "Cache-control" content= "No-cache" >

<body>

<%

String SessionID = Session.getid ();

System.out.println ("Current SessionID =" + SessionID);

If there is a new Session property set

String dataname = Request.getparameter ("Dataname");

if (dataname! = null && dataname.length () > 0) {

String DataValue = Request.getparameter ("DataValue");

Session.setattribute (Dataname, DataValue);

}

OUT.PRINTLN ("<b>session attribute list </b><br>");

System.out.println ("============================");

Enumeration E = Session.getattributenames ();

while (E.hasmoreelements ()) {

String name = (string) e.nextelement ();

String value = Session.getattribute (name). toString ();

OUT.PRINTLN (name + "=" + value+ "<br>");

SYSTEM.OUT.PRINTLN (name + "=" + value);

}

%>

<form action= "test.jsp" id= "Form_add" method= "POST" >

Key value: <inputid= "Dataname" name= "Dataname" type= "text"/>

Value:<input id= "DataValue" name= "DataValue" type= "text"/>

<input id= "subbtn" name= "subbtn" type= "Submit" value= "Submit"/>

</form>

</body>

Eighth step: Build

Place Apache on the ip=192.168.0.20 machine (note the directory location when this is placed)

TOMCAT1 in 192.168.0.21 (Must have java_home to start)

TOMCAT2 in 192.168.0.22 (Must have java_home to start)

Start Apache first, start TOMCAT1, TOMCAT2

Open the browser on any machine, enter http://192.168.0.20/test/index.jsp nineth step: Test, verify

1 Load Balancing test:

Multiple browsers are recommended because multiple tabs for the same browser are shared by session sessions

I use IE11, Firefox, Ghrome, opera

For http://192.168.0.20/test/index.jsp, access to each of the 4 Browsers , through TOMCAT1, TOMCAT2 's black window log can see, 4 requests were apportioned, TOMCAT1 has 2 requests, TOMCAT2 has 2

2 Maintaining Session Sessions

Because we are the management system, it is necessary to require that in the same session, the user data, permissions information in the session must be able to maintain, that is, the user in the same session of the JSP all Ajax request information to be consistent

Using the Firefox test

Visit http://192.168.0.20/test/ Index.jsp, after adding a few session properties, found that the properties are listed, the information of the child shoes through the Tomcat log will also find that the same session request must be processed under the fixed tomcat, will not be assigned to other tomcat processing, it is clear that this mechanism will ensure that session information

3 A Tomcat outage, session can be maintained.

On the basis of 2, after being tested to service the TOMCAT1 of the same session, if the client browser continues to access, the data saved in the session disappears and the new data is recorded again.

Obviously, 2 and 3 meet the requirements of the sticky session (define the "difference: Sticky session and copy session" above)

Scenario 2 using Mod_proxy to build Apache2.4+tomcat8

(to be added)

about file synchronization when Tocmat clusters

First, file synchronization,

Second, second, disk sharing,

Third, third, Tomcat virtual path mapping.

The first two are handled by the operating system and other software.

The third is explained here.

There are two options for configuring the method, which resolves

1. Create an XML file in.%tomcat_home%\conf\catalina\localhost

<context path= "/web" docbase= "D:/myweb" crosscontext= "false" debug= "0"

Reloadable= "true" ></Context> recommended way

2. Open%tomcat_home%\conf\server.xml File

Add the following code between

debug= "0" reloadable= "true"/>

Adoption Scenario 1

Method 1 File synchronization

There are several ideas on the Internet:

{{

File upload can only be handled by the Tomcat server, including the creation of thumbnails, writing paths to the database, so can not use the Nginx file upload module;

The file needs to be directly accessible by the interface and the path needs to be written to the database

Several solutions are considered;

1. Adopt load Balancing strategy, file upload dynamic allocation to Tomcat server processing, database file path fill in the file path on the Nginx server, and then each server timed and Nginx server to do file synchronization, the final file download using Nginx direct processing;

2. Load balancing strategy, file upload dynamic allocation to the Tomcat server processing, the file path in the database to fill the Tomcat server file path, and add a special prefix (the only one for this Tomcat server); When the file is downloaded, nginx configures a prefix forwarding rule, Different prefixes are forwarded to the specific Tomcat server;

3. Using the load balancing strategy, file upload dynamic allocation to the Tomcat server processing, the file path in the database fill the file path on the Tomcat server, and then each tomcat server timing each other file synchronization, The final file is downloaded using nginx dynamic allocation to tomcat direct processing;

4. File upload Specifies the tomcat processing on the Nginx server, and the download uses nginx processing;

The pros and cons are not written for the time being, understand, later give it again; the final strategy is 4 (simplest)

}}

!!! Decide to use Method 4, using the same worker (Tomcat) as the URL for uploading and downloading files (except temporary), refer to Scenario 1-step three as Jkmount/* Controller to specify a specific URL

For example: Jkmount/* TOMCAT1 to specifically address specific URLs (and file storage-related) without load balancing

Of course, if the cluster and small, such as only 2 units, you can get a timer, let the file Tocmat start the timer, packaging all the files in the evening sync to other Tomcat

FAQ difference: Sticky session and copy session

Reprint Address:

http://blog.csdn.net/qq_17612199/article/details/52517603

There are two main ways to manage session in a Tomcat cluster:

Sticky session

Indicates that requests sent from the same window will be processed by the same tomcat in the cluster

Worker.lbcontroller.sticky_session=true

The advantage of sticky sessions is that it does not bounce back and forth on different Tomcat processing requests, but the downside is that if Tomcat crashes in the session, then the subsequent request will be processed by another tomcat, and the original session is invalidated and a new session is created again. This will throw a nullpointer access exception if you continue to take a value from the session.

Session Copy

Session replication refers to the use of Tomcat to each other through the multicast session to each Tomcat instance, if one of the access errors, the other Tomcat still has a valid session content, so that it can normally take over the session. The downside is that the amount of information sent by a group advertisement is staggering when there are many instances of Tomcat, or when the user has a large number of operations in the session. The session replication configuration is added in Web. XML in the published application

In addition, the JDK required for session replication must be JDK 5.0 and later. Apache Test start failure Solution

Failure as shown:

Workaround:

Original:

https://blog.csdn.net/su749520/article/details/78984773

That is, the lack of VC library, to install VC14, this Apache official website mentioned the necessary conditions. VC14 or VC15 must be installed, specifically refer to the following "Prerequisites for V14 Installation Success" V14 the necessary conditions for installation success

Only the following systems are supported:
This update applies to the following operating systems: Windows Server R2 Windows 8.1 Windows RT 8.1 Windows Server Windows 8 Windows RT Windows Server 20 R2 Service Pack 1 (SP1) Windows 7 SP1 Windows Server Service Pack 2 (SP2) Windows Vista SP2

Original:

Http://www.iszip.com/post/VC14setupfailed.html

V14 installation failed, i.e.

The reason for the installation failure is that your current decency does not conform to the system described above, and by opening "log Files", you will find

Look to the right.

Find this patch, double-click,

It turns out this patch installation failed and this update does not work for your computer.

We Baidu under this update why not installed ...

I look under my own machine, is Win7 Professional Edition, should be a system patch less SP1

Other methods:

To install this update, you must have April 2014 cumulative update windowsrt 8.1, Windows 8.1, and Windows Server R2 (2919355) installed in Windows 8.1 or WindowsServer 2 012 on R2.

Or, install WindowsServer R2 Service Pack 1 for Windows 7.

Or, install Service Pack 2 for WindowsVista and Windows Server 2008. Win7 SP1

The ultimate version of SP1 is fine.

But the professional version, no SP1, the test is not, Win7 upgrade SP1 patch

First update Win7 to the latest

Then install the SP1 patch pack: Windows6.1-kb976932-x64.exe

Download Address:

https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=5842

WIN2012R2

The solution is as follows:

Install patch KB2919442 Download the KB2919442 patch for x64-based Windows Server R2 now.

Download Address https://www.microsoft.com/zh-cn/download/details.aspx?id=42153

After the KB2919442 installation is complete, continue to install all patches for Windows Server R2 Update (KB2919355) as follows

Download address Download the WindowsServer R2 update package based on x64 now.

KB2919442 is a prerequisite for windowsserver, R2 Update and should be installed before attempting to installKB2919355

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.