PV(page view)即頁面瀏覽量,一般是衡量一個網絡新聞頻道或網站甚至一條網絡新聞的主要指標。網頁瀏覽數是評價網站流量最經常使用的指標之一,簡稱爲PV。 前端
案例概述
本案例設計採用四層模式來實現,主要分爲前端反向代理、web層、數據庫緩存層、數據庫層。
前端反向代理層採用主備模式,Web層採用羣集模式,數據庫緩存層採用主備模式,數據庫層採用主備模式java
主機名 | 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 |
安裝帶有nginx rpm軟件包的源mysql
# 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
注意主從配置 3處不一樣,已經標註linux
# 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; } }
# nginx -t -c /etc/nginx/nginx.conf //測試配置文件語法 # systemctl start keepalived.service //nginx啓動會等待一會
數據庫的安裝配置web
# 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)
導入數據庫redis
# 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;
安裝配置tomcat (節點服務器 兩臺都要作)sql
# 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 //查看端口是否啓動
# vi /usr/local/tomcat8/webapps/ROOT/index.jsp //修改默認網頁內容 <h1>Server 130</h1> //節點1的網頁內容 <h1>Server 131</h1> //節點2的網頁內容
# 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
搭建商城網站shell
# 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
部署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"
配置商城項目中鏈接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
keyspace_hits: 或者 keyspace_misses://關注這個值,命中數和未命中數
# redis-cli -h 192.168.200.200 -p 6379 192.168.200.200:6379> info
只在主服務器是操做
# redis-cli -h 192.168.200.128 info Replication //獲取當前服務器的角色
# 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 //查看集羣信息
在主上
# systemctl stop redis.service # redis-cli -h 192.168.200.128 -p 26379 info Sentinel //發現主變成了129 # systemctl start redis.service //驗證切換成功後在開啓服務
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
# 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;