大型網站架構之百萬PV網站架構案例
本案例采用四層模式實現,主要分為前端反向代理、web層、數據庫緩存層和數據庫層。
- 前端反向代理采用主備模式
- web層采用群集模式
- 數據庫緩存層采用主備模式
- 數據庫層采用主從模式
由於實驗條件限制,本次實驗共打開四臺虛擬機,此處實驗將前端代理層、數據庫緩存層、數據庫層服務搭建在前兩臺虛擬服務器上,web層采用群集模式,用於單獨放置兩臺虛擬機。故本次實驗實際模型為了模擬實際環境,服務搭建按照如下拓撲搭建。
二、實驗環境
主機名 | 操作系統 | IP地址 | 用途 |
---|---|---|---|
server1 | centosx84_64 | 192.168.144.112 | 前端反向代理Nginx、Redis緩存主機、MySQL主數據庫 |
server2 | centosx84_64 | 192.168.144.111 | 前端反向代理備Nginx、Redis備緩存主機、MySQL備數據庫 |
web1 | centosx84_64 | 192.168.144.113 | Web服務tomcat |
web2 | centosx84_64 | 192.168.144.114 | Web服務tomcat |
三、實驗部署
3.1、master和slave設置keepalive與Nginx反向代理
- 配置yum源
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安裝keepalive軟件和Nginx
yum install -y keepalived nginx
3.1.1配置keepalive
- 配置主從的keepalive,兩臺配置基本相同,配置文件不同處已做說明
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { route_id NGINX_HA //主從不同 } vrrp_script nginx { script "/opt/shell/nginx.sh" //配置帶動Nginx啟動腳本 interval 2 //每隔2s響應 } 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 //調度Nginx啟動腳本的函數名 } virtual_ipaddress { 192.168.144.188 //設置虛擬IP } }
3.1.2創建keepalive帶動Nginx啟動腳本
mkdir -p /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
3.1.3配置Nginx前端調度功能
vim /etc/nginx/nginx.conf
- 在include 上面一行新增
upstream tomcat_pool {
server 192.168.144.113:8080;
server 192.168.144.114:8080;
ip_hash; //會話穩固功能,否則無法通過vip地址登陸
}
server {
listen 80;
server_name 192.168.144.188; //虛擬IP
location / {
proxy_pass http://tomcat_pool;
proxy_set_header X-Real-IP $remote_addr;
}
}
- 配置完成後,檢查Nginx配置文件語法
nginx -t -c /etc/nginx/nginx.conf
-
關閉防火墻和SELinux,準備啟動keepalive,隨後會通過配置文件帶動腳本,啟動Nginx,此處需要註意,若要停止Nginx,則需要先關閉keepalive,然後才可以。
- 查看Nginx是否啟動
netstat -ntap | grep nginx
3.2、部署兩臺web服務器
3.2.1兩臺web服務器部署tomcat,部署步驟完全相同,為了試驗區分,需要在首頁內容做區別
tar xf apache-tomcat-8.5.23.tar.gz
tar xf jdk-8u144-linux-x64.tar.gz
mv jdk1.8.0_144/ /usr/local/java
mv apache-tomcat-8.5.23/ tomcat8
- 解壓完成後,配置jdk環境變量,讓jdk的各種命令為系統識別
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
-
配置tomcat啟動與關閉命令為系統識別
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 8080http://192.168.144.113:8080/ //測試默認測試頁是否正常顯示
http://192.168.144.114:8080/
- 為了試驗區分兩臺web服務器,修改首頁內容
vim /usr/local/tomcat8/webapps/ROOT/index.jsp
<h1>Server 129!!</h1> //註意,web2需要首頁內容不同
- 輸入調度器地址,也就是虛擬地址,測試兩臺節點的調度情況。
http://192.168.175.188/
3.2.2搭建會員商城
- 首先在tomcat配置文件中添加支持商城模塊
cd /usr/local/tomcat8/conf/
vim server.xml
-
跳到行尾,在Host name下新增 在148行位置
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> //日誌調試信息debug為0表示信息越少,docBase指定訪問目錄
- 將會員商城軟件包解壓縮
tar zxvf 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。
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://192.168.144.188\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8 //該成我們設定的虛擬IP
uname=root
password=123456
minIdle=10
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true
- 客戶端測試
http://192.168.144.113:8080/ //默認的用戶名admin 密碼:123456
http://192.168.144.114:8080/
http://192.168.175.188 //輸入虛擬地址測試登錄,並且關閉主再測試登錄
3.3、部署MySQL以及主從
3.3.1安裝mysql
- 在實際生產環境中,此處應當搭建MySQL服務,但由於實驗條件限制,本實驗使用mariadb代替MySQL。
yum install -y mariadb-server mariadb
systemctl start mariadb.service
systemctl enable mariadb.servicenetstat -anpt | grep 3306
mysql_secure_installation //
Set root password? [Y/n] y //設置MySQL管理員賬戶的密碼,我選擇密碼為abc123
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 //刪除test數據庫?
... 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!
mysql -uroot -p //進入數據庫
3.3.2導入數據庫,授權
mysql -u root -p < slsaledb-2014-4-10.sql
mysql -uroot -p
show databases;
GRANT all ON slsaledb.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc123‘; //授予slsaledb數據庫所有表所有權限給root用戶在任意網段登錄,密碼為abc123
flush privileges;
3.3.3 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; //記錄日誌文件名稱和 位置值
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 626 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
grant replication slave on *.* to ‘rep‘@‘192.168.144.%‘ identified by ‘123456‘; //授予主從狀態
flush privileges;
- MySQL從服務器下
vim /etc/my.cnf
[mysqld]下添加
server_id=2
systemctl restart mariadb
netstat -anpt | grep 3306
mysql -u root -p
change master to master_host=‘192.168.144.112‘,master_user=‘rep‘,master_password=‘123456‘,master_log_file=‘mysql_bin.000001‘,master_log_pos=2626;
start slave;
show slave status;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
- 測試主從狀態是否可行,方可進行下一步驟。
3.4部署Redis數據庫緩存層
3.4.1安裝redis
- 很對主從redis數據庫緩存,首先需要安裝redis軟件
yum install -y epel-release //安裝擴展源
yum install redis -y
3.4.2設置主從關系
- 配置主Redis
vim /etc/redis.conf
bind 0.0.0.0 //將監聽網址修改成任意網段
systemctl start redis.service
netstat -anpt | grep 6379
- 測試本身安裝完成能否使用
redis-cli -h 192.168.144.112 -p 6379 //測試連接
192.168.144.112:6379> set name test //設置name 值是test
192.168.144.112:6379> get name //獲取name值
- 配置從Redis
vim /etc/redis.conf
bind 0.0.0.0 //61行,修改監聽地址
...
slaveof 192.168.114.112 6379 //266行下添加主服務器的IP,不是虛擬IP
- 兩臺redis服務器服務重啟
systemctl restart redis.service
- 從服務器上進入redis,發現已經復制完成
redis-cli -h 192.168.144.111 -p 6379
192.168.144.111:6379> get name
"test"
- 至此,redis主從配置完成。
3.4.3配置商城項目中連接redis的參數
- web節點中配置商城項目,指定Redis虛擬IP
vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/applicationContext-mybatis.xml
<!--redis 配置 開始-->
<constructor-arg value="192.168.144.188"/> //47行
<constructor-arg value="6379"/> //48行
3.4.4測試緩存效果
redis-cli -h 192.168.144.188 -p 6379
192.168.175.188:6379> info
keyspace_hits:1 或者 keyspace_misses:2//關註這個值,命中數和未命中數
登錄商城,然後反復點擊需要數據庫參與的操作頁面,再回來檢查keyspace_hits或者keyspace_misses: 值變化。
大型網站架構之百萬PV網站架構案例