簡介
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