Write in front
Apache configures multiple Tomcat to enable request triage, multiple Tomcat services to equalize load, and increased service reliability. Recently studied, encountered a lot of problems, record, convenient for later review, do not like Apache,nginx can also be done.
Get ready
Version is very important, I was due to the version of the reason, has been error
Linux
Use # to lsb_release -a
view the current Linux system version.
This example:CentOS Linux release 7.0.1406 (Core)
Jdk
Use # to java -version
view the current version, if not, install it first. Download link
This example:java Version "1.7.0_80"
Tomcat
Check the decompression installation location. Download link
This example:apache-tomcat-7.0.69.tar.gz
Apache
In the Linux system, the Apache service main program is httpd, so the name is also httpd. Use # to httpd -version
view the current version. Here is the installation of download link using Yum
This example:apache/2.4.6 (CentOS)
Tomcat-connectors
Tomcat-connectors is a plug-in package for Apache and Tomcat, which is very important, and it takes a long time to find it, mainly to use its compiled mod_jk.so files. Mod_jk-1.2.31-httpd-2.2.x.so can be downloaded directly at Apache 2.2.x
Official:mod_jk-1.2.31-httpd-2.2.x.so is for Apache 2.2.x. It has been build against version 2.2.3, but should work with Apache 2.2.0 and later. Rename to mod_jk.so before putting it in your modules directory or adjust your LoadModule statement.,
But I am using Apache 2.4.6, when installing with Yum, the latest version is installed by default, # yum list httpd
This is the only one when querying.
So here we go to download the new tundish and need to compile it. Download link
This example:tomcat-connectors-1.2.42-src.tar.gz
Installation
Put these packages in a folder that I was placed /home
under. Here the main installation is Apache installation, I use Yum installation here:
- #
yum list httpd
#查看可安装版本列表
- #
yum install httpd
#默认安装
httpd -v
#安装结束, view the installation version
If an installation error occurs, you can view the Apache package, such as httpd.x86_64
yum list|grep httpd
Then delete the dependent dependency package. Note: Some of your configurations will also be retained after deletion.
yum erase httpd.x86_64
Using the service httpd start
start Apache service, the default port is 80, the direct access IP or domain name is seen on the page, indicating the startup success, failure to view service httpd status
the exception information.
Apache-tomcat-7.0.69.tar.gz, unzip can be
- #
tar zxvf /home/apache-tomcat-7.0.69.tar.gz
#解压至当前文件夹
- #
cp /home/apache-tomcat-7.0.69 /usr/local/
#拷贝到 the/usr/local/folder
- #
mv /usr/local/apache-tomcat-7.0.69 tomcat1
#修改文件夹名称为tomcat1
This example is more than one tomcat configuration an Apache so the above is done again, creating a second tomcat, named Tomcat2.
Tomcat-connectors compile and get the mod_jk.so file,
- #
tar zxvf /home/tomcat-connectors-1.2.42-src.tar.gz
- #
cd /home/tomcat-connectors-1.2.40-src/native
It is important to note that because the APXS tool under the Apache Bin package is required at compile time, as Yum follows, there will be no bin package, and you will need to download the Httpd-devel development package.
- #
rpm -qa|grep httpd
#查看httpd安装包列表
- #
yum -y install httpd-devel
#如果不存在就直接安装
- #
which apxs
#查询该工具位置, here I am/usr/sbin/apxs
Next Tomcat-connectors compile ...
[native]# ./configure --with-apxs=/usr/sbin/apxs
The position behind the #= is the APXS position installed above, note the need to operate in the /home/tomcat-connectors-1.2.40-src/native
directory.
[native]# make
#执行编译
After the compilation succeeds, locate the mod_jk.so file in/home/tomcat-connectors-1.2.40-src/native/apache-2.0/mod_jk.so and copy it to the Apache module package
- #
cp /home/tomcat-connectors-1.2.40-src/native/apache-2.0/mod_jk.so /etc/httpd/modules/
Use the full path, you can shorthand.
Configuration
Create two files and associate Apache with TOMCATX using the mod_jk.so plug-in package.
Go to Apache configuration file
- #
cd /etc/httpd/conf
#进入配置文件夹
- #
vi mod_jk.conf
#创建文件, shift+: => q => Enter
close
- #
vi workers.properties
#创建文件, shift+: => q => Enter
close
Open the mod_jk.conf file, and write
#加载 mod_jk.so file, placed on top LoadModule jk_module/etc/httpd/modules/mod_jk.so#加载 Tomcat parameter configuration file Jkworkersfile conf/workers.properties#日志保存文件JkLogFile/etc/httpd/logs/mod_jk2.log #日志等级JkLogLevel Infojklogstampformat "[%a %b %d %h:%M: %s %y] "jkoptions +forwardkeysize +ForwardURICompat- Forwarddirectoriesjkrequestlogformat "%w %v %t" ###### #请求拦截, Controller load Balancer name jkmount/servlet/* controllerjkmount/*.jsp controllerjkmount/*. do controller############## end #######################
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Open the workers.properties file, and write
#serverworker. List = Controller#========tomcat1========worker. tomcat1. port=11009#端口号与tomcat一致worker. tomcat1. host=localhostworker. tomcat1. type=ajp13worker. tomcat1. Lbfactor =1#========tomcat2========worker. tomcat2. port=12009#端口号与tomcat一致worker. tomcat2.host=localhostworker.tomcat2.type=ajp13 worker.tomcat21#========controller= Load Balancer Controller name =======worker .controller.type=lbworker .controller.balanced_workers=tomcat1,tomcat2 Span class= "Hljs-preprocessor" > #多个tomcat以 "," separating Worker.controller.sticky_session=falseworker.controller .sticky_session_force=1 #worker. controller.sticky_session=1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
Once the file association is created, you will need to join the Apache configuration file, open httpd.conf, and add the mod_jk.conf file.
- #
vi /etc/httpd/conf/httpd.conf
#打开文件.
- #
Include /etc/httpd/conf/mod_jk.conf
#添加该行, create an association.
Listen 80
You can also modify its access interface, which defaults to 80.
Now that you are connected, modify the tomcatx server.xml configuration file. Partially modified to:
...<!--<connector port= "8081" protocol= "Org.apache.coyote.http11.Http11NioProtocol" Connectiontim eout= "20000" uriencoding= "UTF-8" usebodyencodingforuri= "true" maxthreads= "768" Minsparethreads= "enablelookups=" false "redirectport=" 8443 "/>-<!--the request port number is the same as in the above Workers.properties file, including two--><< Span class= "Hljs-title" >connector port= "11009" protocol=" ajp/1.3 "redirectPort= "8443"/> ... <engine name= "Catalina" defaulthost= " localhost "jvmroute=" TOMCAT1 "; <cluster className=" Org.apache.catalina.ha.tcp.SimpleTcpCluster "/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Test
To see if this cluster configuration is configured successfully, you need to create a simple page of tests, placing it in the root that Tomcat can access. Named test.jsp
The test page content is:
<%@ page contenttype="Text/html; Charset=utf-8 "%><%@ page import="Java.util.*"%><Html><Head><title> Cluster App Test</Title></Head><Body> Server Info:<% Out.println (REQUEST.GETLOCALADDR () +" : " +Request.getlocalport () +"<br>");%><% Out.println ("<br> ID" + session.getid () +"<br>"); If there is a new Session property setString dataname=r Equest.getparameter ("Dataname");if (dataname!=n ull && 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);} %><Formaction="Test2.jsp"Method="POST" > Name:<InputType=TextSize=20 name= "DataName" > <br> value: <input Span class= "Hljs-attribute" >type=text size=20 name=" DataValue "> <br> < Input type=submit> </form> </body> </HTML>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
Launch Apache
- #
service httpd start
- #
service httpd status
#查看是否运行正常
Start Tomcatx
- #
cd /usr/local/tomcat{x}/bin
#打开bin目录
- #
./startup.sh
#启动
- #
tail -f ../logs/catalina.out
#查看打印console, ctrl + z
close the exit
Browser access:http://123.xx.xxx.xx/test.jsp
Page display: ID 7B8C934D14F3EF669C437E5B1B8123DB.TOMCAT1
Refresh: ID bbed560ec85bf46abb639883b9daf754.tomcat2
...
This completes the APACHE+TOMCAT load balancer configuration, and can be easily tested for concurrency through Apache apachebench .
- #
ab -n 4000 -c 1000 http://123.xx.xxx.xx/
#4000请求 + 1000 concurrent number + requested address, own server.
Some of the relevant data can be reflected, you can also open a Tomcat test, in the configuration of multiple tomcat, for comparison testing.
Summarize
In fact, it is not difficult, just not careful enough, all the software needs to be configured and associated, seize this point, clear ideas, problem solving methods are many.
The online material has been turned over, and I want to record some of the feedback.
Related references:
- http://www.iteye.com/topic/1017961
- Http://www.centoscn.com/CentosServer/www/2015/0417/5204.html
- Http://www.ha97.com/4617.html
Personal blog: http://www.abina.me/articles/2017/01/19/1484820029888.html
Apache+tomcat cluster configuration under Linux environment