1. 程式人生 > 其它 >商城叢集化部署

商城叢集化部署

商城叢集化部署

1.實戰案例 ——構建讀寫分離的資料庫叢集

1.1 案例目標

(1)瞭解 mycat 提供的讀寫分離功能

(2)瞭解 mysql 資料庫的主從架構

(3)構建以 mycat 為中介軟體的讀寫分離資料庫叢集

1.2 案例分析

1.規劃節點

使用 Mycat 作為資料庫中介軟體服務構建讀寫分離的資料庫叢集,節點規劃見下表

2.基礎準備

登入 OpenStack 平臺,使用 CentOS7.5 系統,flavor 使用 2vCPU/4G 記憶體/50G 硬碟,建立三臺虛擬機器進行實驗。

其中兩臺虛擬機器 db1 和 db2 部署 MariaDB 資料庫服務,搭建主從資料庫叢集;一臺作為主節點,負責寫入資料庫資訊;另一臺作為從節點,負責讀取資料庫資訊。

使用一臺虛擬機器部署 Mycat 資料庫中介軟體服務,將使用者提交的讀寫操作識別分發給相應的資料庫節點。這樣將使用者的訪問操作、資料庫的讀與寫操作分給三臺主機,只有資料庫叢集的主節點接收增刪改 SQL 語句,從節點接收查詢語句,分擔了主節點的查詢壓力。

1.3 案例實施

  1. 基礎環境配置

(1)修改主機名

使用 hostnamectl 命令修改三臺主機的主機名。

mycat 節點修改主機名命令:

[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]#

db1 節點修改主機名命令:

[root@localhost ~]# hostnamectl set-hostname db1
[root@localhost ~]# bash
[root@db1 ~]#

db2 節點修改主機名命令:

[root@localhost ~]# hostnamectl set-hostname db2
[root@localhost ~]# bash
[root@db2 ~]#

(2)編輯 hosts 檔案

三臺叢集虛擬機器的/etc/hosts 檔案配置部分:

[root@mycat ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.104.44.42 mycat
10.104.44.16 db1
10.104.44.41 db2

(3)配置 yum 安裝源

資料庫叢集需要安裝 MariaDB 資料庫服務,需要給叢集虛擬機器配置 yum 安裝原始檔,使用提供的 gpmall-repo 檔案上傳至mycat虛擬機器的/opt 目錄下,設定本地 yum 源,然後搭建ftp給db1和db2共享源。

1.上傳gpmall-cluster.tar.gz到mycat的/root目錄下
[root@mycat ~]# curl -o gpmall-cluster.tar.gz http://10.104.44.30/file/k8s/shangcheng/gpmall-cluster.tar.gz

2.將gpmall-cluster.tar.gz包解壓到/opt
[root@mycat ~]# tar -zxvf gpmall-cluster.tar.gz -C /opt/

3.將CentOS-1804.iso上傳到/root
[root@mycat ~]# curl -o CentOS-1804.iso http://10.104.44.30/file/os-images/CentOS-1804.iso

4.在/opt下面建立centos目錄
[root@mycat mnt]# mkdir -p /opt/centos/

5.將CentOS-1804.iso掛載到/mnt
[root@mycat ~]# mount -o loop CentOS-1804.iso /mnt

6.複製/mnt下面所有檔案到/opt/centos
[root@mycat mnt]# cp -rfv * /opt/centos/

7.然後將三個節點/etc/yum.repo.d 目錄下的檔案移動到/media 下,命令如下:

[root@mycat ~]#  mv /etc/yum.repos.d/* /media/

mycat虛擬機器的 yum 安裝原始檔配置部分:

[root@mycat ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=file:///opt/gpmall-cluster/gpmall-repo
gpgcheck=0
enabled=1

db1虛擬機器的 yum 安裝原始檔配置部分:

[root@db1 ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=ftp://mycat/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=ftp://mycat/gpmall-cluster/gpmall-repo
gpgcheck=0
enabled=1

db2虛擬機器的 yum 安裝原始檔配置部分:

[root@db2 ~]# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=ftp://mycat/centos
gpgcheck=0
enabled=1
[mariadb]
name=mariadb
baseurl=ftp://mycat/gpmall-cluster/gpmall-repo
gpgcheck=0
enabled=1

8.安裝vsftpd,vim,bash*,net-tools

[root@mycat ~]# yum -y install vim bash* net-tools vsftpd

9.進入ftp配置檔案,新增第一行

[root@mycat ~]# cat /etc/vsftpd/vsftpd.conf 
anon_root=/opt
# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.

#然後重啟ftp
[root@mycat ~]# systemctl restart vsftpd
[root@mycat ~]# systemctl enable vsftpd

(4)部署 Mycat 中介軟體服務需要先部署 JDK1.7 或以上版本的 JDK 軟體環境,這裡部署JDK1.8 版本。

mycat 節點安裝 java 環境:

[root@mycat ~]#  yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

[root@mycat ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

2.部署 MariaDB 主從資料庫叢集服務

(1)安裝 MariaDB 服務

通過 yum 命令在 db1 和 db2 虛擬機器節點上安裝 MariaDB 服務,命令如下。

[root@db1 ~]#  yum install -y mariadb mariadb-server

[root@db2 ~]#  yum install -y mariadb mariadb-server

兩個節點啟動 MariaDB 服務,並設定 MariaDB 服務為開機自啟。

[root@db1 ~]# systemctl start mariadb
[root@db1 ~]# systemctl enable mariadb

[root@db2 ~]# systemctl start mariadb
[root@db2 ~]# systemctl enable mariadb

(2)初始化 MariaDB 資料庫

在 db1 和 db2 虛擬機器節點上初始化 MariaDB 資料庫,並設定 MariaDB 資料庫 root 訪問
使用者的密碼為 123456。

[root@db1 ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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: #輸入資料庫 root 密碼 123456
Re-enter new password: #重複輸入密碼 123456
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] y
... Success!
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
... 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] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
5
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!

(3)配置資料庫叢集主節點

編輯主節點 db1 虛擬機器的資料庫配置檔案 my.cnf,在配置檔案 my.cnf 中增添下面的內
容。

[root@db1 ~]# cat /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# include *.cnf from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin = mysql-bin #記錄操作日誌
binlog_ignore_db = mysql #不同步 mysql 系統資料庫
server_id = 16  #資料庫叢集中的每個節點 id 都要不同,一般使用 IP 地址的最後段的數字,例如 10.104.44.16,server_id 就寫 16

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

編輯完成配置檔案 my.cnf 後,重啟 MariaDB 服務。

[root@db1 ~]#  systemctl restart mariadb

(4)開放主節點的資料庫許可權

在主節點 db1 虛擬機器上使用 mysql 命令登入 MariaDB 資料庫,授權在任何客戶端機器
上可以以 root 使用者登入到資料庫。

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.3.23-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "123456";
Query OK, 0 rows affected (0.001 sec)

在主節點 db1 資料庫上建立一個 user 使用者讓從節點 db2 連線,並賦予從節點同步主節
點資料庫的許可權,命令如下。

MariaDB [(none)]> grant replication slave on *.* to 'user'@'db2' identified by '123456';
Query OK, 0 rows affected (0.001 sec)

(5)配置從節點 db2 同步主節點 db1

在從節點 db2 虛擬機器上使用 mysql 命令登入 MariaDB 資料庫,配置從節點連線主節點的連線資訊。master_host 為主節點主機名 db1,master_user 為在步驟(4)中建立的使用者 user,命令如下。

MariaDB [(none)]> change master to  master_host='db1',master_user='user',master_password='123456';

配置完畢主從資料庫之間的連線資訊之後,開啟從節點服務。使用命令 show slave
status\G;並檢視從節點服務狀態,如果 Slave_IO_Running 和 Slave_SQL_Running 的狀態都為
YES,則從節點服務開啟成功。查詢結果如下。

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

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
   Master_Host: db1
   Master_User: user
   Master_Port: 3306
 Connect_Retry: 60
   Master_Log_File: mysql-bin.000002
   Read_Master_Log_Pos: 715
Relay_Log_File: db2-relay-bin.000004
 Relay_Log_Pos: 1014
 Relay_Master_Log_File: mysql-bin.000002
  Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
   Replicate_Do_DB: 
   Replicate_Ignore_DB: 
Replicate_Do_Table: 
Replicate_Ignore_Table: 
   Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
Last_Errno: 0
Last_Error: 
  Skip_Counter: 0
   Exec_Master_Log_Pos: 715
   Relay_Log_Space: 1620
   Until_Condition: None
Until_Log_File: 
 Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File: 
Master_SSL_CA_Path: 
   Master_SSL_Cert: 
 Master_SSL_Cipher: 
Master_SSL_Key: 
 Seconds_Behind_Master: 0
 Master_SSL_Verify_Server_Cert: No
 Last_IO_Errno: 0
 Last_IO_Error: 
Last_SQL_Errno: 0
Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
  Master_Server_Id: 16
Master_SSL_Crl: 
Master_SSL_Crlpath: 
Using_Gtid: No
   Gtid_IO_Pos: 
   Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
 Parallel_Mode: conservative
 SQL_Delay: 0
   SQL_Remaining_Delay: NULL
   Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
  Slave_DDL_Groups: 49
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 134
1 row in set (0.000 sec)

(6)驗證主從資料庫的同步功能

先在主節點 db1 的資料庫中建立庫 test,並在庫 test 中建立表 company,插入表資料。
建立完成後,查看錶 company 資料,如下所示。

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table company(id int not null primary key,name varchar(50),addr
varchar(255));
Query OK, 0 rows affected (0.165 sec)
MariaDB [test]> insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.062 sec)

MariaDB [test]> select * from company;
+----+------------+------+
| id | name   | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.000 sec)

這時從節點 db2 的資料庫就會同步主節點資料庫建立的 test 庫,可以在從節點查詢 test
資料庫與表 company,如果可以查詢到資訊,就能驗證主從資料庫叢集功能在正常執行。查
詢結果如下所示。

MariaDB [(none)]> show databases;
+--------------------+
| Database   |
+--------------------+
| gpmall |
| information_schema |
| mysql  |
| performance_schema |
| test   |
+--------------------+
5 rows in set (0.001 sec)

MariaDB [(none)]> select * from test.company;
+----+------------+------+
| id | name   | addr |
+----+------------+------+
|  1 | facebook   | usa  |
|  2 | bastetball | usa  |
+----+------------+------+
2 rows in set (0.001 sec)

3.部署 Mycat 讀寫分離中介軟體服務

(1)安裝 Mycat 服務
將 mycat 服務的二進位制軟體包 Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz
上傳到mycat虛擬機器的/root目錄下,並將軟體包解壓到/use/local目錄中。賦予解壓後的mycat目錄許可權。

[root@mycat ~]# tar -zxvf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# chown -R 777 /usr/local/mycat/

在/etc/profile 系統變數檔案中新增 mycat 服務的系統變數,並生效變數。

[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile

(2)編輯 mycat 的邏輯庫配置檔案

配置 mycat 服務讀寫分離的 schema.xml 配置檔案在/usr/local/mycat/conf/目錄下,可以在
檔案中定義一個邏輯庫,使使用者可以通過 mycat 服務管理該邏輯庫對應的 MariaDB資料庫。
在這裡定義一個邏輯庫 schema,name 為 USERDB;該邏輯庫 USERDB 對應資料庫 database
為 test(在部署主從資料庫時已安裝);設定資料庫寫入節點為主節點 db1;設定資料庫讀
取節點為從節點 db2。(可以直接刪除原來 schema.xml 的內容,替換為如下。注:IP 需要
修改成實際的 IP 地址)

[root@mycat ~]# cat /usr/local/mycat/conf/schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="USERDB" checkSQLschema="true" sqlMaxLimit="100"
dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="test" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql"
dbDriver="native" writeType="0" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="10.104.44.16:3306" user="root" password="123456">
<readHost host="hostS1" url="10.104.44.41:3306" user="root"  password="123456" />
</writeHost>
</dataHost>
</mycat:schema>

sqlMaxLimit:配置預設查詢數量

database:為真實資料庫名

balance="0",不開啟讀寫分離機制,所有讀操作都發送到當前可用的 writeHost 上。

balance="1",全部的 readHost 與 stand by writeHost 參與 select 語句的負載均衡,簡單的說,當雙主雙從模式(M1->S1,M2->S2,並且 M1 與 M2 互為主備),正常情況下,M2,S1,S2都參與 select 語句的負載均衡。

balance="2",所有讀操作都隨機的在 writeHost、readhost 上分發。

balance="3",所有讀請求隨機的分發到 wiriterHost 對應的 readhost 執行,writerHost 不負擔讀壓力,注意 balance=3 只在 1.4 及其以後版本有,1.3 沒有。

writeType="0",所有寫操作傳送到配置的第一個 writeHost,第一個掛了切到還生存的第二個 writeHost,重新啟動後已切換後的為準,切換記錄在配置檔案中:dnindex.properties . writeType="1",所有寫操作都隨機的傳送到配置的 writeHost。

(3)修改配置檔案許可權

修改 schema.xml 的使用者許可權,命令如下:

[root@mycat ~]# cd /usr/local/mycat/conf/
[root@mycat conf]# chown root:root schema.xml

(4)編輯 mycat 的訪問使用者

修改/usr/local/mycat/conf/目錄下的 server.xml 檔案,修改 root 使用者的訪問密碼與資料庫,
密碼設定為 123456,訪問 mycat 的邏輯庫為 USERDB,命令如下。

[root@mycat ~]# cat /usr/local/mycat/conf/server.xml

在配置檔案的最後部分,
<user name="root">
		<property name="password">123456</property>
		<property name="schemas">USERDB</property>

然後刪除如下幾行:

<user name="root">
	    <property name="password">user</property>
	    <property name="schemas">TESTDB</property>
	    <property name="readOnly">true</property>
</user>

儲存並退出 server.xml 配置檔案。

(5)啟動 mycat 服務

通過命令啟動 mycat 資料庫中介軟體服務,啟動後通過 netstat -ntpl 命令檢視虛擬機器埠
開放情況,如果有開放 8066 和 9066 埠,則表示 mycat 服務開啟成功。埠查詢情況如下。

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...

[root@mycat ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address State   PID/Program name
tcp0  0 0.0.0.0:63790.0.0.0:*   LISTEN  20170/redis-server  
tcp0  0 0.0.0.0:111 0.0.0.0:*   LISTEN  560/rpcbind 
tcp0  0 0.0.0.0:80  0.0.0.0:*   LISTEN  20908/nginx: master 
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  1537/sshd   
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1076/master 
tcp0  0 127.0.0.1:32000 0.0.0.0:*   LISTEN  19993/java  
tcp6   0  0 :::9066 :::*LISTEN  19993/java  
tcp6   0  0 :::6379 :::*LISTEN  20170/redis-server  
tcp6   0  0 :::111  :::*LISTEN  560/rpcbind 
tcp6   0  0 :::43792:::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:3888   :::*LISTEN  18112/java  
tcp6   0  0 :::35827:::*LISTEN  19993/java  
tcp6   0  0 :::21   :::*LISTEN  21098/vsftpd
tcp6   0  0 :::42933:::*LISTEN  18112/java  
tcp6   0  0 :::22   :::*LISTEN  1537/sshd   
tcp6   0  0 ::1:25  :::*LISTEN  1076/master 
tcp6   0  0 :::37438:::*LISTEN  18664/java  
tcp6   0  0 :::1984 :::*LISTEN  19993/java  
tcp6   0  0 :::8066 :::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:9092   :::*LISTEN  18664/java  
tcp6   0  0 :::2181 :::*LISTEN  18112/java   

4.驗證資料庫叢集服務讀寫分離功能

(1)用 mycat 服務查詢資料庫資訊

先在 mycat 虛擬機器上使用 yum 安裝 mariadb-client 服務。

[root@mycat ~]#  yum install -y MariaDB-client

在 mycat 虛擬機器上使用 mysql 命令檢視 mycat 服務的邏輯庫 USERDB,因為 mycat 的邏
輯庫 USERDB 對應資料庫 test(在部署主從資料庫時已安裝),所以可以檢視庫中已經創
建的表 company。命令如下。

[root@mycat ~]# mysql -h127.0.0.1 -P8066 -uroot -p123456

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server
(OpenCloundDB)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| USERDB |
+----------+
1 row in set (0.001 sec)
MySQL [(none)]> use USERDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [USERDB]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company |
+----------------+
1 row in set (0.003 sec)
MySQL [USERDB]> select * from company;
+----+----------+------+
| id | name | addr |
+----+----------+------+
| 1 | facebook | usa |
+----+----------+------+
1 row in set (0.005 sec)

(2)用 mycat 服務新增表資料

在 mycat 虛擬機器上使用 mysql 命令對錶 company 新增一條資料(2,"basketball","usa"),添
加完畢後查看錶資訊。命令如下。

MySQL [USERDB]> insert into company values(2,"bastetball","usa");
Query OK, 1 row affected (0.050 sec)

MySQL [USERDB]> select * from company;
+----+------------+------+
| id | name | addr |
+----+------------+------+
| 1 | facebook | usa |
| 2 | bastetball | usa |
+----+------------+------+
2 rows in set (0.002 sec)

(3)驗證 mycat 服務對資料庫讀寫操作分離

在mycat虛擬機器節點使用mysql命令通過9066埠查詢對資料庫讀寫操作的分離資訊。
可以看到所有的寫入操作 WRITE_LOAD 數都在 db1 主資料庫節點上,所有的讀取操作
READ_LOAD 數都在 db2 主資料庫節點上。由此可見資料庫讀寫操作已經分離到 db1 和 db2
節點上了。命令如下。

[root@mycat ~]# mysql -h127.0.0.1 -P9066 -uroot -p123456 -e 'show @@datasource;'
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| DATANODE | NAME   | TYPE  | HOST | PORT | W/R  | ACTIVE | IDLE | SIZE | EXECUTE | READ_LOAD | WRITE_LOAD |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------+------------+
| dn1  | hostM1 | mysql | 10.104.44.16 | 3306 | W|  0 |8 | 1000 |1167 | 0 |  0 |
| dn1  | hostS1 | mysql | 10.104.44.41 | 3306 | R|  0 |8 | 1000 |1237 |79 |  0 |
+----------+--------+-------+--------------+------+------+--------+------+------+---------+-----------

至此,mycat 讀寫分離資料庫案例完成。

2 實戰案例 ——ZooKeeper 叢集部署

2.1 案例目標

(1)瞭解 ZooKeeper 分散式應用程式協調服務

(2)使用三臺機器搭建 ZooKeeper 叢集

(3)使用 ZooKeeper 叢集

2.2 案例分析

1.規劃節點

ZooKeeper 集群系統的節點規劃,見表

2.基礎準備

使用上面案例建立的三臺雲主機。使用提供的 zookeeper-3.4.14.tar.gz 包和 gpmall-repo資料夾,安裝 zookeeper 服務。

2.3 案例實施

  1. 搭建 ZooKeeper 叢集

    (1)安裝 JDK 環境

3個節點安裝 Java JDK 環境,3 個節點均執行命令如下:

[root@mycat ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@db1 ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@db2 ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel

[root@mycat ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

[root@db1 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

[root@db2 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)

(2)解壓 ZooKeeper 軟體包

將 zookeeper-3.4.14.tar.gz 軟體包上傳至 3 個節點的/root 目錄下,進行解壓操作,3 個節
點均執行命令如下:

[root@mycat ~]# tar -zxvf zookeeper-3.4.14.tar.gz
[root@db1 ~]# tar -zxvf zookeeper-3.4.14.tar.gz
[root@db2 ~]# tar -zxvf zookeeper-3.4.14.tar.gz

(3)修改3個節點配置檔案

在 zookeeper1 節點,進入 zookeeper-3.4.14/conf 目錄下,修改 zoo_sample.cfg 檔案為zoo.cfg,並編輯該檔案內容如下:

[root@mycat ~]# cat zookeeper-3.4.14/conf/zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
server.1=10.104.44.42:2888:3888
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
server.2=10.104.44.16:2888:3888
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
server.3=10.104.44.41:2888:3888
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

[root@mycat ~]# grep -n '^'[a-Z] zoo.cfg
2:tickTime=2000
5:initLimit=10
8:syncLimit=5
12:dataDir=/tmp/zookeeper
14:clientPort=2181
18:server.1=10.104.44.42:2888:3888
21:server.2=10.104.44.16:2888:3888
23:server.3=10.104.44.41:2888:3888

命令解析:

  • initLimit:ZooKeeper 叢集模式下包含多個 zk 程序,其中一個程序為 leader,餘下的
    程序為 follower。當 follower 最初與 leader 建立連線時,它們之間會傳輸相當多的資料,尤
    其是 follower 的資料落後 leader 很多。initLimit 配置 follower 與 leader 之間建立連線後進行
    同步的最長時間。

  • syncLimit:配置 follower 和 leader 之間傳送訊息,請求和應答的最大時間長度。

  • tickTime:tickTime 則是上述兩個超時配置的基本單位,例如對於 initLimit,其配置
    值為 5,說明其超時時間為 2000ms * 5 = 10 秒。

  • server.id=host:port1:port2:其中 id 為一個數字,表示 zk 程序的 id,這個 id 也是 dataDir
    目錄下 myid 檔案的內容。host 是該 zk 程序所在的 IP 地址,port1 表示 follower 和 leader 交
    換訊息所使用的埠,port2 表示選舉 leader 所使用的埠。

  • dataDir:其配置的含義跟單機模式下的含義類似,不同的是叢集模式下還有一個 myid
    檔案。myid 檔案的內容只有一行,且內容只能為 1 - 255 之間的數字,這個數字亦即上面介
    紹 server.id 中的 id,表示 zk 程序的 id

注意:zookeeper2 和 和 zookeeper3 節點的操作與修改的配置和 zookeeper1 節點一樣。

(4)建立 myid 檔案

在 3 臺機器 dataDir 目錄(此處為/tmp/zookeeper)下,分別建立一個 myid 檔案,檔案
內容分別只有一行,其內容為 1,2,3。即檔案中只有一個數字,這個數字即為上面 zoo.cfg
配置檔案中指定的值。ZooKeeper是根據該檔案來決定ZooKeeper叢集各個機器的身份分配。

建立 myid 檔案,命令如下:

zookeeper1 節點:

# mkdir /tmp/zookeeper
# vi /tmp/zookeeper/myid
# cat /tmp/zookeeper/myid
1

zookeeper2 節點:

# mkdir /tmp/zookeeper
# vi /tmp/zookeeper/myid
# cat /tmp/zookeeper/myid
2

zookeeper3 節點:

# mkdir /tmp/zookeeper
# vi /tmp/zookeeper/myid
# cat /tmp/zookeeper/myid
3

(5)啟動 ZooKeeper 服務

在 3 臺機器的 zookeeper/bin 目錄下執行命令如下:

zookeeper1 節點:

# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower

zookeeper2 節點:

# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... already running as process 10175.

# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader

zookeeper3 節點:

# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower

可以看到,3 個節點,zookeeper2 為 leader,其他的都是 follower。

至此,ZooKeeper 叢集配置完畢。

3 實戰案例 ——Kafka 叢集部署

3.1 案例目標

(1)瞭解 Kafka 分散式釋出訂閱訊息系統。

(2)搭建 Kafka 分散式釋出訂閱訊息系統。

(3)使用 Kafka 分散式釋出訂閱訊息系統。

3.2 案例分析

  1. 規劃節點

    Kafka 叢集的節點規劃,見下表

2.基礎準備

使用ZooKeeper叢集搭建的3個節點構建Kafka叢集,因為Kafka服務依賴於ZooKeeper
服務,所以不再多建立雲主機來進行試驗。軟體包使用提供的 kafka_2.11-1.1.1.tgz 軟體包。

3.3 案例實施

搭建 Kafka 叢集

(1)解壓 Kafka 軟體包

將提供的kafka_2.11-1.1.1.tgz軟體包,上傳至3個節點的/root目錄下,並執行解壓操作,
3 個節點執行的解壓命令如下:

tar -zxvf kafka_2.11-1.1.1.tgz

(2)修改 3 個節點配置檔案

在 zookeeper1 節點,進入 kafka_2.11-1.1.1/config 目錄下,編輯 server.properties 檔案。
在配置檔案中找到以下兩行並註釋掉(在文字前加#)如下所示:

#broker.id=0
#zookeeper.connect=localhost:2181

然後在配置檔案的底部新增如下 3 個配置。

zookeeper1 節點:

broker.id=1
zookeeper.connect=10.104.44.42:2181,10.104.44.16:2181,10.104.44.41:2181
listeners = PLAINTEXT://10.104.44.42:9092

命令解析:

  • broker.id:每臺機器不能一樣。

  • zookeeper.connect:因為有 3 臺 ZooKeeper 伺服器,所以在這裡 zookeeper.connect 設
    置為 3 臺。

  • listeners:在配置叢集的時候,必須設定,不然以後的操作會報找不到 leader 的錯誤。

另外兩臺伺服器,zookeeper.connect 的配置跟這裡的一樣,但是 broker.id 和 listeners 不
能一樣。

zookeeper2 節點:

broker.id=2
zookeeper.connect=10.104.44.42:2181,10.104.44.16:2181,10.104.44.41:2181
listeners = PLAINTEXT://172.16.51.18:9092

zookeeper3 節點:

broker.id=3
zookeeper.connect=10.104.44.42:2181,10.104.44.16:2181,10.104.44.41:2181
listeners = PLAINTEXT://172.16.51.30:9092

(3)啟動服務

在 zookeeper1 節點,進入 kafka_2.11-1.1.1/bin 目錄下,啟動服務命令如下。

zookeeper1 節點:

# ./kafka-server-start.sh -daemon ../config/server.properties
# jps
11416 Kafka
11464 Jps
10479 QuorumPeerMain

zookeeper2 節點:

# ./kafka-server-start.sh -daemon ../config/server.properties
# jps
11121 Kafka
11188 Jps
10175 QuorumPeerMain

zookeeper3 節點:

# ./kafka-server-start.sh -daemon ../config/server.properties
# jps
11080 Kafka
10188 QuorumPeerMain
11149 Jps

(4)測試服務

在 zookeeper1 節點,進入 kafka_2.11-1.1.1/bin 目錄下,建立 topic 命令如下。

zookeeper1 節點:

# ./kafka-topics.sh --create --zookeeper 10.104.44.42:2181 --replication-factor 1 --partitions 1 --topic test

Created topic "test".

如果成功的話,會輸出“Created topic "test".”。

檢視 topic,雖然 topic 是在 10.104.44.42 上建立的,但是在其他機器上也能看到。例如在
任意啟動的機器 kafka_2.11-1.1.1/bin 的目錄中執行命令如下:

zookeeper2 節點:

[root@zookeeper2 bin]# ./kafka-topics.sh --list --zookeeper 172.16.51.18:2181
test

zookeeper3 節點:

[root@zookeeper3 bin]# ./kafka-topics.sh --list --zookeeper 172.16.51.30:2181
test

測試成功。

4 實戰案例 —— 構建叢集應用系統環境

4.1 案例目標

(1)瞭解叢集部署應用系統的架構

(2)構建 mycat 讀寫分離資料庫叢集

(3)構建 Zookeeper 叢集

(4)構建 Kafka 叢集

(5)安裝 redis 服務

4.2 案例分析

1.規劃節點

使用 Mycat 讀寫分離資料庫作為應用系統的資料庫後端,使用 Zookeeper 叢集,Kafka
叢集提供應用系統排程服務,使用 Reids 提供應用系統訊息佇列服務,節點規劃見下表

2.基礎準備

叢集應用系統的部署使用上面案例建立的雲主機。

4.3 案例實施

  1. 修改資料庫配置

(1)新建 gpmall 資料庫

將提供的 gpmall.sql 資料庫檔案上傳至資料庫 db1 節點的/root 目錄下。登入 db1 節點,
使用資料庫使用者名稱:root;密碼:123456 登入資料庫,建立庫 gpmall,將提供的 gpmall.sql
檔案匯入到 gpmall 庫中,命令如下。

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.3.18-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database gpmall;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> use gpmall
Database changed

MariaDB [gpmall]> source /root/gpmall.sql
Query OK, 0 rows affected (0.000 sec)
Query OK, 0 rows affected (0.000 sec)

gpmall.sql 檔案匯入完畢後,退出資料庫。

(2)修改 Mycat 配置

登入 mycat 節點,修改 schema.xml 配置檔案,檔案位置在/usr/local/mycat/conf 目錄下,
修改 schema.xml 檔案內容如下,將 schema name 修改為 gpmall;database 修改為 gpmall。

[root@mycat conf]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="gpmall" checkSQLschema="false" sqlMaxLimit="100"
dataNode="dn1"></schema>
<dataNode name="dn1" dataHost="localhost1" database="gpmall" />
<dataHost name="localhost1" maxCon="1000" minCon="10" balance="3" dbType="mysql"
dbDriver="native" writeType="0" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM1" url="172.16.51.22:3306" user="root" password="123456">
<readHost host="hostS1" url="172.16.51.26:3306" user="root"
password="123456" />
</writeHost>
</dataHost>
</mycat:schema>

登入 mycat 節點,修改 server.xml 配置檔案,檔案位置在/usr/local/mycat/conf 目錄下。
在 server.xml 檔案的最後部分,將USERDB改成
gpmall,具體配置檔案如下:

<user name="root">
		<property name="password">123456</property>
		<property name="schemas">gpmall</property>

		<!-- 表級 DML 許可權設定 -->
		<!--
		<privileges check="false">
		<schema name="TESTDB" dml="0110" >
		 <table name="tb01" dml="0000"></table>
		 <table name="tb02" dml="1111"></table>
		</schema>
	</privileges>
-->
</user>

(3)重啟 Mycat 服務

登入 mycat 節點,進入/usr/local/mycat/bin 目錄下,重啟 mycat 服務,命令如下:

[root@mycat bin]# ./mycat restart
Stopping Mycat-server...
Mycat-server was not running.
Starting Mycat-server...

檢視 Mycat 服務是否啟動,命令如下:(看到 8066 埠說明 Mycat 服務啟動)

[root@mycat ~]# netstat -ntlp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address State   PID/Program name
tcp0  0 0.0.0.0:63790.0.0.0:*   LISTEN  20170/redis-server  
tcp0  0 0.0.0.0:111 0.0.0.0:*   LISTEN  560/rpcbind 
tcp0  0 0.0.0.0:80  0.0.0.0:*   LISTEN  20908/nginx: master 
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  1537/sshd   
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1076/master 
tcp0  0 127.0.0.1:32000 0.0.0.0:*   LISTEN  19993/java  
tcp6   0  0 :::9066 :::*LISTEN  19993/java  
tcp6   0  0 :::6379 :::*LISTEN  20170/redis-server  
tcp6   0  0 :::111  :::*LISTEN  560/rpcbind 
tcp6   0  0 :::43792:::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:3888   :::*LISTEN  18112/java  
tcp6   0  0 :::35827:::*LISTEN  19993/java  
tcp6   0  0 :::21   :::*LISTEN  21098/vsftpd
tcp6   0  0 :::42933:::*LISTEN  18112/java  
tcp6   0  0 :::22   :::*LISTEN  1537/sshd   
tcp6   0  0 ::1:25  :::*LISTEN  1076/master 
tcp6   0  0 :::37438:::*LISTEN  18664/java  
tcp6   0  0 :::1984 :::*LISTEN  19993/java  
tcp6   0  0 :::8066 :::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:9092   :::*LISTEN  18664/java  
tcp6   0  0 :::2181 :::*LISTEN  18112/java  
  1. 安裝 Redis 服務

(1)安裝 Redis 服務
在 mycat 節點使用 yum 安裝 Redis 服務,命令如下:

[root@redis ~]# yum install redis –y

(2)啟動 Redis 服務

修改 Redis 配置檔案,編輯/etc/redis.conf 檔案。

將 bind 127.0.0.1 這一行註釋掉;將 protected-mode yes 改為 protected-mode no。

啟動 Redis 服務命令如下。

[root@mall ~]# systemctl start redis
[root@mall ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to
/usr/lib/systemd/system/redis.service.

檢查 Redis 服務啟動,命令如下:(檢查到 6379 埠即證明 Redis 服務執行)

[root@mycat ~]# netstat -ntlp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address State   PID/Program name
tcp0  0 0.0.0.0:63790.0.0.0:*   LISTEN  20170/redis-server  
tcp0  0 0.0.0.0:111 0.0.0.0:*   LISTEN  560/rpcbind 
tcp0  0 0.0.0.0:80  0.0.0.0:*   LISTEN  20908/nginx: master 
tcp0  0 0.0.0.0:22  0.0.0.0:*   LISTEN  1537/sshd   
tcp0  0 127.0.0.1:250.0.0.0:*   LISTEN  1076/master 
tcp0  0 127.0.0.1:32000 0.0.0.0:*   LISTEN  19993/java  
tcp6   0  0 :::9066 :::*LISTEN  19993/java  
tcp6   0  0 :::6379 :::*LISTEN  20170/redis-server  
tcp6   0  0 :::111  :::*LISTEN  560/rpcbind 
tcp6   0  0 :::43792:::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:3888   :::*LISTEN  18112/java  
tcp6   0  0 :::35827:::*LISTEN  19993/java  
tcp6   0  0 :::21   :::*LISTEN  21098/vsftpd
tcp6   0  0 :::42933:::*LISTEN  18112/java  
tcp6   0  0 :::22   :::*LISTEN  1537/sshd   
tcp6   0  0 ::1:25  :::*LISTEN  1076/master 
tcp6   0  0 :::37438:::*LISTEN  18664/java  
tcp6   0  0 :::1984 :::*LISTEN  19993/java  
tcp6   0  0 :::8066 :::*LISTEN  19993/java  
tcp6   0  0 10.104.44.42:9092   :::*LISTEN  18664/java  
tcp6   0  0 :::2181 :::*LISTEN  18112/java  

至此,應用商城叢集部署需要的環境 Mycat、Redis、Zookeeper、Kafka 服務均已部署完畢。

5 實戰案例 —— 部署叢集應用系統

5.1 案例目標

(1)瞭解 nginx 負載均衡架構

(2)構建 nginx 負載均衡

(3)構建叢集應用系統

5.2 案例分析

  1. 規劃節點

使用 Mycat 讀寫分離資料庫作為應用系統的資料庫後端,使用 Zookeeper 叢集,Kafka叢集提供應用系統排程服務,使用 Reids 提供應用系統訊息佇列服務,節點規劃見下表

  1. 基礎準備

使用上面案例的三臺雲主機進行部署。使用提供gpmall-shopping-0.0.1-SNAPSHOT.jar、gpmall-user-0.0.1-SNAPSHOT.jar、shopping-provider-0.0.1-SNAPSHOT.jar、user-provider-0.0.1-SNAPSHOT.jar、dist 這 5 個包部署應用系統,其中 4 個 jar 包為後端服務包,在 db1 節點和 db2 節點上均執行;dist 為前端包,在 mycat 節點上部署。

5.3 案例實施

  1. 基礎環境配置

(1)配置 hosts 檔案

在 db1 和 db2 節點,編輯/etc/hosts 配置檔案,檔案內容如下:

db1 節點:

[root@db1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.104.44.42 mycat
10.104.44.16 db1
10.104.44.41 db2
10.104.44.42 redis.mall
10.104.44.42 mysql.mall
10.104.44.42 kafka1.mall
10.104.44.16 kafka2.mall
10.104.44.41 kafka3.mall
10.104.44.42 zk1.mall
10.104.44.16 zk2.mall
10.104.44.41 zk3.mall

db2 節點:

[root@db2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.104.44.42 mycat
10.104.44.16 db1
10.104.44.41 db2
10.104.44.42 redis.mall
10.104.44.42 mysql.mall
10.104.44.42 kafka1.mall
10.104.44.16 kafka2.mall
10.104.44.41 kafka3.mall
10.104.44.42 zk1.mall
10.104.44.16 zk2.mall
10.104.44.41 zk3.mall

(2)執行jar包

將提供的四個jar包,上傳至 db1、db2 節點的/root 目錄下,然後執行這四個jar包,命令如下:

db1 節點:

# nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
[1] 11025
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
[2] 11039
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
[3] 11085
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
[4] 11105
# nohup: ignoring input and appending output to ‘nohup.out’

檢查 jar 包是否執行,命令如下:

# ps -ef |grep java
root 17333  0.5  8.4 4789532 676440 pts/0  Sl   Apr13   1:33 java -jar user-provider-0.0.1-SNAPSHOT.jar
root 17383  0.3  6.4 4840552 517104 pts/0  Sl   Apr13   0:54 java -jar shopping-provider-0.0.1-SNAPSHOT.jar
root 17432  0.3  7.3 4702388 585720 pts/0  Sl   Apr13   1:06 java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar
root 17469  0.3  5.7 4671768 460392 pts/0  Sl   Apr13   0:56 java -jar gpmall-user-0.0.1-SNAPSHOT.jar
root 18093  0.0  0.0 112812   964 pts/0S+   00:05   0:00 grep --color=auto java

db2 節點:

 # nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
[1] 10915
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
[2] 10929
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
[3] 11004
# nohup: ignoring input and appending output to ‘nohup.out’

# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
[4] 11024
# nohup: ignoring input and appending output to ‘nohup.out’

檢查 jar 包是否執行,命令如下:

# ps -ef |grep java
root 17333  0.5  8.4 4789532 676440 pts/0  Sl   Apr13   1:33 java -jar user-provider-0.0.1-SNAPSHOT.jar
root 17383  0.3  6.4 4840552 517104 pts/0  Sl   Apr13   0:54 java -jar shopping-provider-0.0.1-SNAPSHOT.jar
root 17432  0.3  7.3 4702388 585720 pts/0  Sl   Apr13   1:06 java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar
root 17469  0.3  5.7 4671768 460392 pts/0  Sl   Apr13   0:56 java -jar gpmall-user-0.0.1-SNAPSHOT.jar
root 18093  0.0  0.0 112812   964 pts/0S+   00:05   0:00 grep --color=auto java

至此,應用系統部署完畢。

3.前端配置

(1)安裝 nginx 服務

登入 mycat 節點,安裝 nginx 服務,命令如下:

# yum install nginx -y

(2)上傳前端程式碼

將提供的 dist 資料夾上傳至 nginx 節點的/root 目錄下,然後將 dist 中的內容複製到 nginx的專案目錄,命令如下:

首先將 nginx 預設專案目錄的檔案刪除:

# rm -rf /usr/share/nginx/html/*

然後將 dist 中的檔案複製至/usr/share/nginx/html 目錄下:

# cp -rvf dist/* /usr/share/nginx/html/

(3)修改 nginx 配置檔案

修改 nginx 配置檔案/etc/nginx/conf.d/default.conf 如下,新增以下標註,IP 地址為運
行 jar 包的兩個伺服器的 IP。

upstream myuser {                    #新增這行
    server 172.16.51.18:8082;		 #新增這行
    server 172.16.51.30:8082;        #新增這行
    ip_hash;                         #新增這行
}                                    #新增這行
upstream myshopping {                #新增這行
    server 172.16.51.18:8081;        #新增這行
    server 172.16.51.30:8081;        #新增這行
    ip_hash;                         #新增這行
}									 #新增這行
upstream mycashier {				 #新增這行
    server 172.16.51.18:8083;		 #新增這行
    server 172.16.51.30:8083;		 #新增這行
    ip_hash;						 #新增這行
}									 #新增這行

server {
    listen 80;
    server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
}
location /user {                        #新增這行
    proxy_pass http://myuser;           #新增這行
    }                                   #新增這行
location /shopping {                    #新增這行
    proxy_pass http://myshopping;       #新增這行
    }                                   #新增這行
location /cashier {                     #新增這行
    proxy_pass http://mycashier;        #新增這行
    }                                   #新增這行

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

(4)啟動 nginx 服務

在 mycat 節點,啟動 nginx 服務,命令如下:

# systemctl start nginx

檢視 nginx 是否啟動,命令如下(檢視 80 埠是否啟動):

# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
10287/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
946/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
864/master
tcp6 0 0 :::22 :::* LISTEN
946/sshd
tcp6 0 0 ::1:25 :::* LISTEN
864/master

4.網站訪問

開啟瀏覽器,在位址列中輸入 http://172.16.51.6,訪問介面,如圖

單擊右上角“頭像”,進行登入操作,使用使用者名稱/密碼為 test/test 進行登入,如圖

登入後,可進行購買商品操作,單擊首頁“地平線 8 號”旅行箱圖片,如圖

進入購買介面,單擊“現在購買”按鈕,進行購買商品操作,如圖

單擊完“現在購買”按鈕,跳轉到提交訂單介面,如圖

至此,叢集部署應用系統完成。