实验拓扑图:
实验环境:
在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境。
实验过程中需要安装的工具包包括:vim unzip lrzsz lsof netstat bind gcc gcc-c++ pcre-devel openssl openssl-devel zlib-devel gd-devel
Tomcat WEB1:CentOS 7 192.168.81.51:8079
192.168.81.51:8080
Tomcat WEB2:CentOS 7 192.168.81.52:8079
192.168.81.52:8080
Nginx代理:CentOS 6.5 192.168.81.54
172.18.7.177
DNS服务器:CentOS 6.5 172.18.7.135
测试用客户机: 172.18.7.144
实验过程:
一、搭建Tomcat服务器
(提前下载apache-tomcat-7.0.47.tar.gz、jdk-8u20-linux-x64.rpm、Anynote_1.3.4.149.zip)
#systemctl stop firewalld \\关闭服务器防火墙
#setenforce 0 \\关闭selinux
#rpm -ivh jdk-8u20-linux-x64.rpm \\安装JAVA环境(这里选择1.8版本的JAVA)
#java –version
#tar -zxvf apache-tomcat-7.0.47.tar.gz –C /mnt
#mv /mnt/ apache-tomcat-7.0.47.tar.gz /opt/tomcat
#chmod +x /opt/tomcat \\设置目录可执行权限
#cp –ar /opt/tomcat /opt/tomcat2
#vim /opt/tomcat/conf/server.xml(修改三个端口号,在这里分别修改为8004,8008,8079)
shutdown 端口:8005 主要负责启动关闭.
ajp端口:8009 主要负责通过ajp均衡(常用于apache和tomcat整合)
http端口:8080 可以通过web页面直接访问(nginx+tomcata整合)
#/opt/ tomcat/bin/startup.sh
#/opt/ tomcat2/bin/startup.sh
#netstat –atnl(验证是否出现8008,8009,8079,8080,出现则为成功启动,这时可以通过在浏览器输入IP+端口访问Tomcat默认页面)
#yum –y install mariadb mariadb-server
#systemctl start mariadb
#mysqladmin –uroot –p(输入密码)
#mysql –u root –p
#unzip Anynote_1.3.4.149.zip
#mv Anynote_1.3.4.149/Anynote.war /opt/tomcat/webapps/
#cp /opt/tomcat/webapps/Anynote.war /opt/tomcat2/webapps/
#cd ANynote_1.3.4.149 /Anynote/database
MariaDB [(none)]> create database Sqlite;
MariaDB [(none)]> \q
#mysql -uroot -p Sqlite < zeyu.sql
#vim /opt/tomcat/webapps/Anynote/WEB-INF/jdbc.properties
#vim /opt/tomcat2/webapps/Anynote/WEB-INF/jdbc.properties(修改部分如下)
重启tomcat1和tomcat2
# /opt/tomcat/bin/shutdown.sh
# /opt/tomcat/bin/startup.sh
# /opt/tomcat2/bin/shutdown.sh
# /opt/tomcat2/bin/startup.sh
登录tomcat查看Anynote是否搭建成功
二、搭建Nginx代理服务器(提前下载nginx-1.6.3.tar.gz、Anynote_1.3.4.149.zip)
# groupadd -g 108 -r nginx
# useradd -u 108 -r -g 108 nginx
# id nginx \\ uid=108(nginx) gid=108(nginx) 组=108(nginx)
# tar zxvf nginx-1.6.3.tar.gz
# cd nginx-1.6.3
#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_image_filter_module --with-http_gzip_static_module
# make && make install
# /usr/local/nginx/sbin/nginx \\启动NGINX
# ps -ef |grep nginx
# vim /usr/local/nginx/conf/nginx.conf(修改配置文件实现负载均衡和动静分离)
upstream jsp_tomcat {
ip_hash;
server 192.168.81.51:8079 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.81.52:8079 weight=1 max_fails=2 fail_timeout=30s;
}
upstream jpg_tomcat {
ip_hash;
server 192.168.81.52:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.81.51:8080 weight=1 max_fails=2 fail_timeout=30s;
}
}
server {
listen 80;
server_name www.e-qianxi.com;
location / {
index index.html;
root /data/www/wugk;
}
location ~ .*\.(jsp|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jsp_tomcat;
}
location ~ .*\.( gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://jpg_tomcat;
}
# pkill nginx
# /usr/local/nginx/sbin/nginx \\重新启动nginx
# unzip Anynote_1.3.4.149.zip
# mv Anynote_1.3.4.149/Anynote/* /data/www/wugk/
访问代理IP测试(分别尝试访问了内网和外网的IP地址/Anynote/index.jsp)
三、DNS代理服务器配置
# yum -y install bind*
# vim /etc/named.conf
# vim /var/named/e-qianxi.com.zone
# vim /var/named/172.18.7.zone
# service named restart
四、配置两台Tomcat WEB的Mysql实现同步(双主复制)
修改两台服务器数据库的/etc/my.cnf文件
192.168.81.51:(server-id号不可以相同,replicate-do-db设置的是需要同步的数据库名称)
192.168.81.52:
# systemctl restart mariadb.service \\分别重新启动数据库
#mysql –uroot –p
192.168.81.51:
MariaDB [(none)]> grant replication slave on *.* to master@'192.168.81.52' identified by '123';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status;(确认File和Position)
MariaDB [(none)]> stop slave;
MariaDB [(none)]> change master to
master_host='192.168.81.52',
master_user='master', \\用户的用户名
master_password='123', \\用户的密码
master_log_file='mysql-bin.000001', \\之前确认的File
master_log_pos=625; \\之前确认的Posision
192.168.81.52:
MariaDB [(none)]> grant replication slave on *.* to master@'192.168.81.51' identified by '123';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status;
MariaDB [(none)]> change master to
master_host='192.168.81.51',
master_user='master',
master_password='123',
master_log_file='mysql-bin.000001',
master_log_pos=625;
分别开启slave
MariaDB [(none)]> start slave;
查看slave状态:show slave status\G;(出现双YES为配置正确)
五、登录测试用客户机,设置DNS服务器为172.18.7.135
使用客户机访问www.e-qianxi.com并添加任务
使用真机分别访问Tomcat的IP地址端口检测是否数据同步。
(在双主复制搭建之前添加的任务不会同步,在双主复制搭建之后添加的任务所有Tomcat都会有)
(在同一个tomcat服务器上的虽然端口8079的和8080不同,但是调用的是同一个数据库)