大型网站架构之百万PV网站架构

栏目: 服务器 · 发布时间: 7年前

内容简介:PV(page view)即页面浏览量,通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。网页浏览数是评价网站流量最常用的指标之一,简称为PV。

PV(page view)即页面浏览量,通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。网页浏览数是评价网站流量最常用的指标之一,简称为PV。

  • 案例概述

    本案例设计采用四层模式来实现,主要分为前端反向代理、web层、数据库缓存层、数据库层。

    前端反向代理层采用主备模式,Web层采用群集模式,数据库缓存层采用主备模式,数据库层采用主备模式

  • 架构拓扑图:实现是正常情况数据流向,虚线是异常情况下的数据流向。

    大型网站架构之百万PV网站架构
  • 电脑配置有限 这里就将代理层、 redis 缓存数据层、 mysql 数据库层 合并在一起了
  • 案例所用的软件包:点击下载

主机名 IP 用途
master 192.168.200.128 前端nginx反向代理主机、redis缓存主机、mysql数据主库
backup 192.168.200.129 前端nginx反向代理备机、redis缓存备机、mysql数据备库
web1 192.168.200.130 tomcat服务器、显示网站
web2 192.168.200.131 tomcat服务器、显示网站
VIP 192.168.200.200

  • master、backup 服务器上配置
  • keepalived+nginx安装配置
  • 安装带有nginx rpm软件包的源

    # systemctl stop firewalld.service
    # setenforce 0
    # rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/\
    #  nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • 使用centos 默认仓库完成下面的安装
  • 注意主从配置 3处不同,已经标注

    # yum install -y keepalived nginx
    # vi /etc/keepalived/keepalived.conf     //从上修改三个参数
    
    ! Configuration File for keepalived
    
    global_defs {
        route_id NGINX_HA              //备份为 NGINX_HB
    }  //下面删除4行
    
    //触发脚本↓↓
    vrrp_script nginx {
        script "/opt/shell/nginx.sh"   
        interval 2
    }
    
    vrrp_instance VI_1 {
        state MASTER               //备份为BACKUP
        interface ens33
        virtual_router_id 51        
        priority 100                 //备份优先级小于主
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
    }
    
    track_script {
        nginx
    }
    
    virtual_ipaddress {
        192.168.200.200
        }
    }
    
    # mkdir /opt/shell
    # vi /opt/shell/nginx.sh      //编写触发nginx启动的脚本
            #!/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
  • 配置nginx前端调度功能
  • 主备配置一样

    # vi /etc/nginx/nginx.conf //在include 上面一行新增
    
     upstream tomcat_pool {
                                    server 192.168.200.130:8080;    //web节点服务器
                                    server 192.168.200.131:8080;
                                    ip_hash;           #会话稳固功能,否则无法通过vip地址登陆
                    }
                    server {
                                    listen 80;
                                    server_name 192.168.200.200; #虚拟出的IP  
                                    location / {
                                                    proxy_pass http://tomcat_pool;
                                                    proxy_set_header X-Real-IP $remote_addr;
                                    }
        }

    大型网站架构之百万PV网站架构

    # nginx -t -c /etc/nginx/nginx.conf  //测试配置文件语法
    # systemctl start keepalived.service    //nginx启动会等待一会
  • master、backup 服务器上配置
  • 数据库的安装配置

    # yum install -y mariadb-server mariadb
    # systemctl start mariadb.service
    # systemctl enable mariadb.service
    # netstat -anpt | grep 3306
    # mysql_secure_installation    //常规安全设置输入( 回车 n n n n y)
  • 导入数据库

    # mysql -u root -p  < slsaledb-2014-4-10.sql
    # mysql -uroot -p
    # show databases;
    > GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123';
    > flush privileges;
  • web服务器上配置
  • 安装配置tomcat (节点服务器 两台都要做)

    # systemctl stop firewalld.service
    # setenforce 0
    # tar xf apache-tomcat-8.5.23.tar.gz -C /opt
    # tar xf jdk-8u144-linux-x64.tar.gz -C /opt
    # cp -rv jdk1.8.0_144/ /usr/local/java
    # vi /etc/profile   //末尾添加下面4行
        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    //查看版本
    # 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    //启动tomcat
    # netstat -anpt | grep 8080  //查看端口是否启动

大型网站架构之百万PV网站架构

# vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默认网页内容
            <h1>Server 130</h1>   //节点1的网页内容

            <h1>Server 131</h1>  //节点2的网页内容
  • 在浏览器测试页面
  • http://192.168.200.200/ 输入调度器地址,也就是虚拟地址,测试两台节点的调度情况。

大型网站架构之百万PV网站架构 大型网站架构之百万PV网站架构

# cd /usr/local/tomcat8/conf/
# vi server.xml   //跳到行尾,在Host name下新增  在149行
    <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>
日志调试信息debug为0表示信息越少,docBase指定访问目录       # tomcatup    //启动tomcat

大型网站架构之百万PV网站架构

  • 搭建商城网站

    # 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。
    # tomcatdown    //停止tomcat
    # tomcatup    //启动tomcat

大型网站架构之百万PV网站架构

大型网站架构之百万PV网站架构 大型网站架构之百万PV网站架构 大型网站架构之百万PV网站架构

  • master、backup 服务器上配置
  • 部署redis主从和群集

    # yum install -y epel-release
    # yum install redis -y
    # cat /etc/redis.conf | grep -v "^#" | grep -v "^$"
    # vi /etc/redis.conf
        bind 0.0.0.0
        从服务器上266行多如下一行配置
        slaveof 192.168.200.128  6379  //主服务器的IP不是虚拟IP
    
    # systemctl start redis.service
    # netstat -anpt | grep 6379
    # redis-cli -h 192.168.200.128 -p 6379 //测试连接
    192.168.200.128:6379> set name test  //设置name 值是test
    192.168.200.128:6379> get name  //获取name值
    "test"
    # redis-cli -h 192.168.200.129 -p 6379 //登录从,获取值,成功说明主从同步成功
    192.168.200.129:6379> get name
    "test"
  • web服务器上配置
  • 配置商城项目中连接redis的参数

    # vi /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
        47                 <constructor-arg value="192.168.200.200"/>
        48                 <constructor-arg value="6379"/>
    # tomcatdown    //停止tomcat
    # tomcatup    //启动tomcat

大型网站架构之百万PV网站架构

  • master、backup 服务器上配置
  • 测试redis缓存效果
  • 登录商城,然后反复点击需要数据库参与的操作页面,再回来检查keyspace_hits或者keyspace_misses: 值变化
  • keyspace_hits: 或者 keyspace_misses://关注这个值,命中数和未命中数

    # redis-cli -h 192.168.200.200 -p 6379
    192.168.200.200:6379> info

    大型网站架构之百万PV网站架构 大型网站架构之百万PV网站架构

  • 配置redis集群主从切换
  • 只在主服务器是操作

    # redis-cli -h  192.168.200.128 info Replication   //获取当前服务器的角色

大型网站架构之百万PV网站架构

# vi /etc/redis-sentinel.conf (17行  68行 98行)
17 protected-mode no
68 sentinel monitor mymaster 192.168.200.128 6379 1 //1表示1台从 注意:修改
98 sentinel down-after-milliseconds mymaster 3000 //故障切换时间单位是毫秒

# service redis-sentinel start //启动集群
# netstat -anpt | grep 26379
# redis-cli -h 192.168.200.128 -p 26379 info Sentinel  //查看集群信息

大型网站架构之百万PV网站架构 大型网站架构之百万PV网站架构

  • 验证主从切换
  • 在主上

    #  systemctl stop redis.service
    # redis-cli -h 192.168.200.128 -p 26379 info Sentinel //发现主变成了129

大型网站架构之百万PV网站架构

  • master、backup 服务器上配置
  • 配置mysql主从复制
  • mysql主服务器配置

    # 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

    大型网站架构之百万PV网站架构

    # systemctl restart mariadb
    # netstat -anpt | grep 3306
    
    # mysql -u root
    > show master status; //记录日志文件名称和 位置值
    > grant replication slave on *.* to 'rep'@'192.168.200.%' identified by '123456';
    > flush privileges;
  • mysql从服务器配置

    # vi /etc/my.cnf / /[mysqld]下
        server_id=2
    
    # systemctl restart mariadb
    # netstat -anpt | grep 3306
    # mysql -u root
    > change master to master_host='192.168.200.128',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
    > start slave;
    > show slave status\G;

大型网站架构之百万PV网站架构

  • 验证架构
  • 关掉master 浏览网页:192.168.200.200

大型网站架构之百万PV网站架构


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Computation and Programming Using Python

Introduction to Computation and Programming Using Python

John V. Guttag / The MIT Press / 2013-7 / USD 25.00

This book introduces students with little or no prior programming experience to the art of computational problem solving using Python and various Python libraries, including PyLab. It provides student......一起来看看 《Introduction to Computation and Programming Using Python》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具