Apache httpd + Tomcat Simple cluster

Source: Internet
Author: User
Tags builtin server port tomcat server

The cluster is actually very simple, let's say what parts of the Httpd+tomcat cluster should be aware of:

First things to use have

apache-tomcat-8.0.32:http://tomcat.apache.org/download-80.cgi

httpd-2.4.18-x86-vc11-r3/apache24:http://httpd.apache.org/download.cgi Http://apache.opencas.org//httpd/binari Es/win32/http://www.apachehaus.com/cgi-bin/download.plx

tomcat-connectors-1.2.40-windows-i386-httpd-2.4.x/mod_jk.so : http://tomcat.apache.org/ download-connectors.cgi http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/

(tomcat-connectors-1.2.40-windows-x86_64-httpd-2.4.x/mod_jk.so) See if you are a 64-bit system or a 32-bit system, but also whether it is a Windows platform or a Linux platform

1, first if you decide to use the cluster, then your Web. XML must have <distributable/>

Below is one of the simplest web. xml:

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
Id= "webapp_id" version= "3.1" >
<distributable/>

<display-name>archetype Created Web application</display-name>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

2, configuring Tomcat

If you are booting multiple tomcat on a single machine, you must pay more attention to several points:

1)

<server port= "8006" shutdown= "shutdown" > shutdwon Server port, if you are booting multiple tomcat on a single machine, this port should be as different as possible.

<listener classname= "Org.apache.catalina.startup.VersionLoggerListener"/>

2)

<connector port= "8081" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>

Note that this protocol and this port, this is the port where you stand alone, after a single tomcat boot, to access Tomcat. namely: http://localhost:8081/webapplication/index.jsp.

Redirectport= "8443" This can be used for a while, Tomcat does not start the Ssprotocol protocol by default, if you start the conversation, you can find out more about Tomcat configuring HTTPS SSL TLS-related content, This port also needs to be different for each tomcat.

<!--
<connector port= "8443" protocol= "Org.apache.coyote.http11.Http11NioProtocol"
Maxthreads= "sslenabled=" true "scheme=" "https" secure= "true"
Clientauth= "false" sslprotocol= "TLS"/>
-

3)

<!--Define an AJP 1.3 Connector on port 8009-
<connector port= "8010" protocol= "ajp/1.3" redirectport= "8443"/>

This place must be noted, because Apache HTTPD and Tomcat communication links are using this port, if you are a single-player boot multiple Tomcat, this port will be different.

About ajp/1.3 This can be self-check, remember Ajp13. Because the rear apachehttpd will use this, and MOD_JK's configuration has a ajp13

4)

<!--should set Jvmroute to support load-balancing via AJP ie:
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "jvm1" >
-
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "Tomcat1" >

This depends on how each tomcat is different, regardless of whether a single machine starts multiple or multiple machines. This name should remember that this will correspond to the name of the worker in Apache httpd mod_jk and must be like!

5)

<!--
<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-
<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

This place annotation is released, this used Tomcat provides the default Simpletcpcluster, more detailed configuration see:

Http://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html

Http://tomcat.apache.org/tomcat-8.5-doc/index.html

With this tomcat server.xml configured, you can start with multiple tomcat and try Tomcat.

3, configure Apache httpd

About HTTPD installation not much to say, to find the \apache24 directory,

Put the prepared mod_jk.so into the Apache24\modules directory, in this directory you will see a lot of *.so files, these are the link library files, this directory, put in.

Note that and the version number, and the system, and how many bits of the system, and (httpd is x64 or x86) are closely related, do not choose the wrong mod_jk.so.

1) configuration, first to modify the httpd.conf, in the apache24\conf directory first back to find this file (about the APACHEHTTP directory structure, you can go down to learn more)

Add a line at the end of the httpd.conf:

# ADD MOD_JK for Tomcat load Blance
Include conf/extra/mod_jk.conf #这个文件现在当然不存在了 that you need to create.

The file section reads as follows:
# Secure (SSL/TLS) connections
# note:the Following must must be present to support
# starting without SSL on platforms with No/dev/random equivalent
# but a statically compiled-in mod_ssl.
#
<ifmodule ssl_module>
#Include conf/extra/httpd-ssl.conf
Include conf/extra/httpd-ahssl.conf
Sslrandomseed Startup Builtin
Sslrandomseed Connect Builtin
</IfModule>
<ifmodule http2_module>
Protocolshonororder on
Protocols H2 H2C http/1.1
</IfModule>

# ADD MOD_JK for Tomcat load Blance
Include conf/extra/mod_jk.conf

2) Create files by directory: conf/extra/mod_jk.conf

Complete Catalog: apache24\conf\extra\mod_jk.conf

What to write in the mod_jk.conf file, below is the simplest configuration can be used directly copy:

More detailed Configuration reference website: http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

#
# load MOD_JK for tomcat load Blance
#

#Load mod_jk Module (load mod_jk.so Library)
LoadModule jk_module modules/mod_jk.so
#Where to find Workers.properties (Workers configuration file, where Tomcat's cluster information is configured, this file does not exist now and needs to be created by itself)
Jkworkersfile conf/workers.properties
#Where to put JK Shared memory (Configure a shared file)
Jkshmfile Logs/httpd/mod_jk.shm
#Where to put JK Logs (config log file)
Jklogfile Logs/httpd/mod_jk.log
#Set the JK log level [Debug/error/info] (configure log levels)
Jkloglevel Debug
#Send Servlet for Context/examples to worker named Worker1

# (Configure some filters, like filters in the Web, which path files go to which worker, special attention to controller, this name can be random, But limited to the workers.properties file that will be created, remember that the name must be the same as the worker name in Worker.properties
#JkMount/servlet/* controller
#JkMount/servlets/* controller
#Send JSPs for context/examples to worker named Worker1
#JkMount/*.jsp controller
#JkMount/*.html controller
#JkMount/*.htm controller
Jkmount /* controller
#monitor the cluster Status (monitor the configuration of the cluster status as above, you can use Http://localhost/jkstatus to monitor the cluster status)
jkmount/jkstatus Status

3) Create Conf/workers.properties

Full path: apache24\conf\workers.properties

What is workers.properties going to write about? Information for the Tomcat cluster:

Detailed configuration and instructions refer to: http://tomcat.apache.org/connectors-doc/reference/workers.html

Example code that can be used directly with the copy

Worker.list=controller,status # # # #看到没, this is the controller

Worker.maintain=60

#========tomcat1========
WORKER.TOMCAT1.TYPE=AJP13 # # # #还记得开始要注意的jvmRoute吗? Remember the ajp13 to remember?
Worker.tomcat1.host=localhost # # # #这个是一个ip地址, if not the local IP directly to the remote machine
worker.tomcat1.port=8010 # # # #还记得开始让注意的 protocol= "ajp/1.3" port, that is it.
Worker.tomcat1.lbfactor=1 # # # #负载均衡的一个因数, similar to the weight of the same, do cluster load balancing use

#========tomcat2========
Worker.tomcat2.type=ajp13
Worker.tomcat2.host=localhost
worker.tomcat2.port=8011
Worker.tomcat2.lbfactor=1

#========tomcat3========
Worker.tomcat3.type=ajp13
Worker.tomcat3.host=localhost
worker.tomcat3.port=8012
Worker.tomcat3.lbfactor=1

#========controller, Load Balancer controller ========
WORKER.CONTROLLER.TYPE=LB # # # #负载均衡控制器, similar to a virtual worker,type must be lb
WORKER.CONTROLLER.BALANCED_WORKERS=TOMCAT1,TOMCAT2,TOMCAT3 # # #负载均衡集群中所有的tomcat都在这里了
Worker.controller.sticky_session=true # # # # # # # # # of sessions between Tomcat, using no sticky
Worker.controller.sticky_session_force=1

Worker.status.type=status # # # # # # The entire configuration, including the view of Tomcat status in the cluster.

All configurations are complete.

Now you can restart Tomcat and Apache httpd, and if your configuration is not a problem, you can now access your single tomcat and access to your Apache HTTPD's cluster portal.

A session test page is given below to test the session. Of course you have to make webapplication, because you need <distributable in Web. /> Remember?

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "java.util.*"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>cluster App test</title>
<body>

Server Info:
<%
Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br/>");
%>
<%
Out.println ("<br> ID" + session.getid () + "<br>");
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 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" method= "POST" >
Name: <input type=text size=20 name= "Dataname" > <br/>
Value: <input
Type=text size=20 name= "DataValue" > <br/>
<input type= "Submit" value= "Submit" >
</form>
</body>

Well, the article should be technical, about the cluster session replication, the beginning of the first.

In fact, there are many to think about, and then think of some websocket application, how to do, such as the use of WEBSOCKET+HTML5 on the page to draw, how to do this cluster?

Apache httpd + Tomcat Simple cluster

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.