This article mainly describes the Nginx load Scheduler + dual Tomcat load and session sharing +mysql back-end database, has a certain reference value, now share to everyone, the need for friends can refer to
Nginx Load Scheduler + dual Tomcat load and session replication +mysql backend database
Environment:
IP |
function |
192.168.2.5 |
Nginx |
192.168.2.6 |
Tomcat1 |
192.168.2.7 |
Tomcat2 |
192.168.2.8 |
Mysql |
First, session sharing before the configuration
Steps:
① close the firewall or open port 80,8080,3306, turn off SELinux
② Installing Nginx
Download the latest version of wget http://nginx.org/download/nginx-1.13.9.tar.gz from Nginx official website
[root@192 ~]# yum-y install pcre-devel zlib-devel gcc gcc-c++ make
[root@192 ~]# useradd-m-s/sbin/nologin nginx
[root@192 ~]# tar zxf nginx-1.13.9.tar.gz-c/usr/src
[root@192 ~]# cd/usr/src/nginx-1.13.9/
[root@192 nginx-1.13.9]#./configure--prefix=/usr/local/nginx--user=nginx--group=nginx--with-http_stub_status_ Module && make && make install
[root@192 ~]# ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin
Do not start Nginx
③ two Tomcat hosts for installation
JDK download required license from official website, allow to download to local, import host tomcat from official website downloads find tomcat7.0 or later wget http://mirror.bit.edu.cn/apache/tomcat/ Tomcat-7/v7.0.86/bin/apache-tomcat-7.0.86.tar.gz
[root@192 ~]# tar zxf jdk-7u65-linux-x64.gz-c/usr/src
[root@192 ~]# tar zxf apache-tomcat-7.0.54.tar.gz-c/usr/src
[root@192 ~]# MV jdk1.7.0_65//usr/local/java
[root@192 ~]# MV apache-tomcat-7.0.54//USR/LOCAL/TOMCAT7
[root@192 ~]# Vim/etc/profile
Export Java_home=/usr/local/javaexport catalina_home=/usr/local/tomcat7export path= $JAVA _home/bin: $CATALINA _home/ Bin: $PATH
[root@192 ~]# Source/etc/profile
[root@192 ~]# Java-version
Java version "1.8.0_171" Java (tm) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot (tm) 64-bit Server VM (Build 25.1 71-B11, Mixed mode)
[root@192 ~]# catalina.sh version
Using catalina_base: /usr/local/tomcat7using catalina_home: /usr/local/tomcat7using catalina_tmpdir:/usr/ Local/tomcat7/tempusing jre_home: /usr/local/javausing CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/ Usr/local/tomcat7/bin/tomcat-juli.jarserver Version:apache Tomcat/7.0.server built: May 10:26:15server Number: 7.0.86.0OS Name: linuxos version: 3.10.0-327.el7.x86_64architecture: AMD64JVM version: 1.7.0_65-B17JVM Vendor: Oracle Corporation
Start Tomcat
[root@192 ~]#/usr/local/tomcat7/bin/startup.sh
Using catalina_base: /usr/local/tomcat7using catalina_home: /usr/local/tomcat7using catalina_tmpdir:/ Usr/local/tomcat7/tempusing jre_home: /usr/local/javausing CLASSPATH: /usr/local/tomcat7/bin/ Bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jartomcat started.
Test access
http://192.168.2.6:8080
http://192.168.2.7:8080
④ two hosts to modify the Tomcat configuration file
[root@192 ~] #cd/usr/local/tomcat7/conf
[root@192 ~] #cp server.xml Server.xml.bak
[root@192 ~] #vim server.xml
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "tomcat-1" >
[root@192 ~] #mkdir-P/WEB/WEBAPP1
[root@192 ~] #vim/web/webapp1/index.jsp
<% @page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
Restart Tomcat
[root@192 ~] #shutdown. sh
[root@192 ~] #startup. sh
Note: The second host differs from Server.xml in jvmroute= "Tomcat-2" and index.jsp <title>tomcat-2</title>
Test access See two Tomcat hosts session ID is different, preparation work completed
http://192.168.2.6:8080
http://192.168.2.7:8008
Second, session sharing configuration
Steps:
① Two host configuration sessions shared cluster
[root@192 ~] #vim/usr/local/tomcat7/conf/server.xml
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "tomcat-1" > <!--for clustering A look at documentation in:/docs/cluster-howto.html (Simple-to)/docs/config/cluster.html (Referen CE documentation)-<!--<cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"/>- <cluster classname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster" channelsendoptions= "8" > <manager classname= "Org.apache.catalina.ha.session.DeltaManager" expiresessionsonshutdown= "FA LSE "notifylistenersonreplication=" true "/> <channel classname=" org.apache.catalina.tribes.g Roup. Groupchannel "> <membership classname=" Org.apache.catalina.tribes.membership.McastService " address= "228.0.0.4" port= "45564" frequency= "500" Droptime= "3000"/> <receiver classname= "org.apache.catalina.tribes.transport.nio.NioReceiver" addr ess= "192.168.2.5" port= "4000" autobind= "[selector]" Timeout= "maxthreads=" 6 "/> <sender classname=" Org.apache.catalina.tribes.tran Sport. Replicationtransmitter "> <transport classname=" Org.apache.catalina.tribes.transport.nio.PooledParallelS Ender "/> </Sender> <interceptor classname=" org.apache.catalina.tribes.group.interceptors . Tcpfailuredetector "/> <interceptor classname=" Org.apache.catalina.tribes.group.interceptors.MessageDispat Ch15interceptor "/> </Channel> <valve classname=" Org.apache.catalina.ha.tcp.ReplicationValve " Filter= ""/> <valve classname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/> <deployer class Name= "Org.apache.Catalina.ha.deploy.FarmWarDeployer "temdir="/tmp/war-temp/"deploydir="/tmp/war-deploy/" Watchdir= "/tmp/war-listen/" watchenabled= "false"/> <clusterlistener classname= "Org.apach E.catalina.ha.session.jvmroutesessionidbinderlistener "/> <clusterlistener className=" Org.apache.catalina.ha.session.ClusterSessionListener "/> </Cluster> <!--use of the Lockoutrealm to prevent a Ttempts to guess user passwords via a brute-force attack-to <realm classname= "Org.apache.catalina.realm.Lock Outrealm "> <!--this Realm uses the userdatabase configured in the global JNDI resources under the key" U Serdatabase ". Any edits that is performed against this userdatabase is immediately available for use by the Realm. --<realm classname= "Org.apache.catalina.realm.UserDatabaseRealm" resourcename= "Userdatabase"/> &L t;/realm>
[root@192 ~] #mkdir/web/webapp1/web-inf
[root@192 ~] #cp/usr/local/tomcat7/conf/web.xml web-inf/
[root@192 ~] #vim web-inf/web.xml
<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 "><distributable/> #添加这个单词, must have this step, otherwise the user's session cannot be used
If you turn on the firewall, turn on the following
[root@192 ~] #firewall-cmd--add-port=45564/udp--permanent
[root@192 ~] #firewall-cmd--add-port=4000/tcp--permanent
[root@192 ~] #firewall-cmd--reload
Restart Tomcat
[root@192 ~] #shutdown. sh
[root@192 ~] #startup. sh
Note: Two tomcat configurations as long as the receiver segment refers to the second host IP address can be
② configuration Nginx
[root@192 ~] #vim/usr/local/nginx/conf/nginx.conf
... http {..... upstream Tomcat_server { server 192.168.2.6:8080-weight=1; Server 192.168.2.7:8080 weight=1; } #gzip on ; server { listen ; server_name localhost; #charset Koi8-r; #access_log logs/host.access.log main; Location/{ root html; Index index.html index.htm; Proxy_pass http://tomcat_server; }
Start Nginx, Access nginx address Refresh page test access
[root@192 ~] #nginx
http://192.168.2.5
Third, connect MySQL database
Steps:
MySQL container in ①192.168.2.8 as the database server, configure MySQL:
Mysql>grant all privileges in . to javauser@ ' 192.168.2.% ' identified by ' 123.com ';
Mysql> CREATE DATABASE Javatest
Mysql>use Javatest
Mysql>create table testdata (id int not NULL auto_increment primary key,foo varchar), bar varchar (10));
Mysql>insert into TestData (Foo,bar) VALUES (' Hello ', ' 123.com ');
Mysql>select * from TestData;
ID |
Foo |
Bar |
1 |
Hello |
123.com |
② download Mysql-connector-java-5.1.22-bin.jar Copy to tomcat7/lib/directory (two Tomcat needs to be configured)
[root@192 ~]# CP mysql-connector-java-5.1.22-bin.jar/usr/local/tomcat7/lib/
③ Configuration Context.xml
[root@192 ~]# Cp/usr/local/tomcat7/conf/context.xml/usr/local/tomcat7/conf/context.xml.bak
[root@192 ~]# Vim/usr/local/tomcat7/conf/context.xml
<Context> <!--Default set of monitored resources-- <WatchedResource>WEB-INF/web.xml< /watchedresource> <resource name= "Jdbc/testdb" auth= "Container" type= "Javax.sql.DataSource" Maxactive= "maxidle=" "maxwait=" 10000 " username=" Javauser "password=" 123.com "Driverclassname=" Com.mysql.jdbc.Driver " url=" Jdbc:mysql://192.168.2.8:3306/javatest "/>
④ Configuring Web. Xml
[root@192 ~]# Vim/web/webapp1/web.xml
... <distributable/><description>mysql Test app</description> <resource-ref> < Description>db connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </ Resource-ref>
⑤ writing a connection database JSP file
[root@192 ~]# vim/web/webapp1/test.jsp
<%@ page language= "java" import= "java.sql.*" pageencoding= "GB2312"%>
⑥ rebooting Tomcat, testing the connection
[root@192 ~]# shutdown.sh
[root@192 ~]# startup.sh
http://192.168.2.5/test.jsp
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!