Linux under Apache+tomcat load balancing and clustering

Source: Internet
Author: User

First of all, my environment.

An Ubuntu virtual machine,

An example of a apache2.2

Two instances of a tomcat1.7


1. Installing the Apache server

sudo apt-get install apache2
If you want to restart, run the command:

Sudo/etc/init.d/apache2 restart

Apache under Ubuntu will default to create the path/var/www,apache the default load, which is the load of the path below the

2. Installation of two Tomcat instances

Go to the official website to download a

And then at the local CP a bit,

At this point the directory structure is:

/HOME/HEHE/MY/SOFT/TOMCAT1,

/home/hehe/my/soft/tomcat2

3. Installing MOD_JK

MOD_JK is essentially a connector for Apache and Tomcat, and it provides a cluster and load Balancing the function.

The address is http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/x86_64/, Note that the version of Apache you are installing corresponds to.

4. Configure Apache

Ubuntu defaults to placing Apache's installation files under/etc/apache2

After entering this directory, create a new httpd.conf file

Rest assured that Apache will load this file when it is started.

Then compile the file. Add the words.

ServerName localhost:80    include mod_jk.conf

Then we create a new mod_jk.conf file under the current path

The first place to put mod_jk-1.2.31-httpd-2.2.x.so under the current path

#load module mod_jk-1.2.31-httpd-2.2.3.so is for Apache 2.2.x.  LoadModule jk_module/usr/lib/apache2/modules/mod_jk-1.2.31-httpd-2.2.x.so# This is used to configure the load Jkworkersfile workers.properties# This is the print log Jkloglevel debug# This is the name of the log Jklogfile mod_jk.log# the level of printing jkloglevel warn

So in the current path we have to create a new Mod_jk.log, file, which will log

Next Configure the Workers.properties file

Create a new Workers.properties file under the current path

#负载均衡器worker. List=lb_worker,worker_1,worker_2,jkstatus  #第一个tomcat的配置worker. Worker_1.host=localhost # Tomcat on which machine, if other machines, need to write the IP address of the other machine  worker.worker_1.port=8009      #端口, and so will be configured in Tomcat when the corresponding worker.worker_1. Type=ajp13     worker.worker_1.lbfactor=1     #加载因子 # The configuration of a second tomcat Worker.worker_2.host=localhost  worker.worker_2.port=9009  worker.worker_2.type=ajp13  worker.worker_2.lbfactor=1  #类型是一个负载均衡器worker. lb_worker.type=lb# retries three times worker.lb_worker.retries=3# load to TOMCAT1 and tomcat2worker.lb_worker.balance_workers=worker_1, worker_2# This is the sticky of the session, that is, whether the same session is submitted to the same tomcat, because later, the cluster will use a session to submit to two machines, so set to Falseworker.lb_  the state of Worker.sticky_session=false  WORKER.LB_WORKER.STICKY_SESSION_FORCE=FALSE#JK worker.jkstatus.type= Status
5. Configure the request to the load Balancer

There is a default file under/etc/apache2/sites-available,

In the last sentence of this document </VirtualHost> before adding such a paragraph

Jkmount/*.jsp Lb_workerjkmount/jkstatus Jkstatus
And then I handed it to Lb_worker.

6. Next Configure TOMCAT1

Change the conf below the Server.xml file, change the port, and the corresponding

   <connector port= "8009" protocol= "ajp/1.3" redirectport= "8443"/>

Then find this sentence, change it to this, pay attention to worker_1 and workers.properties correspondence

<engine name= "Catalina" defaulthost= "localhost" jvmroute= "worker_1" >

If the cluster, but also to change a sentence, the sentence before and after the comment symbol to remove, let go

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

Next Configure TOMCAT2

Change the following sentence first, the port number is changed to 8006

<server port= "8006" shutdown= "Shutdown" >

and change the following sentence.

    <connector port= "9082" protocol= "http/1.1"               connectiontimeout= "20000"               redirectport= "9443"/>

Change the conf below the Server.xml file, change the port, and the corresponding

   <connector port= "9009" protocol= "ajp/1.3" redirectport= "9443"/>

Then find this sentence, change it to this, pay attention to worker_2 and workers.properties correspondence

<engine name= "Catalina" defaulthost= "localhost" jvmroute= "worker_2" >

If the cluster, but also to change a sentence, the sentence before and after the comment symbol to remove, let go

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

7. Create a new project mytest, there is a file called wel.jsp,

The content of this JSP is:

<%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
and put the project under the WebApp of TOMCAT2.

And also in the TOMCAT1 directory.

Note that the following sentence is changed to this, 2 to 1

   System.out.println ("This is Tomcat Server nod 1");


    Out.println (REQUEST.GETLOCALADDR () + ":" + request.getlocalport () + "<br>this is Tomcat server nod 1");

8. Restart a apache,tomcat1,tomcat2

And then access it inside the browser.

http://127.0.0.1/mytest/wel.jsp

Constantly refresh the page, you will find it in Node1, and Node2 constantly change, indicating that the request is sometimes given to the 1, sometimes to the tomcat2, to achieve load balancing function


Below to implement the TOMCAT1, and the TOMCAT2 cluster function

The so-called cluster is that a client's corresponding session in two Tomcat has the same corresponding session

Inside the mytest, create a new wel2.jsp file.

The contents are as follows:

<%@ page contenttype= "text/html; Charset=utf-8 "%><%@ page import=" java.util.* "%>
   </form></body>
There's a file like this in two Tomcat.

Then add the following sentence to the two Tomcat Web. xml

<distributable/>, two sessions can be copied from one to the other.

Restart

Then visit http://10.211.55.3/mytest/wel2.jsp


Re-enter this address



You will find that you have actually given two servers, and the values in the session are identical!

The cluster is complete.




Linux under Apache+tomcat load balancing and clustering

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.