PV(page view,页面浏览量)即点击量,通常是衡量一个网站受欢迎程度的主要指标。
本案例采用四层模式实现,主要分为前端反向代理层、web层、数据库缓存层和数据库层。前端反向代理层采用主备模式,web层采用集群模式,数据库缓存层采用主备模式,数据库层采用主从模式。每一层都做到了高可用架构,大大提高了业务的稳定性。
案例架构图如下:实线表示是正常情况下数据流向,虚线表示的是非正常情况下的数据流向。
案例环境如下表:
主机角色 | ip地址 | 用途 |
---|---|---|
master | 192.168.174.139 | 前端nginx反向代理主机、redis缓存主机、mysql数据主库 |
backup | 192.168.174.141 | 前端nginx反向代理备机、redis缓存备机、mysql数据备库 |
tomcat-node1 | 192.168.174.142 | tomcat服务器 |
tomcat-node2 | 192.168.174.165 | tomcat服务器 |
二、安装nginx+keepalived
(1)安装带有nginx rpm软件包的源
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(2)使用yum安装keepalived跟nginx服务
yum install -y keepalived nginx
(3)修改keepalived.conf配置文件
vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { ???route_id NGINX_HA ????//主服务器id为NGINX_HA,从是NGINX_HB}vrrp_script nginx { ???script "/opt/shell/nginx.sh" ??//编写脚本,待会创建 ???interval 2}vrrp_instance VI_1 { ???state MASTER ???interface eth0 ???virtual_router_id 51 ???priority 100 ?//master优先级要高于backup ???advert_int 1 ???authentication { ???????auth_type PASS ???????auth_pass 1111}track_script { ???nginx ??????????//触发脚本}virtual_ipaddress { ???192.168.174.110 ???//虚拟ip地址 ???}}
(4)创建nginx.shell脚本目录,并编写脚本
mkdir /opt/shellvi /opt/shell/nginx.sh#!/bin/bashk=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $k -gt 0 ];then ???/bin/systemctl start nginx.serviceelse/bin/systemctl stop nginx.servicefichmod +x /opt/shell/nginx.sh
从服务器keepalived配置操作跟主服务器操作一样。
(5)编辑nginx.conf配置文件,主从操作一样
upstream tomcat_pool { ???????????????server 192.168.174.165:8080; ?//两台节点服务器地址 ???????????????server 192.168.174.142:8080; ???????????????ip_hash; ???????} ???????server { ???????????????listen 80; ???????????????server_name 192.168.174.110; //虚拟ip地址 ???????????????location / { ???????????????????????proxy_pass http://tomcat_pool; ???????????????????????proxy_set_header X-Real-IP $remote_addr; ???????????????} ???????}
(6)检查语法并开启服务
nginx -t -c /etc/nginx/nginx.conf ?//测试配置文件语法systemctl start keepalived.service ???//nginx启动会等待一会
(7)测试keepalived的功能是否正常
主服务器:ip addr
关闭keepalived服务再次查看主服务器,此时虚拟ip地址已经不存在了
切换到从服务器上查看,此时虚拟ip自动漂移到从服务器。
开启主服务器上的keepalived服务,此时虚拟ip再次漂移到主服务器。
三、Tomcat安装
两台节点服务器安装过程一样。
(1)解压缩Apache跟jdk,
tar xf apache-tomcat-8.5.23.tar.gztar xf jdk-8u144-linux-x64.tar.gzcp -rv jdk1.8.0_144/ /usr/local/java
(2)配置jdk环境变量
vi /etc/profileexport JAVA_HOME=/usr/local/javaexport JRE_HOME=/usr/local/java/jreexport PATH=$PATH:/usr/local/java/binexport CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/libsource /etc/profile ?//让环境变量及时生效java -version ?//查看java版本
(3)创建tomcat开启跟关闭的链接,并开启服务
cp -r apache-tomcat-8.5.23 /usr/local/tomcat8ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatupln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdowntomcatupnetstat -anpt | grep 8080
(4)测试默认测试页是否正常显示
http://192.168.174.165:8080/ ?http://192.168.174.142:8080/vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容<h1>this is server 142!!</h1>
输入调度器地址,也就是虚拟地址http://192.168.174.110/ ,测试两台节点的调度情况
当down掉165这台主机后,查看页面显示的信息,此时显示的是另外一台tomcat服务器的首页
cd /usr/local/tomcat8/conf/vi server.xml //跳到行尾,在Host name下新增 148<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>日志调试信息debug为0表示信息越少,docBase指定访问目录
四、mysql安装
(1)yum安装maridb数据库
yum install -y mariadb-server mariadbsystemctl start mariadb.service ?//开启服务systemctl enable mariadb.service ?netstat -anpt | grep 3306 ??//查看服务是否开启成功
mysql_secure_installation ???//常规安全设置
Enter current password for root (enter for none): #敲回车OK, successfully used password, moving on...Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!Remove anonymous users? [Y/n] n ... skipping.Disallow root login remotely? [Y/n] n ... skipping.Remove test database and access to it? [Y/n] n ... skipping.Reload privilege tables now? [Y/n] y ... Success!
(2)测试mariadb是否可用
mysql -uroot -p ?//进入数据库
(3)导入商城数据
mysql -u root -p ?< slsaledb-2014-4-10.sqlmysql -uroot -pshow databases;GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ?‘abc123‘; //给root用户授权flush privileges; //刷新
(5)在两台tomcat服务器上搭建网站
tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps///解压商城cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classesvi jdbc.properties //修改数据库IP地址是VRRP的虚拟IP,以及授权的用户名root和密码abc123。driverClassName=com.mysql.jdbc.Driverurl=jdbc\:mysql\://192.168.174.110\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8uname=rootpassword=abc123minIdle=10maxIdle=50initialSize=5maxActive=100maxWait=100removeAbandonedTimeout=180removeAbandoned=true
(6)网站测试
http://192.168.1754.142:8080/ ??//默认的用户名admin 密码:123456http://192.168.174.165:8080/http://192.168.174.110 //输入虚拟地址测试登录,并且关闭主再测试登录
五、redis集群
(1)安装epel源
yum install -y epel-release
(2)安装Redis
yum install redis -y
(3)更改redis.conf配置文件
vim /etc/redis.confbind 0.0.0.0
从服务器上多如下一行配置
slaveof 192.168.174.139 ?6379 ??//主服务器的IP不是虚拟IP
开启服务,检查端口是否开启
systemctl start redis.servicenetstat -anpt | grep 6379
(4)进入主服务器数据库,创建数据
redis-cli -h 192.168.174.139 -p 6379192.168.174.139:6379> set username zhangsanOK192.168.174.139:6379> get username"zhangsan"
(5)进入从服务器,测试是否同步
redis-cli -h 192.168.174.141 -p 6379192.168.174.141:6379> get username"zhangsan" ????//获取到主服务器创建的数据,说明主从同步成功
(6)在两台tomcat服务器上配置网站链接redis的参数
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/ vim applicationContext-mybatis.xml <constructor-arg value="192.168.174.110"/> ????????#47行 ???????????????<constructor-arg value="6379"/> ???#48行
(7)打开网站页面,点击需要进行数据库调用的操作,并测试缓存是否开启
192.168.174.110:6379> info ????//查看缓存是否开启成功# Stats...keyspace_hits:2 ?//当命中为2时说明缓存开启成功keyspace_misses:20 ??//未命中...
(8)主服务器配置redis主从切换
vi /etc/redis-sentinel.conf sentinel monitor mymaster 192.168.174.139 6379 1 #1表示1台从(这边的ip地址是主服务器的ip地址) sentinel down-after-milliseconds mymaster 3000 ??#故障切换时间单位是毫秒
(9)启动集群服务,并查看集群信息
service redis-sentinel startnetstat -anpt | grep 26379redis-cli -h 192.168.174.139 -p 26379 192.168.174.139:26379> info ???#查看集群信息# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.174.139:6379,slaves=1,sentinels=1 ??//此时redis主服务器为139这台主机
(10)关闭主服务器redis服务,查看集群信息中的redis主服务器ip是否发生变化
systemctl stop redis.service ??#关闭主服务器的redis服务 redis-cli -h 192.168.174.139 -p 26379 ?192.168.174.139:26379> info ?# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=mymaster,status=ok,address=192.168.174.141:6379,slaves=1,sentinels=1 ?//此时发现Redis主服务器ip地址切换为从redis服务器地址,说明主从redis配置成功
六、mysql主从配置
1、主服务器上的配置
(1)编辑my.cnf配置文件
vi /etc/my.cnf[mysqld]binlog-ignore-db=mysql,information_schemacharacter_set_server=utf8log_bin=mysql_binserver_id=1log_slave_updates=truesync_binlog=1
(2)开启mysql服务,并查看端口状态
systemctl restart mariadbnetstat -anpt | grep 3306tcp ???????0 ?????0 0.0.0.0:3306 ???????????0.0.0.0:* ??????????????LISTEN ?????13998/mysqld
(3)进入数据库,查看master状态,
mysql -u rootMariaDB [(none)]>show master status; //记录日志文件名称和 位置值+------------------+----------+--------------+--------------------------+| File ????????????| Position | Binlog_Do_DB | Binlog_Ignore_DB ????????|+------------------+----------+--------------+--------------------------+| mysql_bin.000001 | ?????473 | ?????????????| mysql,information_schema |+------------------+----------+--------------+--------------------------+1 row in set (0.00 sec)grant replication slave on *.* to ‘rep‘@‘192.168.174.%‘ identified by ‘123456‘;flush privileges;
3、从服务器配置
(1)编辑my.cnf配置文件
[mysqld]binlog-ignore-db=mysql,information_schemacharacter_set_server=utf8log_bin=mysql_binserver_id=2 ???//server_id改为2,表示为是第一台从服务器,为1,表示为是mysql主服务器log_slave_updates=truesync_binlog=1
(2)开启mysql服务,查看端口状态
systemctl restart mariadbnetstat -anpt | grep 3306
(3)进入数据库
change master to master_host=‘192.168.174.139‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=473;
start slave;show slave status\G; ?//查看slave状态Slave_IO_Running: Yes ??//当都为yes时表示开启成功Slave_SQL_Running: Yes
(4)测试主从同步
在主服务器上创建checkMysql
MariaDB [(none)]> create database checkMysql;Query OK, 1 row affected (0.01 sec)MariaDB [(none)]> show databases;+--------------------+| Database ??????????|+--------------------+| information_schema || checkMysql ????????|| mysql ?????????????|| performance_schema || slsaledb ??????????|| test ??????????????|+--------------------+6 rows in set (0.00 sec)
查看mysql从服务器数据库,此时存在checkMysql,说明mysql主从创建成功。
MariaDB [(none)]> show databases;+--------------------+| Database ??????????|+--------------------+| information_schema || checkMysql ????????|| mysql ?????????????|| performance_schema || slsaledb ??????????|| test ??????????????|+--------------------+6 rows in set (0.00 sec)
百万PV网站架构案例
原文地址:http://blog.51cto.com/13620954/2155656