PV(Page View,頁面瀏覽量)即點擊量,一般意義上說PV的多少是衡量一個網絡新聞頻道或網站甚至一條網絡新聞的主要指標。pv的解釋是這樣的:一個訪問者在24小時(0點-23點)內到底看了網站的幾個頁面。須要注意的是:同一我的瀏覽網站的同一個頁面,不重複計算pv量,點擊100次頁只算1次。前端
本次實驗設計採用四層模式實現,主要分爲前端反向代理層、web層、數據庫緩存層和數據庫層。前端反向代理層採用主備模式,web層採用集羣模式,數據庫緩存層採用主備模式,數據庫層採用主從模式。java
主機名 | ip地址 | 角色 |
---|---|---|
master | 192.168.0.106 | nginx+keepalived主、redis主、mysql數據主庫 |
backup | 192.168.0.115 | nginx+keepalived備、redis備、mysql數據從庫 |
web1 | 192.168.0.104 | 後端tomcat服務 |
web2 | 192.168.0.105 | 後端tomcat服務 |
VIP | 192.168.0.188 | 虛擬IP地址 |
全部軟件包點擊下載node
systemctl stop firewalld.service
setenforce 0mysql
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmlinux
yum install -y keepalived nginxnginx
rpm -qa | grep mysqlweb
rpm -qa|grep -i mariadbredis
rpm -qa|grep mariadb|xargs rpm -e --nodepssql
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpmshell
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server -y
yum install -y epel-release #更新epel源
yum install redis -y #安裝redis
systemctl stop firewalld.service #關閉防火牆
setenforce 0
tar xf jdk-8u101-linux-x64.tar.gz -C /usr/local
cd /usr/local/
mv jdk1.8.0_101 java
sed -i '$aJAVA_HOME=/usr/local/java\nCLASSPATH=$JAVA_HOME/lib\nPATH=$PATH:$JAVA_HOME/bin\nexport PATH JAVA_HOME CLASSPATH' /etc/profile
source /etc/profile
java -version
tar xvf apache-tomcat-8.5.43.tar.gz -C /usr/local #解壓
cd /usr/local
mv apache-tomcat-8.5.43 tomcat #重命名
chmod +x tomcat/bin/* #給腳本受權
ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/tomcatup #創建軟鏈接
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/tomcatdown
vim /etc/keepalived/keepalived.conf #修改三個參數
! Configuration File for keepalived global_defs { route_id NGINX_HA #備機要改 NGINX_HB } vrrp_script nginx { #nginx監控腳本,後面會建立 script "/opt/shell/nginx.sh" interval 2 } vrrp_instance VI_1 { state MASTER #備機改成BACKUP interface ens33 virtual_router_id 53 #2臺必須同樣 priority 100 #優先級,備機要比這個小,如90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { nginx } virtual_ipaddress { 192.168.0.188 #虛擬ip地址 } }
vim /etc/nginx/nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream tomcatpool { server 192.168.0.105:8080; #節點服務器1的ip地址,端口號 server 192.168.0.104:8080; #節點服務器2的ip地址,端口號 ip_hash; #會話穩固功能,不然沒法經過vip地址登錄 } server { listen 80; server_name 192.168.0.188; #虛擬ip地址 location / { proxy_pass http://tomcatpool; proxy_set_header X-Real-IP $remote_addr; } } include /etc/nginx/conf.d/*.conf; }
mkdir /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 #腳本受權
備註:2.臺nginx服務配置,nginx腳本都同樣!
配置都沒有報錯,vip也實現漂移了,但網頁打開就是報錯,查看nginx日誌,也看到不到啥問題
配置完成後,2臺都沒有出現VIP(虛擬ip)
1.keepalived配置文件不對,2臺state必須一主一備,不能都爲master
2.2臺virtual_router_id必須相同
3.以上2點須要都知足才行
配置沒有報錯,也看到vip漂移了,在後面使用web服務鏈接數據庫,使用vip地址鏈接,不能登陸網站,只能看到網頁。可是,使用數據庫真實ip地址就能夠登陸網頁。
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 start mysqld #啓動服務
mysql < slsaledb-2014-4-10.sql #導入商城數據
mysql
show databases; #查看數據是否導入
GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '123456'; #slsaledb數據庫受權
grant replication slave on *.* to 'rep'@'192.168.0.%' identified by '123456'; #從庫受權
flush privileges;
show master status;
vim /etc/my.cnf
在[mysqld]下加入
server_id=2 relay-log=relay-log-bin relay-log-index=slave-relay-bin.index
systemctl start mysqld #啓動服務
mysql < slsaledb-2014-4-10.sql #導入商城數據
mysql
show databases; #查看數據是否導入
GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '123456'; #slsaledb數據庫受權
mysql
change master to master_host='192.168.0.106',master_user='rep',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=406;
start slave;
show slave status \G
tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat/webapps/
vim /usr/local/tomcat/conf/server.xml
在Host name下新增 在149行
#做用是讓用戶訪問時不使用項目名能夠直接訪問 <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>
cd /usr/local/tomcat/webapps/SLSaleSystem/WEB-INF/classes
vim jdbc.properties
driverClassName=com.mysql.jdbc.Driver #填寫虛擬ip地址 url=jdbc\:mysql\://192.168.0.188\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8 #填寫受權時的用戶名密碼 uname=root password=123456 minIdle=10 maxIdle=50 initialSize=5 maxActive=100 maxWait=100 removeAbandonedTimeout=180 removeAbandoned=true
tomcatup
netstat -ntap |grep 8080
1.先訪問2臺節點服務器網址
http://192.168.0.104:8080
http://192.168.0.105:8080
2.在訪問虛擬IP地址
http://192.168.0.188
vim /etc/redis.conf
在61行修改成
bind 0.0.0.0
systemctl start redis.service #啓動服務
netstat -anpt | grep 6379
redis-cli -h 192.168.0.106 -p 6379 #測試鏈接
192.168.0.106:6379> set name test
OK
192.168.0.106:6379> get name
"test"
192.168.0.106:6379> exit
vi /etc/redis.conf
bind 0.0.0.0 #61行 slaveof 192.168.0.106 6379 #266行,主服務器的IP
systemctl start redis.service #啓動服務
redis-cli -h 192.168.0.115 -p 6379 #查看
192.168.0.115:6379> get name
"test" #同步成功
192.168.0.115:6379> exit
配置商城項目中鏈接redis的參數
vi /usr/local/tomcat/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
#填寫虛擬IP地址,47行 <constructor-arg value="192.168.0.188"/> <constructor-arg value="6379"/>
tomcatdown
tomcatup
redis-cli -h 192.168.0.188 -p 6379
192.168.0.188:6379> info
只在主服務器是操做
redis-cli -h 192.168.0.106 info Replication #獲取當前服務器的角色
vim /etc/redis-sentinel.conf
protected-mode no #17 行 sentinel monitor mymaster 192.168.200.128 6379 1 #69行,1表示1臺備機 sentinel down-after-milliseconds mymaster 3000 #98行,故障切換時間單位是毫秒
service redis-sentinel start #啓動集羣
netstat -anpt | grep 26379
redis-cli -h 192.168.0.106 -p 26379 info Sentinel #查看集羣信息
在主服務器上(master)
systemctl stop redis.service
redis-cli -h 192.168.0.106 -p 26379 info Sentinel #查看集羣信息
關掉全部主服務器(master)和一臺tomcat服務(we1)
查看網頁: http://192.168.0.188