1、案例分析
1.1、规划节点
使用 Mycat 读写分离数据库作为应用系统的数据库后端,使用 ZooKeeper 集群,Kafka
集群提供应用系统调度服务,使用 Reids 提供应用系统消息队列服务。
1.2.基础准备
使用 CentOS 7.2 系统,flavor 使用 2vCPU/4G 内存/50G 硬盘,创建 3 台虚拟机分别为 jar1
节 点 、 jar2 节 点 和 Nginx 节 点 。 使 用 提 供 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 包为后端服
务包,在 jar1 节点和 jar2 节点上均运行,dist 为前端包,在 Nginx 节点上部署。
2、案例实施
1.基础环境配置
(1)修改主机名
登录 172.16.51.11、172.16.51.56、172.16.51.45 这 3 台虚拟机,使用 hostnamectl 命令修
改 3 台主机的主机名。
172.16.51.11 节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname jar1
172.16.51.56 节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname jar2
172.16.51.45 节点修改主机名命令:
[root@localhost ~]# hostnamectl set-hostname nginx
(2)配置 Yum 源
将提供的 gpmall-repo 文件夹上传至 jar1、jar2、nginx 这 3 个节点的/opt 目录下,然后
修改 Yum 源文件。
首先将 3 个节点/etc/yum.repo.d/目录下的所有文件移动到/media 目录下,命令如下:
# mv /etc/yum.repos.d/* /media/
然后在/etc/yum.repo.d 目录下创建 local.repo 文件,内容如下:
# cat /etc/yum.repos.d/local.repo
[gpmall]
name=gpmall
baseurl=file:///opt/gpmall-repo
gpgcheck=0
enabled=1
(3)安装 Java 环境
因为需要在 jar1、jar2 节点上运行提供的 4 个 JAR 包,所以需要在 jar1 和 jar2 节点上配
置 Java 环境。安装 java 环境命令如下:
jar1 节点:
[root@jar1 ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@jar1 ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
jar2 节点:
[root@jar2 ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
[root@jar2 ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
2.后端配置
(1)配置 hosts 文件
在 jar1 和 jar2 节点,编辑/etc/hosts 配置文件,文件内容如下:
jar1 节点:
[root@jar1 ~]# cat /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
172.16.51.23 kafka1.mall
172.16.51.32 kafka1.mall
172.16.51.41 kafka1.mall
172.16.51.23 zk1.mall
172.16.51.32 zk1.mall
172.16.51.41 zk1.mall
jar2 节点:
[root@jar2 ~]# cat /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
172.16.51.23 kafka1.mall
31
172.16.51.32 kafka1.mall
172.16.51.41 kafka1.mall
172.16.51.23 zk1.mall
172.16.51.32 zk1.mall
172.16.51.41 zk1.mall
(2)运行 jar 包
将提供的 4 个 jar 包,上传至 jar1、jar2 节点的/root 目录下,然后运行这 4 个 jar 包,命
令如下:
jar1 节点:
[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’
检查 jar 包是否运行,命令如下:
[root@jar1 ~]# ps -aux |grep java
root 11025 21.9 12.1 3605624 492588 pts/0 Sl 11:49 0:11 java -jar
user-provider-0.0.1-SNAPSHOT.jar
root 11039 21.3 9.9 3596260 400936 pts/0 Sl 11:49 0:10 java -jar
shopping-provider-0.0.1-SNAPSHOT.jar
root 11105 28.5 9.4 3590068 384476 pts/0 Sl 11:50 0:09 java -jar
gpmall-user-0.0.1-SNAPSHOT.jar
root 11193 161 12.7 3596228 514868 pts/0 Sl 11:50 0:14 java -jar
gpmall-shopping-0.0.1-SNAPSHOT.jar
root 11272 0.0 0.0 112640 960 pts/0 S+ 11:50 0:00 grep --color=auto
java
jar2 节点:
[root@jar2 ~]# nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
[1] 10915
[root@jar2 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar2 ~]# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
[2] 10929
[root@jar2 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar2 ~]# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
[3] 11004
[root@jar2 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@jar2 ~]# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
[4] 11024
[root@jar2 ~]# nohup: ignoring input and appending output to ‘nohup.out’
检查 jar 包是否运行,命令如下:
[root@jar2 ~]# ps -ef |grep java
root 10915 9970 27 11:50 pts/0 00:00:11 java -jar
user-provider-0.0.1-SNAPSHOT.jar
root 10929 9970 28 11:50 pts/0 00:00:10 java -jar
shopping-provider-0.0.1-SNAPSHOT.jar
root 11004 9970 49 11:51 pts/0 00:00:13 java -jar
gpmall-shopping-0.0.1-SNAPSHOT.jar
root 11024 9970 41 11:51 pts/0 00:00:09 java -jar
gpmall-user-0.0.1-SNAPSHOT.jar
root 11113 9970 0 11:51 pts/0 00:00:00 grep --color=auto java
至此,应用系统部署完毕。
3.前端配置
(1)安装 Nginx 服务
登录 Nginx 节点,安装 Nginx 服务,命令如下:
[root@nginx ~]# yum install nginx -y
(2)上传前端代码
将提供的 dist 文件夹上传至 Nginx 节点的/root 目录下,然后将 dist 中的内容复制到 nginx
的项目目录,命令如下:
首先将 Nginx 默认项目目录的文件删除:
[root@nginx ~]# rm -rf /usr/share/nginx/html/*
然后将 dist 中的文件复制至/usr/share/nginx/html 目录下:
[root@nginx ~]# cp -rvf dist/* /usr/share/nginx/html/
(3)修改 Nginx 配置文件
修改 Nginx 配置文件/etc/nginx/conf.d/default.conf 如下,添加** **的部分,IP 地址为
运行 JAR 包的 2 个服务器的 IP。
**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;
}**
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 服务
在 nginx 节点,启动 Nginx 服务,命令如下:
[root@nginx ~]# systemctl start nginx
查看 Nginx 是否启动,命令如下(查看 80 端口是否启动):
[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
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.45,访问界面,如图
单击右上角“头像”,进行登录操作,使用用户名/密码为 test/test 进行登录,如图
登录后,可进行购买商品操作,单击首页“地平线 8 号”旅行箱图片,如图
进入购买界面,单击“现在购买”按钮,进行购买商品操作,如图
单机完“现在购买”按钮,跳转到提交订单界面,如图
至此,集群部署应用系统完成。