這篇文章主要介紹了關於Nginx負載調度器+雙Tomcat負載及會話共用+MySQL後端資料庫,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
Nginx負載調度器+雙Tomcat負載及會話複製+MySQL後端資料庫
環境:
IP |
作用 |
192.168.2.5 |
nginx |
192.168.2.6 |
tomcat1 |
192.168.2.7 |
tomcat2 |
192.168.2.8 |
mysql |
一、session共用之前配置
步驟:
①關閉防火牆或者開放連接埠80,8080,3306,關閉selinux
②安裝nginx
從nginx官網下載最新版wget http://nginx.org/download/nginx-1.13.9.tar.gz
[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
不啟動nginx
③兩台tomcat主機進行安裝
jdk從官網下載需要許可,允許之後下載至本地,匯入主機tomcat從官網downloads找到tomcat7.0或者更高版本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.171-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 19 2014 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
啟動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.
測試訪問
http://192.168.2.6:8080
http://192.168.2.7:8080
④兩台主機修改tomcat設定檔
[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"><Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="true"/>
[root@192 ~]#mkdir -p /web/webapp1
[root@192 ~]#vim /web/webapp1/index.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%><html> <head><title>tomcat-1</title> </head><body> <h1><font color="red">Session serviced by tomcat</font></h1><table aligh="center" border="1"><tr> <td>Session ID</td> <td><%=session.getId()%></td><% session.setAttribute("abc","abc");%> </tr><tr><td>Create on</td><td><%= session.getCreationTime() %></td></tr> </table></body></html>
重啟tomcat
[root@192 ~]#shutdown.sh
[root@192 ~]#startup.sh
注意:第二台主機不同處server.xml中jvmRoute=”tomcat-2”,還有index.jsp中<title>tomcat-2</title>
測試訪問看到兩台tomcat主機session ID不同,準備工作完成
http://192.168.2.6:8080
http://192.168.2.7:8008
二、session共用配置
步驟:
①兩台主機配置會話共用群集
[root@192 ~]#vim /usr/local/tomcat7/conf/server.xml
.......<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1"> <!--For clustering, please take a look at documentation at: /docs/cluster-howto.html (simple how to) /docs/config/cluster.html (reference 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="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.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" address="192.168.2.5" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" temDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> <!-- Use the LockOutRealm to prevent attempts to guess user passwords via a brute-force attack --> <Realm className="org.apache.catalina.realm.LockOutRealm"> <!-- This Realm uses the UserDatabase configured in the global JNDI resources under the key "UserDatabase". Any edits that are performed against this UserDatabase are immediately available for use by the Realm. --> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="true"/>
[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/> #添加這個單詞,必須有這一步,否則使用者的session沒法使用
如果開啟防火牆的話,開啟以下
[root@192 ~]#firewall-cmd --add-port=45564/udp --permanent
[root@192 ~]#firewall-cmd --add-port=4000/tcp --permanent
[root@192 ~]#firewall-cmd --reload
重啟tomcat
[root@192 ~]#shutdown.sh
[root@192 ~]#startup.sh
注意:兩個tomcat配置一樣只要在Receiver段指對第二台主機ip地址即可
②配置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 80; 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; }
啟動nginx,訪問nginx地址重新整理頁面測試訪問
[root@192 ~]#nginx
http://192.168.2.5
三、串連mysql資料庫
步驟:
①192.168.2.8中的mysql容器作為資料庫伺服器,配置mysql:
mysql>grant all privileges on . 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(25),bar varchar(10));
mysql>insert into testdata(foo,bar) values(‘hello’,’123.com’);
mysql>select * from testdata;
id |
foo |
bar |
1 |
hello |
123.com |
②下載mysql-connector-java-5.1.22-bin.jar 複製到tomcat7/lib/目錄下(兩台tomcat都需要配置)
[root@192 ~]# cp mysql-connector-java-5.1.22-bin.jar /usr/local/tomcat7/lib/
③配置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="100" maxIdle="30" maxWait="10000" username="javauser" password="123.com" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.2.8:3306/javatest"/>
④配置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>
⑤編寫串連資料庫jsp檔案
[root@192 ~]# vim /web/webapp1/test.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%><html> <head> <title>MySQL</title> </head><body>connect MySQL<br><%String driverClass="com.mysql.jdbc.Driver";String url="jdbc:mysql://192.168.2.8:3306/javatest";String username = "javauser";String password = "123.com";Class.forName(driverClass);Connection conn=DriverManager.getConnection(url, username, password);Statement stmt=conn.createStatement();ResultSet rs = stmt.executeQuery("select * from testdata");while(rs.next()){ out.println("<br>foo:"+rs.getString(2)+"bar:"+rs.getString(3));}rs.close();stmt.close();conn.close();%></body></html>
⑥重啟tomcat,測試連接
[root@192 ~]# shutdown.sh
[root@192 ~]# startup.sh
http://192.168.2.5/test.jsp
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!