百萬PV網站架構案例

1、百萬pv架構案例概述

PV(page view,頁面瀏覽量)即點擊量,一般是衡量一個網站受歡迎程度的主要指標。前端

本案例採用四層模式實現,主要分爲前端反向代理層、web層、數據庫緩存層和數據庫層。前端反向代理層採用主備模式,web層採用集羣模式,數據庫緩存層採用主備模式,數據庫層採用主從模式。每一層都作到了高可用架構,大大提升了業務的穩定性。java

案例架構圖以下:實線表示是正常狀況下數據流向,虛線表示的是非正常狀況下的數據流向。
百萬PV網站架構案例node

案例環境以下表:mysql

主機角色 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服務器

2、安裝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腳本目錄,並編寫腳本linux

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配置操做跟主服務器操做同樣。nginx

(5)編輯nginx.conf配置文件,主從操做同樣web

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)檢查語法並開啓服務redis

nginx -t -c /etc/nginx/nginx.conf  //測試配置文件語法
systemctl start keepalived.service    //nginx啓動會等待一會

(7)測試keepalived的功能是否正常
主服務器:ip addr
百萬PV網站架構案例
關閉keepalived服務再次查看主服務器,此時虛擬ip地址已經不存在了
百萬PV網站架構案例
切換到從服務器上查看,此時虛擬ip自動漂移到從服務器。
百萬PV網站架構案例
開啓主服務器上的keepalived服務,此時虛擬ip再次漂移到主服務器。
百萬PV網站架構案例sql

3、Tomcat安裝

兩臺節點服務器安裝過程同樣。shell

(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 //修改默認網頁內容

<h1>this is server 142!!</h1>

輸入調度器地址,也就是虛擬地址http://192.168.174.110/ ,測試兩臺節點的調度狀況
百萬PV網站架構案例

當down掉165這臺主機後,查看頁面顯示的信息,此時顯示的是另一臺tomcat服務器的首頁
百萬PV網站架構案例

cd /usr/local/tomcat8/conf/

vi server.xml //跳到行尾,在Host name下新增 148

<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>
日誌調試信息debug爲0表示信息越少,docBase指定訪問目錄

4、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 //輸入虛擬地址測試登陸,而且關閉主再測試登陸

百萬PV網站架構案例
百萬PV網站架構案例
百萬PV網站架構案例

5、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
 <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 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配置成功

6、mysql主從配置

一、主服務器上的配置

(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;

三、從服務器配置

(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)
相關文章
相關標籤/搜索