一、百万pv架构案例概述
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 keepalived
global_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/shell
vi /opt/shell/nginx.sh
#!/bin/bash
k=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $k -gt 0 ];then
/bin/systemctl start nginx.service
else
/bin/systemctl stop nginx.service
fi
chmod +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.gz
tar xf jdk-8u144-linux-x64.tar.gz
cp -rv jdk1.8.0_144/ /usr/local/java
(2)配置jdk环境变量
vi /etc/profile
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
source /etc/profile //让环境变量及时生效
java -version //查看java版本
(3)创建tomcat开启跟关闭的链接,并开启服务
cp -r apache-tomcat-8.5.23 /usr/local/tomcat8
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
tomcatup
netstat -anpt | grep 8080
(4)测试默认测试页是否正常显示
http://192.168.174.165:8080/
http://192.168.174.142:8080/
vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容
this is server 142!!
输入调度器地址,也就是虚拟地址http://192.168.174.110/ ,测试两台节点的调度情况
当down掉165这台主机后,查看页面显示的信息,此时显示的是另外一台tomcat服务器的首页
cd /usr/local/tomcat8/conf/
vi server.xml //跳到行尾,在Host name下新增 148
日志调试信息debug为0表示信息越少,docBase指定访问目录
四、mysql安装
(1)yum安装maridb数据库
yum install -y mariadb-server mariadb
systemctl 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] y
New 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.sql
mysql -uroot -p
show 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/classes
vi jdbc.properties //修改数据库IP地址是VRRP的虚拟IP,以及授权的用户名root和密码abc123。
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.174.110\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=root
password=abc123
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
(6)网站测试
http://192.168.1754.142:8080/ //默认的用户名admin 密码:123456
http://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.conf
bind 0.0.0.0
从服务器上多如下一行配置
slaveof 192.168.174.139 6379 //主服务器的IP不是虚拟IP
开启服务,检查端口是否开启
systemctl start redis.service
netstat -anpt | grep 6379
(4)进入主服务器数据库,创建数据
redis-cli -h 192.168.174.139 -p 6379
192.168.174.139:6379> set username zhangsan
OK
192.168.174.139:6379> get username
"zhangsan"
(5)进入从服务器,测试是否同步
redis-cli -h 192.168.174.141 -p 6379
192.168.174.141:6379> get username
"zhangsan" //获取到主服务器创建的数据,说明主从同步成功
(6)在两台tomcat服务器上配置网站链接redis的参数
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
vim applicationContext-mybatis.xml
#47行
#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 start
netstat -anpt | grep 26379
redis-cli -h 192.168.174.139 -p 26379
192.168.174.139:26379> info #查看集群信息
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0: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
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0: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_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
(2)开启mysql服务,并查看端口状态
systemctl restart mariadb
netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 13998/mysqld
(3)进入数据库,查看master状态,
mysql -u root
MariaDB [(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_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2 //server_id改为2,表示为是第一台从服务器,为1,表示为是mysql主服务器
log_slave_updates=true
sync_binlog=1
(2)开启mysql服务,查看端口状态
systemctl restart mariadb
netstat -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)