centos7.2部署集群应用系统商城

在我之前的文章中,我为各位详细介绍了单节点的应用系统商城以及数据库、zookeeper、Kafka等服务集群的部署搭建。那么在这篇文章中我将画上一个句号。我会将之前这些文章提到的配置内容进行再次运用,来完成这次的集群应用系统商城实验。

关于本次实验所使用到的软件资源包均已上传百度网盘,需要自取。

链接:https://pan.baidu.com/s/1a-7_iAIX0DBAMkF9bhiTcA?pwd=2333 
提取码:2333 
复制这段内容后打开百度网盘手机App,操作更方便哦。

集群搭建应用系统商城的本质就是每个主机只做一些服务实现分布式管理,而不是一味的将所有服务压在一台主机上。这既需要主机具有很高的性能要求,也让其散失了高可用性。

首先来看一下ip地址规划:

网卡模式同样不做要求。

IP

主机名

节点

172.16.51.6

mycat

Mycat中间件服务节点

172.16.51.22

db1

MariaDB数据库集群主节点

172.16.51.26

db2

MariaDB数据库集群从节点

172.16.51.23

zookeeper1

集群节点

172.16.51.32

zookeeper2

集群节点

172.16.51.41

zookeeper3

集群节点

172.16.51.40

redis

消息队列服务节点

172.16.51.11

jar1

Tomcat1节点

172.16.51.56

jar2

Tomcat2节点

172.16.51.45

nginx

Nginx服务器

不要看到主机集群的数量多而混乱,我们可以一个模块一个模块的来搭建并将其全部组合起来。

这么多的主机会让vm虚拟机的克隆功能显得异常重要,我们需要懂得适时使用克隆来提升我们的实验效率。

先开启一台安装好centos7.2系统的主机,我们先配置网卡ip地址,关闭防火墙、写好主机映射文件,再把这次实验的所有本地软件资源都上传并配置好yum源。然后完整克隆出需要的这几台主机,并开启它们修改好对应的ip地址和主机名,完成最基本的实验开始前环境。

我们把第一台主机设为mycat节点,因此我们配置ip地址和主机名。

[root@localhost ~]# hostnamectl set-hostname mycat
[root@localhost ~]# bash
[root@mycat ~]# vi /etc/sysconfig/network-scripts/ifcfg-eno16777736

记得重启网络服务。

随后关闭防火墙。

上传所有本地资源做镜像挂载,我们这边直接把整个实验需要的都传上来方便我们后续克隆完后其他机子能直接各取所需。

普通软件包没啥好说的。

各个集群服务软件包。

然后重点我们这次的后端jar包,上传的是这个簇文件夹目录中的。不要上传成单节点single的了,不一样用不了。

都上传完毕以后移走本地网络源然后编辑本地镜像源。

[root@mycat ~]# mv /etc/yum.repos.d/* /home/
[root@mycat ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[mall]
name=mall
baseurl=file:///root/gpmall-repo
gpgcheck=0
enabled=1

创建仓库目录先不用挂载,反正还要关机克隆。。。

[root@mycat ~]# mkdir /opt/centos

最后写好我们的主机映射文件即可开始克隆。

[root@mycat ~]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.51.40  redis.mall
172.16.51.6  mysql.mall mycat
172.16.51.23  kafka1.mall
172.16.51.32  kafka1.mall
172.16.51.41  kafka1.mall
172.16.51.23  zk1.mall zk1
172.16.51.32  zk1.mall zk2
172.16.51.41  zk1.mall zk3
172.16.51.22  db1
172.16.51.26  db2
172.16.51.11  jar1
172.16.51.56  jar2
172.16.51.45  nginx

关机,逐个选择完整克隆出我们所需的机子。最好先保存个快照。

玛雅,终于克隆完了。幸好我的很大,能把这么多机子全部兜住。各位要根据自己电脑的配置合理分配内存和硬盘噜🦌。QAQ

然后看自己电脑配置逐步一台一台开启修改它们各自的ip地址和主机名,我的撑得住我就一次性全开了慢慢改。改完以后最好再全部关机一下保存个初始纯净快照,这样后面哪台机子做错做崩了还能还原救一下,不然又要去克隆mycat的那个快照重新搞了。

自己参照上面的网络规划表修改地址和主机名,一定要记得修改完地址和主机名以后记得重启网络服务和bash刷新一下系统信息。

只能耐心点慢慢在虚拟机上改。这边就贴一个改db1的过程,剩下就不贴了。自己认真改好,全改好以后不要忘记把mycat也刷新一下网络服务和系统信息以防止地址冲突和主机名重复。

全部配置完以后,我们就进入了正式的实验环境了,接着其他就是把前面文章讲到的几个集群重新搭建一遍。我们首先来实现主从读写分离数据库集群。

先将所有虚拟机都挂载一下本地源,然后在mycat节点上安装java环境。

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

然后去db1和2节点安装数据库服务,并且都将其启动后设置为自启动。

yum install -y mariadb mariadb-server

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

都进行数据库初始化,这边数据库密码一定要是123456不能改,因为后端jar包中mysql驱动配置的密码就为123456。

这边只贴db1的图,db2也是一样的流程。

[root@db1 ~]#  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] 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
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!
[root@db1 ~]#

配置db1为数据库主节点。

[root@db1 ~]# vi /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 all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步MySQL系统数据库
server_id = 22                            #数据库集群中的每个节点id都要不同,一般使用IP地址
的最后段的数字,例如172.16.51.18,server_id就写18

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

然后重启服务。

开放db1主节点的数据库权限。在主节点db1虚拟机上使用mysql命令登录MariaDB数据库,授权在任何客户端机器上可以以root用户登录到数据库。在主节点db1数据库上创建一个user用户让从节点db2连接,并赋予从节点同步主节点数据库的权限

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> grant all privileges  on *.* to root@'%' identified by "123456";
Query OK, 0 rows affected (0.000 sec)

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

MariaDB [(none)]>

让db2从节点同步db1主节点。在从节点db2虚拟机上使用mysql命令登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名db1,master_user为在db1中创建的用户user。

配置完毕主从数据库之间的连接信息之后,开启从节点服务。使用show slave status\G; 命令并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。

[root@db2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 21
Server version: 10.3.18-MariaDB 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)]> change master to master_host='db1',master_user='user',master_password='12  3456';
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 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.000001
           Read_Master_Log_Pos: 702
                Relay_Log_File: db2-relay-bin.000002
                 Relay_Log_Pos: 1001
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes

回到主节点db1上验证一下同步功能。先在主节点db1的数据库中创建库test,并在库test中创建表company,插入表数据。创建完成后,查看表company数据。

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
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 test;
Query OK, 1 row affected (0.000 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.034 sec)

MariaDB [test]>  insert into company values(1,"facebook","usa");
Query OK, 1 row affected (0.001 sec)

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

MariaDB [test]>

这时从节点db2的数据库就会同步主节点数据库创建的test库,可以在从节点查询test数据库与表company,如果可以查询到信息,就能验证主从数据库集群功能在正常运行。

[root@db2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.3.18-MariaDB 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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.000 sec)

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

MariaDB [(none)]>

我们要到db1上导入这次搭建应用商城的数据库表。

回到db1上构建数据库。

[root@db1 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
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

接着我们回到mycat节点上部署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/
[root@mycat ~]# echo export MYCAT_HOME=/usr/local/mycat/ >> /etc/profile
[root@mycat ~]# source /etc/profile

然后编写mycat的逻辑库文件来实现读写分离。直接把里面的内容用dG键删光重写。

[root@mycat ~]# vi /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="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>

再去修改服务配置文件,要到文件末尾进行修改。在server.xml文件的最后部分,将<property name="schemas">USERDB</property>改成<property name="schemas">gpmall</property>,然后删除或者注释掉下面多余的参数配置。

就如我这边贴出的代码所示,多余的部分已经删除,数据库名也已经修改为gpmall。

[root@mycat ~]# vi /usr/local/mycat/conf/server.xml
        <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>


</mycat:server>

再修改一下配置文件权限。

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

再次回到mycat节点上启动服务,安装网络工具包,使用命令查看端口验证服务是否启动。如果能看到8066端口,则说明Mycat服务启动成功。有的时候有延迟没出来的话可以把启动服务的start替换成restart重启服务再查看端口试试。

[root@mycat ~]# /bin/bash /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]# yum install -y net-tools
[root@mycat ~]# 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:22              0.0.0.0:*               LISTEN      1129/sshd      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1611/master    
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      3826/java      
tcp6       0      0 :::9066                 :::*                    LISTEN      3826/java      
tcp6       0      0 :::36682                :::*                    LISTEN      3826/java      
tcp6       0      0 :::22                   :::*                    LISTEN      1129/sshd      
tcp6       0      0 ::1:25                  :::*                    LISTEN      1611/master    
tcp6       0      0 :::48605                :::*                    LISTEN      3826/java      
tcp6       0      0 :::1984                 :::*                    LISTEN      3826/java      
tcp6       0      0 :::8066                 :::*                    LISTEN      3826/java      
[root@mycat ~]#

到这里我们数据库的机子已经配置完了,接着我们去配置redis服务,我们到redis节点上去。

安装服务先。

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

和单节点类似的修改配置文件。将bind 127.0.0.1这一行注释掉,将protected-mode yes改为protected-mode no。

[root@redis ~]# vi /etc/redis.conf

然后启动redis服务并设置为自启动。如果检查到6379端口,即证明Redis服务运行成功。

[root@redis ~]# systemctl start redis
[root@redis ~]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.
[root@redis ~]# yum  install -y net-tools
[root@redis ~]# 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:6379            0.0.0.0:*               LISTEN      2435/redis-server *
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1129/sshd      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1591/master    
tcp6       0      0 :::6379                 :::*                    LISTEN      2435/redis-server *
tcp6       0      0 :::22                   :::*                    LISTEN      1129/sshd      
tcp6       0      0 ::1:25                  :::*                    LISTEN      1591/master    
[root@redis ~]#

接下来我们配置zookeeper集群和Kafka集群服务。

我们这边主机名直接设置为zk1,2,3。

步骤和前面文章搭建集群的方法一模一样,先在3个节点上安装java环境。我这边只贴一个节点的图。

[root@zk1 ~]# yum install -y java java-devel

同样三个节点都解压服务文件。

[root@zk1 ~]# tar -zxvf zookeeper-3.4.14.tar.gz

接着修改节点配置文件,然后scp传递给另外两个节点。一般来说其他都是默认对的上的,就直接文件末尾添加3个节点参数条目即可。

[root@zk1 ~]# cd zookeeper-3.4.14/conf/
[root@zk1 conf]# mv zoo_sample.cfg zoo.cfg
[root@zk1 conf]# vi zoo.cfg
2:tickTime=2000
5:initLimit=10
8:syncLimit=5
12:dataDir=/tmp/zookeeper
14:clientPort=2181
29:server.1=172.16.51.23:2888:3888
30:server.2=172.16.51.32:2888:3888
31:server.3=172.16.51.41:2888:3888
[root@zk1 conf]# scp zoo.cfg root@172.16.51.32:/root/zookeeper-3.4.14/conf/zoo.cfg
The authenticity of host '172.16.51.32 (172.16.51.32)' can't be established.
ECDSA key fingerprint is a2:06:6f:65:a3:1f:9c:53:91:6d:0a:81:d0:6f:da:6a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.51.32' (ECDSA) to the list of known hosts.
root@172.16.51.32's password:
zoo.cfg                                                      100% 1019     1.0KB/s   00:00
[root@zk1 conf]# scp zoo.cfg root@172.16.51.41:/root/zookeeper-3.4.14/conf/zoo.cfg
The authenticity of host '172.16.51.41 (172.16.51.41)' can't be established.
ECDSA key fingerprint is a2:06:6f:65:a3:1f:9c:53:91:6d:0a:81:d0:6f:da:6a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.51.41' (ECDSA) to the list of known hosts.
root@172.16.51.41's password:
zoo.cfg                                                      100% 1019     1.0KB/s   00:00
[root@zk1 conf]#

然后各自创建myid文件编辑id。自己对应的1,2,3节点都编辑填上对应1,2,3标号即可。这里只给出zk1节点,2和3节点则是把数字改成2和3。

[root@zk1 ~]# cd
[root@zk1 ~]# mkdir /tmp/zookeeper
[root@zk1 ~]# vi /tmp/zookeeper/myid
[root@zk1 ~]# cat /tmp/zookeeper/myid
1
[root@zk1 ~]#

到启动目录下,启动zookeeper服务。将3个节点都这样启动以后再逐步查看它们的状态。

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

可以看到,3个节点,zookeeper2leader,其他的都是follower

[root@zk1 bin]#  ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower
[root@zk2 bin]#  ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: leader
[root@zk3 bin]#  ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: follower

那zookeeper集群弄完了,我们马上开始弄Kafka。

依旧是3节点都解压安装包。记得切换到root目录下。

[root@zk1 bin]# cd
[root@zk1 ~]# tar -zxvf kafka_2.11-1.1.1.tgz

然后改配置文件。在zookeeper1节点,进入kafka_2.11-1.1.1/config目录下,编辑server.properties文件。在配置文件中找到以下两行并注释掉。

因为这是每个节点都一样的注释区,所以我们在zk1节点上改完以后scp传给另外2个节点即可。

[root@zk1 ~]# cd kafka_2.11-1.1.1/config/
[root@zk1 config]# vi server.properties

[root@zk1 config]# scp server.properties root@172.16.51.32:/root/kafka_2.11-1.1.1/config/server.properties
The authenticity of host '172.16.51.32 (172.16.51.32)' can't be established.
ECDSA key fingerprint is a2:06:6f:65:a3:1f:9c:53:91:6d:0a:81:d0:6f:da:6a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.51.32' (ECDSA) to the list of known hosts.
root@172.16.51.32's password:
server.properties                                            100% 6855     6.7KB/s   00:00
[root@zk1 config]# scp server.properties root@172.16.51.41:/root/kafka_2.11-1.1.1/config/server.properties
The authenticity of host '172.16.51.41 (172.16.51.41)' can't be established.
ECDSA key fingerprint is a2:06:6f:65:a3:1f:9c:53:91:6d:0a:81:d0:6f:da:6a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.51.41' (ECDSA) to the list of known hosts.
root@172.16.51.41's password:
server.properties                                            100% 6855     6.7KB/s   00:00
[root@zk1 config]#

然后我们在文件末尾逐个添加节点们不一样的内容。

zk1节点

broker.id=1
zookeeper.connect=172.16.51.23:2181,172.16.51.32:2181,172.16.51.41:2181
listeners = PLAINTEXT://172.16.51.23:9092

zk2

broker.id=2
zookeeper.connect=172.16.51.23:2181,172.16.51.32:2181,172.16.51.41:2181
listeners = PLAINTEXT://172.16.51.32:9092

zk3

broker.id=3
zookeeper.connect=172.16.51.23:2181,172.16.51.32:2181,172.16.51.41:2181
listeners = PLAINTEXT://172.16.51.41:9092

然后我们在逐个节点上启动服务并用jps命令查看是否开启。这边就只贴zk1节点的图,剩下两个一样的步骤。

[root@zk1 config]# cd
[root@zk1 ~]# cd kafka_2.11-1.1.1/bin/
[root@zk1 bin]# ./kafka-server-start.sh -daemon ../config/server.properties
[root@zk1 bin]# jps
2868 Jps
2459 QuorumPeerMain
2799 Kafka

最好测试一下服务。

在zookeeper1节点,进入kafka_2.11-1.1.1/bin目录下,创建topic。如果成功的话,会输出“Created topic "test".”。

[root@zk1 bin]# ./kafka-topics.sh --create --zookeeper 172.16.51.23:2181 --replication-factor 1 --partitions 1 --topic test
Created topic "test".

查看topic,虽然topic是在172.16.51.23上创建的,但是在其他机器上也能看到。例如在任意启动的机器kafka_2.11-1.1.1/bin的目录中执行命令。

[root@zk2 bin]# ./kafka-topics.sh --list --zookeeper 172.16.51.32:2181
test
[root@zk3 bin]# ./kafka-topics.sh --list --zookeeper 172.16.51.41:2181
test

测试成功以后,我们离完全搭建完集群应用商城不远了。

我们接着去jar1和jar2节点上安装java环境。两个节点都要,截图就不多截了。

[root@jar1 ~]# yum install -y java java-devel

然后去nginx节点配置前端。先安装nginx服务。

[root@nginx ~]# yum install nginx -y

将提供的dist文件夹上传至Nginx节点的/root目录下,然后将dist中的内容复制到nginx的项目目录。

就把nginx默认的页面替换成我们的。

[root@nginx ~]# rm -rf /usr/share/nginx/html/*
[root@nginx ~]# cp -rvf dist/* /usr/share/nginx/html/

修改nginx配置文件设置代理地址端口。

[root@nginx ~]# vi /etc/nginx/conf.d/default.conf

添加这两块代码段至合适的位置。

        upstream myuser {
            server 172.16.51.11:8082;
            server 172.16.51.56:8082;
            ip_hash;
        }

        upstream myshopping {
            server 172.16.51.11:8081;
            server 172.16.51.56:8081;
            ip_hash;
        }
        upstream mycashier {
            server 172.16.51.11:8083;
            server 172.16.51.56:8083;
            ip_hash;
        }


    location /user {
            proxy_pass http://myuser;
        }

    location /shopping {
            proxy_pass http://myshopping;
        }

    location /cashier {
            proxy_pass http://mycashier;
        }

然后启动nginx服务,并查看端口是否启动成功。

[root@nginx ~]#  systemctl start nginx
[root@nginx ~]# yum install -y net-tools
[root@nginx ~]# 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      2491/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1131/sshd      
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1677/master    
tcp6       0      0 :::22                   :::*                    LISTEN      1131/sshd      
tcp6       0      0 ::1:25                  :::*                    LISTEN      1677/master    

最后去jar1和jar2节点运行项目包。

jar1节点:

如果对自己非常自信保证没有错误那么就都加上参数nohup逐一运行,这样看不到任何跑的回显。

命令如下:

[root@jar1 ~]# nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
[1] 11025
[root@jar1 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar1 ~]# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
[2] 11039
[root@jar1 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar1 ~]# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
[3] 11085
[root@jar1 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar1 ~]# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
[4] 11105
[root@jar1 ~]# nohup: ignoring input and appending output to ‘nohup.out’

这一句nohup: ignoring input and appending output to ‘nohup.out’并不是命令而是忽略回显。

jar2节点同样是按照这样的顺序跑完4个包的。

那这里因为我很久没搭建了,担心自己哪里出错了。所以我选择另外一种可以看回显的方式运行4个jar包。命令就是把前面nohup参数去掉即可。

我在jar1节点开始跑第一个包成功了,可以看到状态变为连接,并让我们等待一会。这样就可以了我们回车出来跑下一个。

第二个。

第三个。

第四个。

都跑成功了,有些弹出unknown error等等的不一定是错误,比如几乎每个包跑起来的时候都会出现未知错误连接不到zookeeper服务器的情况,但是过一会它就会再次连接并且连接上。这个时候状态就会显示这个也说明了成功连接上了zookeeper服务器。

既然jar1节点都跑完了并且成功了,那大概率jar2节点也没问题。我们先用命令查看下jar节点的包还在不在运行。

[root@jar1 ~]#  ps -aux |grep java
root       2567  1.6 13.8 4661880 536360 pts/0  Sl   01:21   0:14 java -jar user-provider-0.0.1-SNAPSHOT.jar
root       2632  1.5 13.4 4634208 518804 pts/0  Sl   01:24   0:11 java -jar shopping-provider-0.0.1-SNAPSHOT.jar
root       2694  2.5 14.3 4642336 556068 pts/0  Sl   01:26   0:15 java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar
root       2745  1.8 11.1 4617744 430000 pts/0  Sl   01:27   0:09 java -jar gpmall-user-0.0.1-SNAPSHOT.jar
root       2797  0.0  0.0 112660   964 pts/0    S+   01:36   0:00 grep --color=auto java
[root@jar1 ~]#

都没问题,我们继续去jar2节点上按顺序跑就行了。

[root@jar2 ~]# ps -ef |grep java
root       2563   2285  6 01:37 pts/0    00:00:15 java -jar user-provider-0.0.1-SNAPSHOT.jar
root       2631   2285  9 01:39 pts/0    00:00:11 java -jar shopping-provider-0.0.1-SNAPSHOT.jar
root       2683   2285 19 01:39 pts/0    00:00:15 java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar
root       2746   2285 27 01:40 pts/0    00:00:10 java -jar gpmall-user-0.0.1-SNAPSHOT.jar
root       2800   2285  0 01:40 pts/0    00:00:00 grep --color=auto java
[root@jar2 ~]#

我们两个节点都跑完了,接下来用本地机浏览器输入nginx节点的ip地址访问到商城测试即可。

访问成功!和我之前单节点搭建的文章测试方法一样,我们点击右上角头像完成账号登录,之后再进行购物模拟和收货地址模拟填写。

单击右上角“头像”,进行登录操作,使用用户名/密码为test/test进行登录。

然后下滑随便点击一个商品,进入购买页面。

点击现在购买

删除掉默认的收货地址填写一个新的地址。

至此,整个集群部署系统应用商城实验结束!

  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值