1. 程式人生 > >手把手帶你搭建百萬PV網站架構

手把手帶你搭建百萬PV網站架構

簡介

PV( page view),即頁面瀏覽量,或點選量;通常是衡量一個網路新聞頻道或網站甚至一條網路新聞的主要指標。對PV的解釋是,一個訪問者在24小時(0點到24點)內到底看了你網站幾個頁面。這裡需要強調:同一個人瀏覽你網站同一個頁面,不重複計算PV量,點100次也算1次。說白了pV就是一個訪問者打開了你的幾個頁面。PV之於網站,就像收視率之於電視,從某種程度上已成為投資者衡量商業網站表現的最重要的尺度。PV的計算:當一個訪問者訪問的時候,記錄他所訪問的頁面和對應的IP,然後確定這個P今天詢問了這個頁面沒有。如果你的網站到了23點,單純P有10萬條的話,每個訪問者平均訪問了3個頁面,那麼pV表的記錄就要有30萬條

架構分析

手把手帶你搭建百萬PV網站架構

專案計劃採用四層模式實現,主要分為前端反向代理層、Web層、資料庫快取層、資料庫層

  • 前端反向代理採用主備模式、Web層採用叢集模式資料庫快取層採用主備模式、資料庫層採用主從模式
  • 前端使用keepalived作為高可用軟體,虛擬IP設定為192.168.100.100(內網)和12.0.0.12(外網),設定一個虛擬內網IP是為了內部各個系統之間的通訊

實驗環境

主機名稱 IP地址 系統版本 用途
master 192.168.100.71 CentOS_7.4_x86_64 前端反向代理主機、redis快取主機、MySQL資料庫
backuper 192.168.100.72 CentOS_7.4_x86_64 前端反向代理主機、redis快取主機、MySQL資料庫
tomcat01 192.168.100.73 CentOS_7.4_x86_64 web服務
tomcat02 192.168.100.74 CentOS_7.4_x86_64 web服務

搭建步驟

一、配置Keepalived、Nginx服務

1、配置Master

1).安裝更新源

[[email protected] ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2).安裝相關軟體

[[email protected] ~]# yum -y install keepalived nginx

3).編輯keepalived主配置檔案

[[email protected] ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
        route_id NGINX_HA
}

vrrp_script nginx {
        script "/opt/shell/nginx.sh"
        interval 2
}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 200
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
}

track_script {
        nginx
}

virtual_ipaddress {
        192.168.100.100
        }
}

4).建立觸發指令碼

[[email protected] ~]# mkdir /opt/shell
[[email protected] ~]# vim /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

[[email protected] ~]# chmod +x /opt/shell/nginx.sh #賦予執行許可權

5).編輯nginx配置檔案
[[email protected] ~]# vim /etc/nginx/nginx.conf

http {
……
       upstream tomcat_pool {
                server 192.168.100.73:8080 ;
                server 192.168.100.74:8080 ;
                ip_hash;           #會話穩固功能,否則無法通過vip地址登陸
        }       
        server {
                listen 80;
                server_name 192.168.100.100; #虛擬出的IP
                location / {
                        proxy_pass http://tomcat_pool;
                        proxy_set_header X-Real-IP $remote_addr;
                }       
        }    
include /etc/nginx/conf.d/*.conf;        #在此行上方進行新增編輯
}

[[email protected] ~]# nginx -t -c /etc/nginx/nginx.conf #檢測語法是否正確

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[[email protected] ~]# systemctl start keepalived.service #啟動服務
[[email protected] ~]# ip addr show ens33
手把手帶你搭建百萬PV網站架構

2、配置backuper

1).安裝epel源

配置與master伺服器相同略……

2).安裝相關軟體

配置與master伺服器相同略……

3).編輯keepalived主配置檔案

[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# cp keepalived.conf keepalived.conf.bak #備份原始檔
[[email protected] keepalived]# scp [email protected]:/etc/keepalived/keepalived.conf . #下載master伺服器配置檔案
[[email protected] keepalived]# vim keepalived.conf #編輯主配置檔案

route_id NGINX_HB       #注意,伺服器id要與master伺服器區分
state BACKUP                  #注意,備機定義狀態為BACKUP
priority 150                      #注意,活躍值要低於master伺服器

4).建立觸發指令碼

[[email protected] ~]# mkdir /opt/shell
[[email protected] ~]# cd /opt/shell/
[[email protected] shell]# scp [email protected]:/opt/shell/nginx.sh . #下載master伺服器指令碼檔案
[[email protected] ~]# chmod +x /opt/shell/nginx.sh #賦予執行許可權

5).編輯nginx配置檔案

[[email protected] nginx]# scp [email protected]:/etc/nginx/nginx.conf . #下載master伺服器配置檔案

6).測試

[[email protected] ~]# systemctl start keepalived.service #啟動備機keepalived服務
[[email protected] ~]# systemctl stop keepalived.service #模擬master伺服器故障

[[email protected] ~]# ip addr show ens33 #檢視備機
手把手帶你搭建百萬PV網站架構

三、配置Web服務

1、配置tomcat01主機

1).安裝C語言編譯器

[[email protected] ~]# yum -y install gcc gcc-c++

2).安裝jdk

[[email protected] ~]# tar xvfz jdk-8u144-linux-x64.tar.gz
[[email protected] ~]# mv jdk1.8.0_144/ /usr/local/java
[[email protected] ~]# vim /etc/profile #修改環境變數配置檔案

#末尾新增以下四行
JAVA_HOME=/usr/local/java
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

[[email protected] ~]# source /etc/profile #使配置檔案生效

3).檢視Java版本

[[email protected] ~]# java -version
手把手帶你搭建百萬PV網站架構

4).安裝Tomcat

[[email protected] ~]# tar xvfz apache-tomcat-8.5.23.tar.gz
[[email protected] ~]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8

5).優化命令路徑

[[email protected] ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[[email protected] ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown

6).啟動服務

[[email protected] ~]# tomcatup
[[email protected] ~]# netstat -anpt | grep ':8080'
手把手帶你搭建百萬PV網站架構

7).編輯預設首頁

[[email protected] ~]# vim /usr/local/tomcat8/webapps/ROOT/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
    <head>
        <title>Test JSP Page</title>
    </head>
    <body>
        <% out.println("this is tomcat01 AAAAAAAAAA"); %>
    </body>
</html>

[[email protected] ~]# vim /usr/local/tomcat8/conf/server.xml

<Host name="localhost"  appBase="webapps  unpackWARs="true" autoDeploy="true"> #約149行,在此行下新增以下標籤
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>     #docBase指定訪問目錄;日誌除錯資訊debug為0表示資訊越少,

8).重啟服務

[[email protected] ~]# tomcatdown #關閉
[[email protected] ~]# tomcatup #啟動

四、配置tomcat02主機Web服務

1到6搭建步驟與tomcat01伺服器相同,略……

7、編輯預設首頁

[[email protected] ~]# vim /usr/local/tomcat8/webapps/ROOT/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
    <head>
        <title>Test JSP Page</title>
    </head>
    <body>
        <% out.println("this is tomcat02 BBBBBBBBBB"); %>
    </body>
</html>

五、配置Master、backuper主機Mariadb服務

[[email protected] ~]# yum install -y mariadb-server mariadb

[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# systemctl enable mariadb.service

[[email protected] ~]# mysql_secure_installation #安全設定初始化

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y   #是否要設定root密碼
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  #是否要刪除匿名使用者,"n"表示不刪除匿名使用者
 ... skipping!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y   #是否遠端禁止root登入
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y  #是否刪除測試資料庫並訪問它
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y    #是否此時重新載入許可權表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

[[email protected] ~]# mysql -u root -p < slsaledb-2014-4-10.sql #匯入專案所需要的資料資訊
[[email protected] ~]# mysql -uroot -p
MariaDB [(none)]> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| slsaledb           |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> grant all on slsaledb.* to 'root'@'%' identified by '123'; #授權使用者
MariaDB [(none)]> flush privileges; #重新載入許可權表

六、部署web伺服器商城專案(兩臺tomcat伺服器都要部署)

1、解壓專案

[[email protected] ~]# tar xvfz SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/ #解壓專案

2、修改資料庫配置檔案

[[email protected] ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[[email protected] classes]# vim jdbc.properties #修改連線資料庫配置檔案
手把手帶你搭建百萬PV網站架構

3、重啟tomcat服務

[[email protected] classes]# tomcatdown #關閉
[[email protected] classes]# tomcatup #啟動

4、訪問商城網站

手把手帶你搭建百萬PV網站架構

手把手帶你搭建百萬PV網站架構

七、配置rdis主從同步

1、配置Master

1).安裝redis

[[email protected] ~]# yum install -y epel-release #下載epel源
[[email protected] ~]# yum -y install redis #安裝redis軟體

2).編輯主配置檔案

[[email protected] ~]# vim /etc/redis.conf

bind 0.0.0.0                                                         #約61行,監聽的地址改為任意的ip

3).啟動服務

[[email protected] ~]# systemctl start redis.service #啟動服務
[[email protected] ~]# netstat -anpt | grep 6379
手把手帶你搭建百萬PV網站架構

4).連線redis服務

[[email protected] ~]# redis-cli -h 192.168.100.71 -p 6379

192.168.100.71:6379> set name hello      #設定"name"的值為"hello"
OK
192.168.100.71:6379> get name             #檢視key為"name"對應的value值
"hello"

2、配置backuper

注意相同點略……

1).編輯主配置檔案

[[email protected] ~]# vim /etc/redis.conf

bind 0.0.0.0                                                         #約61行,監聽的地址改為任意的ip
slaveof 192.168.100.71 6379                          #約266行,設定主從同步,新增主伺服器ip以及對應的埠

2).連線服務

[[email protected] ~]# redis-cli -h 192.168.100.72 -p 6379
手把手帶你搭建百萬PV網站架構

九、配置web伺服器連線redis(兩臺都要改)

1、編輯ssm框架mybatis整合配置檔案

[[email protected] ~]# vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml

#編輯以下引數
<!--redis 配置 開始-->    #找到此行
 <constructor-arg value="192.168.100.100"/>   #約47行左右,編輯快取伺服器地址(這裡設定為虛擬ip)
<constructor-arg value="6379"/>                         #約48行左右,編輯快取伺服器埠6379                 

2、重啟tomcat服務

[[email protected] classes]# tomcatdown #關閉
[[email protected] classes]# tomcatup #啟動

3、連線redis

[[email protected] ~]# redis-cli -h 192.168.100.100 -p 6379
192.168.100.100:6379> info #redis伺服器基本資訊及其狀態
手把手帶你搭建百萬PV網站架構

4、訪問商場網站

手把手帶你搭建百萬PV網站架構

5、再次檢視redis資訊

[[email protected] ~]# redis-cli -h 192.168.100.100 -p 6379

192.168.100.100:6379> info  

手把手帶你搭建百萬PV網站架構

十、配置redis群集

1、檢視主伺服器資訊

[[email protected] ~]# redis-cli -h 192.168.100.71 info Replication
手把手帶你搭建百萬PV網站架構

2、編輯redis哨兵配置檔案

[[email protected] ~]# vim /etc/redis-sentinel.conf

protected-mode no      #約17行,關閉保護模式,允許公網訪問redis cache
sentinel monitor mymaster 192.168.100.71 6379 1 #約68行,指向主伺服器,1表示1臺從伺服器
sentinel down-after-milliseconds mymaster 3000    #約98行,故障切換時間,單位預設為毫秒

3、啟動群集

[[email protected] ~]# systemctl start redis-sentinel #啟動群集
[[email protected] ~]# netstat -anpt | grep 26379
手把手帶你搭建百萬PV網站架構

4、模擬故障,關閉master服務

[[email protected] ~]# systemctl stop redis #master伺服器上關閉redis
[[email protected] ~]# redis-cli -h 192.168.100.71 -p 26379 info Sentinel #檢視叢集資訊
手把手帶你搭建百萬PV網站架構

5、新增測試資料

[[email protected] ~]# redis-cli -h 192.168.100.72 -p 6379

192.168.100.72:6379> set product nice
OK
192.168.100.72:6379> get product
"nice"

6、重啟相關服務

[[email protected] ~]# systemctl stop redis #關閉從伺服器
[[email protected] ~]# systemctl start redis #開啟從伺服器
[[email protected] ~]# systemctl start redis #開啟主伺服器

[[email protected] ~]# redis-cli -h 192.168.100.71 -p 26379 info Sentinel #再次檢視redis資訊
手把手帶你搭建百萬PV網站架構

7、檢視新增資料

[[email protected] ~]# redis-cli -h 192.168.100.75 -p 6379 #master故障時新增的資料,此時檢視已經顯示,已經驗證資料同步

192.168.100.75:6379> get product
"nice"

十一、配置mariadb主從複製

1、配置master伺服器

1).修改主配置檔案

[[email protected] ~]# vim /etc/my.cnf

#在[msyqld]標籤下新增以下引數
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).重啟服務

[[email protected] ~]# systemctl restart mariadb
[[email protected] ~]# netstat -anpt | grep 3306

手把手帶你搭建百萬PV網站架構

3).檢視伺服器狀態

[[email protected] ~]# mysql -u root -p -e "show master status;"

Enter password: 
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      470 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

4).授權從伺服器

[[email protected] ~]# mysql -u root -p
MariaDB [(none)]> grant replication slave on . to 'rep'@'192.168.100.%' identified by '123';
MariaDB [(none)]> flush privileges;

2、配置backuper伺服器

1).修改主配置檔案

[[email protected] ~]# vim /etc/my.cnf

#在[msyqld]標籤下新增以下引數
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=2              #注意id要和主伺服器區分
log_slave_updates=true
sync_binlog=1

2).重啟服務

[[email protected] ~]# systemctl restart mariadb

3).配置主從同步

MariaDB [(none)]> change master to master_host='192.168.100.71',master_user='rep',master_password='123',master_log_file='mysql_bin.000001',master_log_pos=470
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
手把手帶你搭建百萬PV網站架構

3、驗證主從同步

1).master伺服器新增資料

[[email protected] ~]# mysql -u root -p
MariaDB [(none)]> create database school;

2).檢視backuper服務

[[email protected] ~]# mysql -u root -p -e "show databases;"
手把手帶你搭建百萬PV網站架構