記CentOS7一個百萬PV商城的測試

PV前端

PV(Page View,頁面瀏覽量)即點擊量,一般意義上說PV的多少是衡量一個網絡新聞頻道或網站甚至一條網絡新聞的主要指標。pv的解釋是這樣的:一個訪問者在24小時(0點-23點)內到底看了網站的幾個頁面。須要注意的是:同一我的瀏覽網站的同一個頁面,不重複計算pv量,點擊100次頁只算1次。
java

實驗概述mysql

採用四層模式實現,主要分爲前端反向代理層、Web層、數據庫緩存層和數據庫層。前端反向代理層層採用主備模式,Web層採用羣集模式,數據庫緩存層採用主備模式,數據庫層採用主從模式。linux

 

實驗環境表nginx


主機名 IP地址 用途
CentOS7-1(master) 192.168.10.10 nginx反向代理(主)、redis緩存處理器(主)、mysql數據庫(主)
CentOS7-2(backup) 192.168.10.11 nginx反向代理(備)、redis緩存處理器(備)、mysql數據庫(從)
CentOS7-3(tomcat1) 192.168.10.12 tomcat(主)
CentOS7-4(tomcat2) 192.168.10.13 tomcat(備)

 

實驗步驟 web

安裝部署nginx、keepalived服務(主從服務器7-1,7-2)

關閉防火牆及SELinuxredis

systemctl stop firewalld.service
setenforce 0sql

安裝帶有nginx的rpm軟件包的源shell

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm數據庫

使用yum倉庫安裝nginx、keepalived服務

yum install -y keepalived nginx

修改keepalived配置文件

global_defs {

router_id NGINX_A                  #修改成不一樣名稱,我設置爲NGINX_B

}

vrrp_script nginx {                    #定義腳本
    script "/opt/shell/nginx.sh"      #添加建立腳本
    interval 2                                #每2s自動執行上面的腳本
}

vrrp_instance VI_1 {
    state MASTER                  #主服務器master,從服務器爲backup
    interface ens33                  #改成本身的網卡名
    virtual_router_id 51     #從服務器ID要不一樣,我設置爲52
    priority 100                  #從服務器低於主服務器優先級,我設置爲90
}

track_script {                    #調用上面的腳本

nginx

}

virtual_ipaddress {
    193.168.10.150             #定義虛擬IP
    }
}

#下面多餘的配置文件能夠刪除

這裏方便修改能夠吧主服務器的配置文件直接複製到從服務器,而後在作微調便可。

scp /etc/keepalived/keepalived.conf root@192.168.10.11:/etc/keepalived/

NGINX的啓動腳本

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

nginx返向代理配置

vim /etc/nginx/nginx.conf

upstream tomcat_pool{
        server 192.168.10.12:8080;                    #兩臺tomcat服務器的IP地址
        server 192.168.10.13:8080;
        ip_hash;               #會話穩固功能
}
    server {
        listen 80;
        server_name 192.168.10.150;       #虛擬IP
        location / {
                        proxy_pass http://tomcat_pool;
                        proxy_set_header X-Real-IP $remote_addr;
                }
    }

測試一下語法

[root@localhost ~]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

開啓keepalived服務,檢測nginx是否隨之啓動

systemctl start keepalived.service

netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      75256/nginx: master

 

部署tomcat服務(兩臺web節點服務器7-3,7-4)

搭建java環境,安裝tomcat

tar xf apache-tomcat-8.5.23.tar.gz -C /usr/local/
tar xf jdk-8u144-linux-x64.tar.gz -C /usr/local/

重命名

cd /usr/local/
mv jdk1.8.0_144/ java
mv apache-tomcat-8.5.23/ tomcat8

添加環境變量

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環境是否安裝成功

# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

apached的優化與軟連接的建立

ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown

這時候已經能夠用VIP訪問站點了。

11016233004


 

修改server.xml配置文件

vim /usr/local/tomcat8/conf/server.xml

148行

<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>

 

mysql數據庫的安裝部署(主從服務器7-1,7-2)

爲了安裝更輕便,這裏使用mariadb代替MySQL數據庫

yum install -y mariadb-server mariadb

systemctl start mariadb.service
systemctl enable mariadb.service

mariadb的常規安全設置


[root@localhost shell]# mysql_secure_installation      #開始常規安全設置

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):    #默認空密碼,直接回車
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y      #建立新密碼
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n   #不刪除匿名用戶
... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n      #遠程ROOT登錄
... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n    #刪除測試數據庫
... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y     #從新加載權限表
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

導入會員商城數據庫至mariadb數據庫

mysql -u root -p < slsaledb.sql

確認mysql數據並受權

GRANT all ON slsaledb.* TO 'root'@'%' IDENTIFIED BY '1';

FLUSH PRIVILEGES;

節點服務器上部署商城項目(7-3,7-4)

解壓會員商城的軟件包至tomcat首頁站點目錄下,並修改配置文件

tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/jdbc.properties    #修改以下

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.10.150\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=root
password=1
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true

重啓tomcat服務,登陸網頁測試


11017000325

 

安裝部署redis數據庫緩存服務(主從服務器7-1,7-2)

yum倉庫安裝redis服務

yum install epel-release -y

yum -y install redis

修改redis配置文件

vim /etc/redis.conf

61行
bind 0.0.0.0        #修改0.0.0.0   監聽全部地址
 
從服務器要多修改一條
266行,添加

slaveof 192.168.10.10 6379

開啓redis服務,進行主從同步測試

systemctl start redis.service

redis-cli -h 192.168.10.10 -p 6379            #登陸主redis服務
set name test      #設置name的值爲test
getname           #獲取name值
 
redis-cli -h 192.168.10.11 -p 6379      #登陸從redis服務
get name      #獲取name值

配置商城項目中鏈接redis的參數(兩個節點服務器中 )

vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml

47行左右
<constructor-arg value="192.168.10.150"/>   #VIP地址

redis主備(主redis上配置)

vim /etc/redis-sentinel.conf

17行 protected-mode no
68行 sentinel monitor mymaster 192.168.10.10 6379 1 //表示1臺從服務
98行 sentinel down-after-milliseconds mymaster 2000 //故障切換時間單位是毫秒

開啓集羣服務

systemctl start redis-sentinel.service

redis-cli -h 192.168.10.10 -p 26379 info Sentinel #查看集羣信息

mysql主從同步部署

修改配置文件

vim /etc/my.cnf

[mysqld]下
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1                   #主從需不同,從我寫了2
log_slave_updates=true
sync_binlog=1

systemctl restart mariadb.service

查看主服務器的日誌記錄文件和偏移量

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> grant replication slave on *.* to 'moo'@'192.168.10.%' identified by '1';
Query OK, 0 rows affected (0.01 sec)

從服務器

MariaDB [(none)]> change master to master_host='192.168.10.10',master_user='moo',master_password='1',master_log_file='mysql_bin.000001',master_log_pos=245;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show slave status\G;

Slave_IO_Running: Yes

Slave_SQL_Running: Yes     #肯定這兩項爲yes

在主服務器關閉keepalived,仍然能訪問商城

 

11017004943

百萬pv商城網站完成!

相關文章
相關標籤/搜索