简介
PV 即点击量, 通常是衡量一个网络新闻频道或网站甚至一条网络新闻的重要指标. PV 从某种程度上已经成为投资者衡量商业网站表现的最重要的尺度.
PV 是一个访问者在 24 小时到底看了网站的几个页面.
案例描述
本案例设计采用四层模式实现, 主要分为前端反向代理, web 层, 数据库缓存层和数据库层. 前端反向代理层采用主备模式, Web 层采用集群模式, 数据库缓存采用主备模式, 数据库层采用主从模式.
案例环境
主: 192.168.177.145 centos7-1
从: 192.168.177.135 centos7-2
节点 1:192.168.177.132 centos7-3
节点 2:192.168.177.133 centos7-4
主服务器 从服务器
安装带有 nginx rpm 软件包的源
#rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
使用 CentOS 默认仓库完成下面的安装 主
- #yum install -y keepalived nginx
- #VIM /etc/keepalived/keepalived.conf // 从上修改三个参数
- ! Configuration File for keepalived
- vrrp_script nginx {
- script "/opt/shell/nginx.sh"
- interval 2
- } // 添加
- global_defs {
- route_id NGINX_HA // 修改
- }
- vrrp_instance VI_1 {
- state MASTER
- 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.177.188 // 虚拟 IP
- 192.168.200.188
- }
- }
- #mkdir /opt/shell
- #cd /opt/shell
- #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
- #chmod +x /opt/shell/nginx.sh // 变成可执行的脚本
从服务器与主服务器配置一样 (内容要修改)
- #yum install -y keepalived nginx
- #VIM /etc/keepalived/keepalived.conf // 从上修改三个参数
- ! Configuration File for keepalived
- vrrp_script nginx {
- script "/opt/shell/nginx.sh"
- interval 2
- } // 添加
- global_defs {
- route_id NGINX_HB // 修改
- }
- vrrp_instance VI_1 {
- state MASTER
- interface ens33
- virtual_router_id 52
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- track_script {
- nginx
- }
- virtual_ipaddress {
- 192.168.177.188 // 虚拟 IP
- 192.168.200.188
- }
- }
- #mkdir /opt/shell
- #cd /opt/shell
- #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
- #chmod +x /opt/shell/nginx.sh // 变成可执行的脚本
配置 nginx 前端调度功能 (主, 从)
- #VIM /etc/nginx/nginx.conf // 在 #gzip on; 下面添加:
- upstream tomcat_pool {
- server 192.168.177.132:8080;
- server 192.168.177.133:8080;
- ip_hash; #会话稳固功能, 否则无法通过 vip 地址登陆
- }
- server {
- listen 80;
- server_name 192.168.177.188; #虚拟出的 IP
- location / {
- proxy_pass http://tomcat_pool;
- proxy_set_header X-Real-IP $remote_addr;
- }
- }
- #nginx -t -c /etc/nginx/nginx.conf // 测试配置文件语法
- #systemctl start keepalived.service //nginx 启动会等待一会
两台节点都要做 tomcat
- #tar xf apache-tomcat-8.5.23.tar.gz
- #tar xf jdk-8u144-Linux-x64.tar.gz
- #mv apache-tomcat-8.5.23/ /usr/local/tomcat8
- #mv jdk1.8.0_144/ /usr/local/java
- #VIM /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
大型网站架构之百万 PV
- #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
- #VIM /usr/local/tomcat8/webapps/ROOT/index.jsp // 修改默认网页内容
- <h1>Server 132!!!</h1>
- http://192.168.177.132:8080/ // 测试默认测试页是否正常显示 (节点)
- http://192.168.177.133:8080/
http://192.168.177.188/ 输入调度器地址, 也就是虚拟地址, 测试两台节点的调度情况.
将 192.168.177.132 宕机 (tomcatdown), 看看 http://192.168.177.188 / 会不会显示 < h1>Server 133!!!</h1>
- #VIM server.xml // 跳到行尾, 在 Host name 下新增 148 行
- <Context path=""docBase="SLSaleSystem"reloadable="true"debug="0"></Context>
日志调试信息 debug 为 0 表示信息越少, docBase 指定访问目录
MySQL 安装 (主, 从)
- #yum install -y mariadb-server mariadb
- #systemctl start mariadb.service
- #systemctl enable mariadb.service // 开机自启动
- #netstat -anpt | grep 3306
- #mysql_secure_installation // 常规安全设置
- #MySQL -u root -p
导入数据库
先挂载
- #MySQL -u root -p <slsaledb-2014-4-10.sql
- #MySQL -u root -p
- show databases;
- GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY 'abc123';
- flush privileges;
大型网站架构之百万 PV
以下在两台 tomcat 节点做
- #tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
- #cd /usr/local/tomcat8/webapps/SLSaleSystem/Web-INF/classes
- #VIM jdbc.properties // 修改数据库 IP 地址是 VRRP 的虚拟 IP, 以及授权的用户名 root 和密码 abc123.
大型网站架构之百万 PV
网站测试
- http://192.168.177.132:8080/ // 默认的用户名 admin 密码: 123456
- http://192.168.177.133:8080/
- http://192.168.177.188 // 输入虚拟地址测试登录, 并且关闭主再测试登录
Redis 集群 (主, 从)
- #yum install -y epel-release
- #yum install Redis -y
- #VIM /etc/Redis.conf
- bind 0.0.0.0
- #systemctl start Redis.service
- #netstat -anpt | grep 6379
- #Redis-cli -h 192.168.177.145 -p 6379 // 测试连接
- 192.168.177.145:6379> set name test // 设置 name 值是 test
- 192.168.177.145:6379> get name // 获取 name 值
- ```![](https://s1.51cto.com/images/blog/202002/10/60cdc1cfe2d25a18a98e6b0c48fcc7a5.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/202002/10/cce7894d602c1a754b201e5288ce357c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
266 行 slaveof 192.168.177.145 6379 // 主服务器的 IP 不是虚拟 IP
- #Redis-cli -h 192.168.177.135 -p 6379 // 登录从, 获取值, 成功说明主从同步成功
- 192.168.177.135:6379> get name
- "test"
- ## 大型网站架构之百万 PV
配置商城项目中连接 Redis 的参数
- 38 <!--redis 配置 开始 -->
- 47 <constructor-arg value="192.168.177.188"/>
- 48 <constructor-arg value="6379"/>
测试缓存效果
- #Redis-cli -h 192.168.177.188 -p 6379
- 192.168.177.188:6379> info
keyspace_hits:1 或者 keyspace_misses:2// 关注这个值, 命中数和未命中数
登录商城, 然后反复点击需要数据库参与的操作页面, 再回来检查 keyspace_hits 或者 keyspace_misses: 值变化.
- ![](https://s1.51cto.com/images/blog/202002/10/def86231b29d9468d871958b53a29aa0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
- ## 配置 Redis 集群主从切换, 只在主服务器上操作
- #Redis-cli -h 192.168.177.145 info Replication // 获取当前服务器的角色
- #VIM /etc/Redis-sentinel.conf
17 行 protected-mode no17G
68 行 sentinel monitor mymaster 192.168.177.145 6379 1 //1 表示 1 台从 注意: 修改
98 行 sentinel down-after-milliseconds mymaster 3000 // 故障切换时间单位是毫秒
- #service Redis-sentinel start // 启动集群
- #netstat -anpt | grep 26379
- #Redis-cli -h 192.168.177.145 -p 26379 info Sentinel // 查看集群信息
- ## MySQL 主从
MySQL 主服务器配置
- #VIM /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
- #systemctl restart mariadb
- #netstat -anpt | grep 3306
- #MySQL -u root -p
- show master status; // 记录日志文件名称和 位置值
- grant replication slave on . to 'rep'@'192.168.177.%' identified by '123456';
- flush privileges;
大型网站架构之百万 PV
MySQL 从服务器配置
- #VIM /etc/my.cnf //[mysqld] 下
- binlog-ignore-db=MySQL,information_schema
- character_set_server=utf8
- log_bin=mysql_bin
- server_id=2
- log_slave_updates=true
- sync_binlog=1
- #systemctl restart mariadb
- #netstat -anpt | grep 3306
- #MySQL -u root -p
- change master to master_host='192.168.177.145',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
- start slave;
- show slave status\G;
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
大型网站架构之百万 PV
来源: http://www.bubuko.com/infodetail-3414962.html