Home>Article>Backend Development> Nginx load scheduler + dual Tomcat load and session sharing + MySQL back-end database

Nginx load scheduler + dual Tomcat load and session sharing + MySQL back-end database

不言
不言 Original
2018-07-07 16:43:16 1901browse

This article mainly introduces the Nginx load scheduler dual Tomcat load and session sharing MySQL back-end database. It has a certain reference value. Now I share it with you. Friends in need can refer to it

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 1. Configuration before session sharing
mysql

Steps:

①Close the firewall or open ports 80, 8080, 3306, close selinux

②Install 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

Do not start nginx

③Two tomcat hosts for installation

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 ~]# 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/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

Start 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.

Test access

http:// 192.168.2.6:8080

http://192.168.2.7:8080

④Two hosts 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

.......   

[root@192 ~]#mkdir -p /web/webapp1

[root@192 ~]#vim /web/webapp1/index.jsp

<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>   tomcat-1 

Session serviced by tomcat

<% session.setAttribute("abc","abc");%>
Session ID <%=session.getId()%>
Create on <%= session.getCreationTime() %>

Restart tomcat

[root@192 ~] #shutdown.sh

[root@192 ~]#startup.sh

Note: The second host is different in jvmRoute=”tomcat-2” in server.xml, and index. tomcat-2

Test access in jsp shows that the session IDs of the two tomcat hosts are different, and the preparations are completed

http://192.168.2.6 :8080

http://192.168.2.7:8008

2. Session sharing configuration

Steps:

①Configure session sharing cluster on two hosts

[root@192 ~]#vim /usr/local/tomcat7/conf/server.xml

.......                           

[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

  #添加这个单词,必须有这一步,否则用户的session没法使用

If the firewall is enabled, enable 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: The two tomcat configurations are the same as long as The Receiver segment refers to the IP address of the second host

②Configure 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; }

Start nginx, visit the nginx address and refresh the page to test access

[root@192 ~]#nginx

http://192.168.2.5

3. Connection mysql database

Steps:

①The mysql container in 192.168.2.8 serves as the database server, configure 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;

②下载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

  WEB-INF/web.xml 

④配置web.xml
[root@192 ~]# vim /web/webapp1/web.xml

......  MySQL Test App  DB Connection jdbc/TestDB javax.sql.DataSource Container 

⑤编写连接数据库jsp文件
[root@192 ~]# vim /web/webapp1/test.jsp

<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>   MySQL 
connect MySQL
<% 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("
foo:"+rs.getString(2)+"bar:"+rs.getString(3)); } rs.close(); stmt.close(); conn.close(); %>

⑥重启tomcat,测试连接
[root@192 ~]# shutdown.sh
[root@192 ~]# startup.sh

http://192.168.2.5/test.jsp

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

ThinkPHP5.0 Linux Apache/Nginx重写URL配置

id 1
foo bar
hello 123.com

The above is the detailed content of Nginx load scheduler + dual Tomcat load and session sharing + MySQL back-end database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn