Make a demo and record it. 1. Environment Preparation
Required Software: Jdk/jre Apache httpd2.4 (with 2.2 have a lot of different points) Tomcat8 several
Tips
Tomcat and Apache httpd just right I have a blog to introduce how to install;
Installing, configuring, and adding services to Apache in Linux
Tomcat-set for service in Windows & Linux
Tomcat this needs a little bit more, the multiple tomcat configuration is actually a free port configured for every module in tomcat that needs ports. There are a few places: Server port: <server port= "8005" shutdown= "Shutdown" > HTTP Protocol Connector port: <connector port= "8080" protocol= "http/ 1.1 "connectiontimeout=" 20000 "redirectport=" 8443 "uriencoding=" UTF-8 "/> AJP Protocol Connector port: <connector port=" 8009 " Protocol= "ajp/1.3" redirectport= "8443"/>
JDK/JRE installation is very simple, Oracle official website is directly downloaded binary files, under pressure configuration environment variable is good (another blog JAVA Why to set environment variables, java_home) 2, the expected target
Receive all requests using 1 HTTPD servers;
Install 2 tomcat to form a cluster;
Configure the HTTPD server to do reverse proxy, distribute the request evenly on request (byrequests) to each Tomcat, thus distributing the pressure to achieve load balancing-loadbalance; 3. Steps
After the "Environment preparation" link, here our software should be installed, the status of the installed I described as follows: The browser address bar input server domain/IP should be able to see: It works. Linux boot httpd and tomcat_1 and tomcat_2 have no errors.
Next, let's start by modifying the configuration of httpd and Tomcat, doing load balancing
httpd Configuration
First open httpd's conf file
Loading modules
(Note, I use "//" is because csdn this markdown with "#" layout is not good to see)
///listed below, no "#" is the original, and we need to remove;
//Slightly explained: Mod_proxy is the agent core module; proxy_ HTTP/AJP is the agreement of the agent, Balancer is must, SLOTMEM_SHM temporarily did not look around, no error
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
# #下面这类模块, is to select the way the load request distribution, should be * * selected and only one * *, as needed to solve the comments
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
//loadmodule lbmethod_bytraffic_ Module modules/mod_lbmethod_bytraffic.so
//loadmodule lbmethod_bybusyness_module modules/mod_lbmethod_ bybusyness.so
//loadmodule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
Virtual Host
Unlock the virtual host configuration file, go to that configuration, or configure it here, but for the understanding of decoupling
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Load Balancing Configuration
At the end of the document, add the Load balancer configuration
proxyrequests Off
<proxy balancer://cluster>
balancermember ajp:// 127.0.0.1:8009 loadfactor=1 route=tomat8_1
balancermember ajp://127.0.0.1:8010 loadfactor=1 route=tomat8_2
</proxy>
//Note that AJP is the protocol, Tomcat will have a port that supports the AJP protocol by default, which refers to the port of the AJP connector in Tomcat <connector>,ip
The route will correspond to the configuration of Tomcat in a moment.
Open httpd/conf/extra/httpd-vhosts.conf File
Note or delete the original VirtualHost configuration, and then configure the
<virtualhost *:80>
ServerAdmin xxx@163.com
ServerName localhost
serveralias localhost
proxypass/balancer://cluster/stickysession=jsessionid nofailover=on
proxypassreverse/balancer://cluster/
Errorlog "Logs/lb-error.log"
customlog "Logs/lb-access.log" Common
</VirtualHost>
Tomcat Configuration
The configuration of Tomcat is almost ready for the environment, which is basically configuring the route and then getting a test site.
Open Server.xml File
<!--Configure the route in the Jvmroute and httpd.conf on the engine, and the other Tomcat remembers the configuration--
<engine name= "Catalina" defaulthost= " localhost "jvmroute=" tomcat8_1 ">
Just do a test site.
To remember the structure of the Javaweb site, Tomcat is not configured by default in the WebApps under the most convenient, in WebApps a new folder, is the site path, such as called Test;
Test put a test.jsp and another folder Web-inf;
Test.jsp the following, simply print out the Jsessionid, the output will take Tomcat's name, so you can know that two tomcat in turn processing:
<%@ page contenttype= "text/html; Charset=utf-8 "%>
Web-inf a Web. xml file;
<?xml version= "1.0" encoding= "Utf-8"?>
<web-app xmlns= "Http://java.sun.com/xml/ns/javaee"
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
version=" 3.0 "
metadata-complete=" true ">
< distributable/>
</web-app>
Above. 4. Temporary knot
In fact, some have not finished writing, there is time to fill up.