本案例採用四層模式實現,主要分爲前端反向代理、web層、數據庫緩存層和數據庫層。前端
因爲實驗條件限制,本次實驗共打開四臺虛擬機,此處實驗將前端代理層、數據庫緩存層、數據庫層服務搭建在前兩臺虛擬服務器上,web層採用羣集模式,用於單獨放置兩臺虛擬機。故本次實驗實際模型爲了模擬實際環境,服務搭建按照以下拓撲搭建。
java
主機名 | 操做系統 | 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 |
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmmysql
yum install -y keepalived nginxlinux
vi /etc/keepalived/keepalived.confnginx
! 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 } }
mkdir -p /opt/shell
vim /opt/shell/nginx.shweb
#!/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.shredis
vim /etc/nginx/nginx.conf sql
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 -t -c /etc/nginx/nginx.confshell
關閉防火牆和SELinux,準備啓動keepalive,隨後會經過配置文件帶動腳本,啓動Nginx,此處須要注意,若要中止Nginx,則須要先關閉keepalive,而後才能夠。數據庫
netstat -ntap | grep nginx
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
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/
vim /usr/local/tomcat8/webapps/ROOT/index.jsp
<h1>Server 129!!</h1> //注意,web2須要首頁內容不一樣
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 //輸入虛擬地址測試登陸,而且關閉主再測試登陸
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 //進入數據庫
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;
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;
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
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.144.112 -p 6379 //測試鏈接
192.168.144.112:6379> set name test //設置name 值是test
192.168.144.112:6379> get name //獲取name值
vim /etc/redis.conf
bind 0.0.0.0 //61行,修改監聽地址 ... slaveof 192.168.114.112 6379 //266行下添加主服務器的IP,不是虛擬IP
systemctl restart redis.service
redis-cli -h 192.168.144.111 -p 6379
192.168.144.111:6379> get name
"test"
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行
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: 值變化。