欢迎来到CloudService文涵的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

商城集群化部署

商城集群化部署

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 号”旅行箱图片,如图

进入购买界面,单击“现在购买”按钮,进行购买商品操作,如图

单击完“现在购买”按钮,跳转到提交订单界面,如图

至此,集群部署应用系统完成。

posted on 2022-04-14 11:18  Cloudservice  阅读(370)  评论(4编辑  收藏  举报