手把手带你搭建百万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).安装更新源

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

2).安装相关软件

[root@master ~]# yum -y install keepalived nginx

3).编辑keepalived主配置文件

[root@master ~]# 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).创建触发脚本

[root@master ~]# mkdir /opt/shell
[root@master ~]# 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

[root@master ~]# chmod +x /opt/shell/nginx.sh #赋予执行权限

5).编辑nginx配置文件
[root@master ~]# 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;        #在此行上方进行添加编辑
}

[root@master ~]# 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

[root@master ~]# systemctl start keepalived.service #启动服务
[root@master ~]# ip addr show ens33
手把手带你搭建百万PV网站架构

2、配置backuper

1).安装epel源

配置与master服务器相同略……

2).安装相关软件

配置与master服务器相同略……

3).编辑keepalived主配置文件

[root@backuper ~]# cd /etc/keepalived/
[root@backuper keepalived]# cp keepalived.conf keepalived.conf.bak #备份源文件
[root@backuper keepalived]# scp [email protected]:/etc/keepalived/keepalived.conf . #下载master服务器配置文件
[root@backuper keepalived]# vim keepalived.conf #编辑主配置文件

route_id NGINX_HB       #注意,服务器id要与master服务器区分
state BACKUP                  #注意,备机定义状态为BACKUP
priority 150                      #注意,活跃值要低于master服务器

4).创建触发脚本

[root@backuper ~]# mkdir /opt/shell
[root@backuper ~]# cd /opt/shell/
[root@backuper shell]# scp [email protected]:/opt/shell/nginx.sh . #下载master服务器脚本文件
[root@backuper ~]# chmod +x /opt/shell/nginx.sh #赋予执行权限

5).编辑nginx配置文件

[root@backuper nginx]# scp [email protected]:/etc/nginx/nginx.conf . #下载master服务器配置文件

6).测试

[root@backuper ~]# systemctl start keepalived.service #启动备机keepalived服务
[root@backuper ~]# systemctl stop keepalived.service #模拟master服务器故障

[root@backuper ~]# ip addr show ens33 #查看备机
手把手带你搭建百万PV网站架构

三、配置Web服务

1、配置tomcat01主机

1).安装C语言编译器

[root@tomcat01 ~]# yum -y install gcc gcc-c++

2).安装jdk

[root@tomcat01 ~]# tar xvfz jdk-8u144-linux-x64.tar.gz
[root@tomcat01 ~]# mv jdk1.8.0_144/ /usr/local/java
[root@tomcat01 ~]# vim /etc/profile #修改环境变量配置文件

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

[root@tomcat01 ~]# source /etc/profile #使配置文件生效

3).查看Java版本

[root@tomcat01 ~]# java -version
手把手带你搭建百万PV网站架构

4).安装Tomcat

[root@tomcat01 ~]# tar xvfz apache-tomcat-8.5.23.tar.gz
[root@tomcat01 ~]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8

5).优化命令路径

[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tomcat01 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown

6).启动服务

[root@tomcat01 ~]# tomcatup
[root@tomcat01 ~]# netstat -anpt | grep ':8080'
手把手带你搭建百万PV网站架构

7).编辑默认首页

[root@tomcat01 ~]# 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>

[root@tomcat01 ~]# 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).重启服务

[root@tomcat01 ~]# tomcatdown #关闭
[root@tomcat01 ~]# tomcatup #启动

四、配置tomcat02主机Web服务

1到6搭建步骤与tomcat01服务器相同,略……

7、编辑默认首页

[root@tomcat02 ~]# 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服务

[root@master ~]# yum install -y mariadb-server mariadb

[root@master ~]# systemctl start mariadb.service
[root@master ~]# systemctl enable mariadb.service

[root@master ~]# 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!

[root@master ~]# mysql -u root -p < slsaledb-2014-4-10.sql #导入项目所需要的数据信息
[root@master ~]# 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、解压项目

[root@tomcat01 ~]# tar xvfz SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/ #解压项目

2、修改数据库配置文件

[root@tomcat01 ~]# cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/
[root@tomcat01 classes]# vim jdbc.properties #修改连接数据库配置文件
手把手带你搭建百万PV网站架构

3、重启tomcat服务

[root@tomcat01 classes]# tomcatdown #关闭
[root@tomcat01 classes]# tomcatup #启动

4、访问商城网站

手把手带你搭建百万PV网站架构

手把手带你搭建百万PV网站架构

七、配置rdis主从同步

1、配置Master

1).安装redis

[root@master ~]# yum install -y epel-release #下载epel源
[root@master ~]# yum -y install redis #安装redis软件

2).编辑主配置文件

[root@master ~]# vim /etc/redis.conf

bind 0.0.0.0                                                         #约61行,监听的地址改为任意的ip

3).启动服务

[root@master ~]# systemctl start redis.service #启动服务
[root@master ~]# netstat -anpt | grep 6379
手把手带你搭建百万PV网站架构

4).连接redis服务

[root@master ~]# 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).编辑主配置文件

[root@backuper ~]# vim /etc/redis.conf

bind 0.0.0.0                                                         #约61行,监听的地址改为任意的ip
slaveof 192.168.100.71 6379                          #约266行,设置主从同步,添加主服务器ip以及对应的端口

2).连接服务

[root@backuper ~]# redis-cli -h 192.168.100.72 -p 6379
手把手带你搭建百万PV网站架构

九、配置web服务器连接redis(两台都要改)

1、编辑ssm框架mybatis集成配置文件

[root@tomcat01 ~]# 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服务

[root@tomcat01 classes]# tomcatdown #关闭
[root@tomcat01 classes]# tomcatup #启动

3、连接redis

[root@master ~]# redis-cli -h 192.168.100.100 -p 6379
192.168.100.100:6379> info #redis服务器基本信息及其状态
手把手带你搭建百万PV网站架构

4、访问商场网站

手把手带你搭建百万PV网站架构

5、再次查看redis信息

[root@master ~]# redis-cli -h 192.168.100.100 -p 6379

192.168.100.100:6379> info  

手把手带你搭建百万PV网站架构

十、配置redis群集

1、查看主服务器信息

[root@master ~]# redis-cli -h 192.168.100.71 info Replication
手把手带你搭建百万PV网站架构

2、编辑redis哨兵配置文件

[root@master ~]# 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、启动群集

[root@master ~]# systemctl start redis-sentinel #启动群集
[root@master ~]# netstat -anpt | grep 26379
手把手带你搭建百万PV网站架构

4、模拟故障,关闭master服务

[root@master ~]# systemctl stop redis #master服务器上关闭redis
[root@master ~]# redis-cli -h 192.168.100.71 -p 26379 info Sentinel #查看集群信息
手把手带你搭建百万PV网站架构

5、添加测试数据

[root@backuper ~]# 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、重启相关服务

[root@backuper ~]# systemctl stop redis #关闭从服务器
[root@backuper ~]# systemctl start redis #开启从服务器
[root@master ~]# systemctl start redis #开启主服务器

[root@master ~]# redis-cli -h 192.168.100.71 -p 26379 info Sentinel #再次查看redis信息
手把手带你搭建百万PV网站架构

7、查看添加数据

[root@mq01 ~]# redis-cli -h 192.168.100.75 -p 6379 #master故障时添加的数据,此时查看已经显示,已经验证数据同步

192.168.100.75:6379> get product
"nice"

十一、配置mariadb主从复制

1、配置master服务器

1).修改主配置文件

[root@master ~]# 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).重启服务

[root@master ~]# systemctl restart mariadb
[root@master ~]# netstat -anpt | grep 3306

手把手带你搭建百万PV网站架构

3).查看服务器状态

[root@master ~]# 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).授权从服务器

[root@master ~]# 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).修改主配置文件

[root@backuper ~]# 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).重启服务

[root@backuper ~]# 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服务器添加数据

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

2).查看backuper服务

[root@backuper ~]# mysql -u root -p -e "show databases;"
手把手带你搭建百万PV网站架构

猜你喜欢

转载自blog.51cto.com/11905606/2301518
今日推荐