>  기사  >  백엔드 개발  >  Nginx 로드 스케줄러 + 듀얼 Tomcat 로드 및 세션 공유 + MySQL 백엔드 데이터베이스

Nginx 로드 스케줄러 + 듀얼 Tomcat 로드 및 세션 공유 + MySQL 백엔드 데이터베이스

不言
不言원래의
2018-07-07 16:43:161961검색

이 글에서는 주로 Nginx 로드 스케줄러 + 듀얼 Tomcat 로드 및 세션 공유 + MySQL 백엔드 데이터베이스를 소개합니다. 이제는 필요한 친구들이 참고할 수 있도록 공유합니다.

Nginx 로드 스케줄러+ 듀얼 Tomcat 로드 및 세션 복제 + MySQL 백엔드 데이터베이스

환경:

从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 ~]# cd /usr/src/nginx- 1.13.9/
IP Function
192.168.2.5 nginx
192.1 6 8.2.6 tomcat1
192.168. 2.7 tomcat2 080, 3306, selinux ② nginx 설치
[root@192 ~]# useradd -M -s /sbin/nologin nginx [root @192 ~]# tar zxf nginx-1.13.9.tar.gz -C /usr/src
[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/java
export CATALINA_HOME=/usr/local/tomcat7
export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH
[root@192 ~]# 소스 /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 버전

Using CATALINA_BASE:   /usr/local/tomcat7
Using CATALINA_HOME:   /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.
Server built:   May 19 2014 10:26:15
Server number:  7.0.86.0
OS Name:        Linux
OS Version:     3.10.0-327.el7.x86_64
Architecture:   amd64
JVM Version:    1.7.0_65-b17
JVM Vendor:     Oracle Corporation
Tomcat 시작

[root@192 ~] # /usr/local/tomcat7/bin/startup.sh

Using CATALINA_BASE:   /usr/local/tomcat7
Using CATALINA_HOME:   /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.

테스트 접속

http://192.168.2.6:8080

http://192.168.2.7:8080

4두 개의 호스트에서 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"와 tomcat-2</에서 다릅니다. title><p> in index.jsp <br></p>테스트 접속 결과 두 Tomcat 호스트의 세션 ID가 다른 것으로 나타나 준비 작업이 완료되었습니다<p></p>http://192.168.2.6:8080<p>http://192.168.2.7 :8008</p> <p>두 번째, 세션 공유 구성</p> <p>단계:</p> <p>①두 호스트가 세션 공유 클러스터 구성<br>[root@192 ~]#vim /usr/local/tomcat7/conf/server.xml<br></p> <pre class="brush:php;toolbar:false">....... <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.catal ina.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"/></pre>[root@192 ~ ]#mkdir /web/webapp1/WEB-INF<p> <strong>[root@192 ~]#cp /usr/local/tomcat7/conf/web.xml WEB-INF/</strong></p>[root@192 ~]#vim WEB-INF /web.xml<p></p> <pre class="brush:php;toolbar:false"><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没法使用</pre> <blockquote>방화벽이 켜져 있으면 다음을 활성화하세요 <br> </blockquote> <h2>[root@192 ~]#firewall-cmd --add-port=45564/udp --permanent</h2> <p></p>[root@192 ~]#firewall-cmd --add-port=4000/tcp --permanent<p> <br></p>[root@192 ~]#firewall-cmd --reload<p></p> <p>Tomcat 다시 시작</p>[root@192 ~]#shutdown.sh <p>[root@192 ~]#startup.sh</p> <p><em>참고: 두 Tomcat 구성은 동일합니다. 수신자 섹션에서 두 번째 호스트의 IP 주소를 가리키기만 하면 됩니다</em><br><em>②nginx 구성</em>[root@192 ~] #vim /usr/local/nginx/conf/nginx.conf<br></p> <pre class="brush:php;toolbar:false">....... 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;         }</pre> <em> nginx를 시작하고 nginx 주소를 방문한 후 페이지를 새로 고쳐 액세스를 테스트하세요 </em><br>[root@192 ~]#nginx<em></em>http://192.168.2.5 <p>3. mysql 데이터베이스에 연결합니다. <br><br>단계: </p> <p>1mysql을 데이터베이스 서버로 사용하고 mysql을 구성합니다. <strong>mysql>다음으로 식별되는 </strong>.</p>에 대한 모든 권한을 부여합니다. '123.com';<p><br>mysql> 데이터베이스 생성 javatest</p> <p>mysql>javatest</p> 사용 <p>mysql>테이블 testdata 생성(id int not null auto_increment 기본 키,foo varchar(25),bar varchar(10));</p> <blockquote> mysql>testdata(foo,bar) 값에 삽입('hello','123.com ') </blockquote> <h2>mysql> testdata에서 * 선택;</h2> <p>②下载mysql-connector-java-5.1.22-bin.jar 复制到tomcat7/lib/目录下(两台tomcat都需要配置)<br>[root@192 ~]# cp mysql-connector-java-5.1.22-bin.jar /usr/local/tomcat7/lib/</p> <p>③配置context.xml<br>[root@192 ~]# cp /usr/local/tomcat7/conf/context.xml /usr/local/tomcat7/conf/context.xml.bak</p> <p>[root@192 ~]# vim /usr/local/tomcat7/conf/context.xml</p> <pre class="brush:php;toolbar:false"><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"/></pre> <p>④配置web.xml<br>[root@192 ~]# vim /web/webapp1/web.xml</p> <pre class="brush:php;toolbar:false">...... <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></pre> <p>⑤编写连接数据库jsp文件<br>[root@192 ~]# vim /web/webapp1/test.jsp</p> <pre class="brush:php;toolbar:false"><%@ 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></pre> <p>⑥重启tomcat,测试连接<br>[root@192 ~]# shutdown.sh<br>[root@192 ~]# startup.sh</p> <blockquote>http://192.168.2.5/test.jsp</blockquote> <p>以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!</p> <p>相关推荐:</p> <p class="comments-box-content"><a title="ThinkPHP5.0 Linux Apache/Nginx重写URL配置" href="//m.sbmmt.com/php-weizijiaocheng-406204.html" target="_blank">ThinkPHP5.0 Linux Apache/Nginx重写URL配置</a><br></p> <p class="mt20 ad-detail-mm hidden-xs"><a title="使用cronolog切割nginx访问日志,定时清理旧日志" href="//m.sbmmt.com/php-weizijiaocheng-406203.html" target="_blank">使用cronolog切割nginx访问日志,定时清理旧日志</a><br></p><p>위 내용은 Nginx 로드 스케줄러 + 듀얼 Tomcat 로드 및 세션 공유 + MySQL 백엔드 데이터베이스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!</p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>성명:</span><div>본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.</div></div></div><div class="nphpSytBox"><span>이전 기사:<a class="dBlack" title="ThinkPHP5.0 Linux Apache/Nginx 재작성 URL 구성" href="//m.sbmmt.com/m/ko/faq/406204.html">ThinkPHP5.0 Linux Apache/Nginx 재작성 URL 구성</a></span><span>다음 기사:<a class="dBlack" title="ThinkPHP5.0 Linux Apache/Nginx 재작성 URL 구성" href="//m.sbmmt.com/m/ko/faq/406206.html">ThinkPHP5.0 Linux Apache/Nginx 재작성 URL 구성</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>관련 기사</h2><em><a href="//m.sbmmt.com/m/ko/article.html" class="bBlack"><i>더보기</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="//m.sbmmt.com/m/ko/faq/1.html" title="cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법" class="aBlack">cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법</a><div class="clear"></div></li><li><b></b><a href="//m.sbmmt.com/m/ko/faq/1.html" title="cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법" class="aBlack">cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법</a><div class="clear"></div></li><li><b></b><a href="//m.sbmmt.com/m/ko/faq/1.html" title="cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법" class="aBlack">cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법</a><div class="clear"></div></li><li><b></b><a href="//m.sbmmt.com/m/ko/faq/1.html" title="cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법" class="aBlack">cURL을 사용하여 PHP에서 Get 및 Post 요청을 구현하는 방법</a><div class="clear"></div></li><li><b></b><a href="//m.sbmmt.com/m/ko/faq/2.html" title="정규식의 모든 표현식 기호(요약)" class="aBlack">정규식의 모든 표현식 기호(요약)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="//m.sbmmt.com/m/ko/"><b class="icon1"></b><p>집</p></a></li><li><a href="//m.sbmmt.com/m/ko/course.html"><b class="icon2"></b><p>강의</p></a></li><li><a href="//m.sbmmt.com/m/ko/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="//m.sbmmt.com/m/ko/login"><b class="icon5"></b><p>나의</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="//m.sbmmt.com/m/ko/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="//m.sbmmt.com/m/ko/"><b class="icon1"></b><span>집</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/course.html"><b class="icon2"></b><span>강의</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/article.html"><b class="icon3"></b><span>기사</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/dic.html"><b class="icon6"></b><span>사전</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/course/type/99.html"><b class="icon7"></b><span>수동</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/xiazai/"><b class="icon8"></b><span>다운로드</span><div class="clear"></div></a></li><li><a href="//m.sbmmt.com/m/ko/faq/zt" title="주제"><b class="icon12"></b><span>주제</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="//m.sbmmt.com/m/ko/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/" >집</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/article.html" class="hover">기사</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/course.html" >강의</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/faq/zt" >주제</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/xiazai" >다운로드</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/game" >게임</a></div><div class="swiper-slide"><a href="//m.sbmmt.com/m/ko/dic.html" >사전</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:setlang('en');" class="language course-right-orders chooselan " href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>