docker部署mall商城

文章目录

      • 一、学习网址
      • 二、mall的docker部署
        • 1)docker环境安装
        • 2)部署harbor
        • 3)Mysql安装
        • 4)部署redis
        • 5)nginx安装
        • 6)RabbitMQ安装
        • 7)Elasticsearch安装
        • 8)Logstash安装
        • 9)kibana安装
        • 10)Mongodb安装
        • 11)安装minio
        • 12)Docker全部环境完成
      • 三、SpringBoot应用部署
        • 1)拉取源码
        • 2)组织结构
        • 3)部署maven
        • 4)修改harbor地址
        • 5)配置harbor仓库认证(https暂时存在问题)
        • 5)修改 mall 应用配置
        • 6)生成 mall 应用容器镜像
        • 7)部署mall-admin
        • 8)部署mall-search
        • 9)部署mall-port
        • 10)访问测试
      • 四、mall-tiny-docker
        • 1)下载源码
        • 2)修改仓库地址
        • 3)修改mysql数据库连接名
        • 4)给mysql授权
        • 5)构建镜像
        • 6)部署mall-tiny-docker应用服务
      • 五、mall前端部署
        • 1)安装npm命令
        • 2)修改prod.env.js配置
        • 3)构建打包代码
        • 4)修改Nginx发布代码
      • 六、mall后端和后端参考地址

mall项目是一套电商系统,包括前台商城系统及后台管理系统,基于SpringBoot+MyBatis实现,采用Docker容器化部署。前台商城系统包含首页门户、商品推荐、商品搜索、商品展示、购物车、订单流程、会员中心、客户服务、帮助中心等模块。后台管理系统包含商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等模块。

- 此文档是博主原创,转载需附上地址,商用需获得博主同意方可,如有问题可微信联系jiawenchao666666,简单问题可免费解决或留言,全程操作有偿指导非诚勿扰。
- 作者已完成k8s部署mall微服务,可有偿提供

一、学习网址

【源码地址】

  • 后台项目mall:https://github.com/macrozheng/mall
  • 前端项目mall-admin-web:https://github.com/macrozheng/mall-admin-web
  • 微服务mall-swarm:https://github.com/macrozheng/mall-swarm
  • 【学习目录】https://mp.weixin.qq.com/s/s_dKL9aAFXgtQi0VO1Ovdw
  • 【学习地址】https://zhuanlan.zhihu.com/p/450915863

二、mall的docker部署

【docker部署地址】

  • https://mp.weixin.qq.com/s?__biz=MzU1Nzg4NjgyMw==&mid=2247483786&idx=1&sn=33052d6967de1a2c9592b2a1c58b8bef&scene=21#wechat_redirect
1)docker环境安装
#安装yum-utils
yum install -y yum-utils device-mapper-persistent-data lvm2

#为yum源添加docker仓库位置:
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

#安装docker:
yum install docker-ce -y

#启动docker:
systemctl start docker
systemctl enable docker
2)部署harbor
#创建镜像,首先搭建镜像
docker run -d -p 5000:5000 --restart=always --name registry2 registry:2

镜像下载完毕后我要们开启远程API

[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
#修改前
ExecStart=/usr/bin/dockerd   -H fd:// --containerd=/run/containerd/containerd.sock

#修改后
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
#让Docker支持http上传镜像
echo '{ "insecure-registries":["192.168.4.116:5000"] }' >> /etc/docker/daemon.json

#修改配置后需要使用如下命令使配置生效
systemctl daemon-reload

#重新启动Docker服务
systemctl stop docker
systemctl start docker

#开启防火墙的Docker构建端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload

#登录harbor
docker login -uadmin  -pHarbor  192.168.4.116:5000

maven构建时修改pom.xml文件中仓库地址

修改前
  
        UTF-8
        UTF-8
        1.8
        true
        unix:///var/run/docker.sock

修改后
#改为自己harbor地址;
 
        UTF-8
        UTF-8
        1.8
        true
        
        http://192.168.4.116:2375

注意:一定要按照格式编写,否则可能构建错误,同时不要输入错误,已经入坑

3)Mysql安装
#下载mysql
docker pull mysql:5.7
#启动mysql
docker run -p 3306:3306 --name mysql \
    --restart=always \
	-v /mydata/mysql/log:/var/log/mysql \
	-v /mydata/mysql/data:/var/lib/mysql \
	-v /mydata/mysql/conf:/etc/mysql \
	-e MYSQL_ROOT_PASSWORD=root  \
	-d mysql:5.7

参数说明:

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口
  • -v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂在到主机
  • -v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
  • -v /mydata/mysql/data:/var/lib/mysql/:将数据文件夹挂载到主机
  • -e MYSQLROOTPASSWORD=root:初始化root用户的密码
#sql地址
yum install git -y 
git clone https://gitee.com/aliyunfc/mall


#将mall.sql文件拷贝到mysql容器的/opt目录下
docker cp document/sql/mall.sql mysql:/opt


#进入运行mysql的docker容器
docker exec -it mysql /bin/bash

#使用mysql命令打开客户端
mysql -uroot -proot --default-character-set=utf8

#创建mall数据库
create database mall character set utf8;

#将sql文件导入到数据库:
use mall;
source /opt/mall.sql;

#创建一个reader帐号并修改权限,使得任何ip都能访问:
grant all privileges on *.* to 'reader' @'%' identified by '123456';
flush privileges;
exit
4)部署redis
#下载redis3.2的docker镜像
docker pull redis:3.2

#使用docker命令启动
docker run -p 6379:6379 --name redis \
    --restart=always \
    -v /mydata/redis/data:/data \
    -d redis:3.2 redis-server --appendonly yes
    
#进入redis容器使用redis-cli命令进行连接
docker exec -it redis redis-cli
[root@localhost sql]# docker exec -it redis redis-cli
127.0.0.1:6379> set a 100
OK
127.0.0.1:6379> get a
"100"
127.0.0.1:6379> exit

5)nginx安装
#下载nginx1.10的docker镜像
docker pull nginx:1.10

#先运行一次容器(为了拷贝配置文件)
docker run -p 80:80 --name nginx \
	-v /mydata/nginx/html:/usr/share/nginx/html \
	-v /mydata/nginx/logs:/var/log/nginx  \
	-d nginx:1.10

#将容器内的配置文件拷贝到指定目录:
docker container cp nginx:/etc/nginx /mydata/nginx/

#修改文件名称:
mv /mydata/nginx/nginx/ /mydata/nginx/conf
#终止并删除容器:
docker stop nginx
docker rm nginx

#使用docker命令启动:
docker run -p 80:80 --name nginx \
    --restart=always \
	-v /mydata/nginx/html:/usr/share/nginx/html \
	-v /mydata/nginx/logs:/var/log/nginx  \
	-v /mydata/nginx/conf:/etc/nginx \
	-d nginx:1.10
6)RabbitMQ安装
#下载rabbitmq3.7.15的docker镜像
docker pull rabbitmq:3.7.15

#使用docker命令启动:
docker run -d --name rabbitmq \
    --restart=always \
	--publish 5671:5671 --publish 5672:5672 --publish 4369:4369 \
	--publish 25672:25672 --publish 15671:15671 --publish 15672:15672 \
	rabbitmq:3.7.15

#进入容器并开启管理功能:
docker exec -it rabbitmq /bin/bash
rabbitmq-plugins enable rabbitmq_management
exit
[root@localhost nginx]# docker exec -it rabbitmq /bin/bash
root@d2c6e05857e5:/# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@d2c6e05857e5:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@d2c6e05857e5...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
  
#开启防火墙端口15672:
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload

访问:http://192.168.4.116:15672/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aRjrhK2l-1653057559891)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGM7eLgtJvZJNKxknWsQm0JfeBMuPjMQ*O6fP1Em6s6tHDetvaiAstTf57Zh9Hn7UCo!/b&bo=PgKOAD4CjgADFzI!&rf=viewer_4&t=5)]

输入账号密码并登录:guest guest

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6zYngiyn-1653057559892)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5rvcmOCSIWSdd6V3jg7KEGNMZdlrkHyb.Z1piHR4kaSAvw3kXqNGlj.w0aztjX90XY0r9.zGiM2Ja2l8mSPdY!/b&bo=PgJLAT4CSwEDFzI!&rf=viewer_4&t=5)]

创建帐号并设置其角色为管理员:mall mall

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kJqbanQR-1653057559893)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5syHFThBnru1qq1h.GWNYsqWXCSWBpJySalNN2F2RvVmQ6mM8PGQ7lNzZhbn01qelauOmpB*DZN5otCnEMnFw!/b&bo=PgLHAT4CxwEDFzI!&rf=viewer_4&t=5)]

创建一个新的虚拟host为:/mall

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-miPJuqT1-1653057559893)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5syHFThBnru1qq1h.GWNYuifQGpS2L8obqtRO7NLs6IifIaozrPzKjA5ve7Vosl5exv0LPYnk8Sra5ghRaZbc!/b&bo=PgKYAD4CmAADFzI!&rf=viewer_4&t=5)]

点击mall用户进入用户配置页面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ELxjnHXg-1653057559893)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5qulVCkBQiVOnkvnEEL.m4DT8ncWtYJHfG5ln5LV5tk5vD.QWdBLEjmLfVj3*TnDjWSjvG16bmlS8bqVLBG19BU!/b&bo=PgLSAD4C0gADFzI!&rf=viewer_4&t=5)]
给mall用户配置该虚拟host的权限

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HwJGVmo0-1653057559894)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5qulVCkBQiVOnkvnEEL.m4Awz11gNc7DVZz27esr6X3UMJxAB6JOOa1KN7SwiI5DBHNB7E8D8yNCE679d7szM!/b&bo=PgI.AT4CPgEDFzI!&rf=viewer_4&t=5)]

创建mall用户给/mall主机授期结果如图所示。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hCdD3tHm-1653057559894)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5r0SPNyPsWTUhUCVVn0k4.I2YjM.tKH**VTuAwxT*.TmSzH1jnVxgUgKrtskY4yonIkKvNgqEgtrxcVVJRhznlo!/b&bo=PgLsAD4C7AADFzI!&rf=viewer_4&t=5)]

【阿里云使用方式】http://www.macrozheng.com/mall/deploy/mall_deploy_windows.html#%E5%88%9B%E5%BB%BA%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4

7)Elasticsearch安装
#下载elasticsearch6.4.0的docker镜像:
docker pull elasticsearch:6.4.0

#修改虚拟内存区域大小,否则会因为过小而无法启动:
sysctl -w vm.max_map_count=262144

#使用docker命令启动:
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch \
    --restart=always \
	-e "discovery.type=single-node" \
	-e "cluster.name=elasticsearch" \
	-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
	-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
	-d elasticsearch:6.4.0

#启动时会发现/usr/share/elasticsearch/data目录没有访问权限,只需要修改/mydata/elasticsearch/data目录的权限,再重新启动。
chmod 777 /mydata/elasticsearch/data/

#安装中文分词器IKAnalyzer,并重新启动:
docker exec -it elasticsearch /bin/bash

#此命令需要在容器中运行
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
[root@32144efeca9c elasticsearch]# elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip
[=================================================] 100%??
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
[root@32144efeca9c elasticsearch]# exit

#重新镜像
docker restart elasticsearch

#开启防火墙:
firewall-cmd --zone=public --add-port=9200/tcp --permanent
firewall-cmd --reload

访问会返回版本信息:http://192.168.4.116:9200/

# 访问返回信息

{
  "name" : "aQomelU",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "3ZdM5rTpSqSBFqtIZkXJ_g",
  "version" : {
    "number" : "6.4.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "595516e",
    "build_date" : "2018-08-17T23:18:47.308994Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
8)Logstash安装
#载Logstash7.6.2的docker镜像:
docker pull logstash:7.6.2
docker pull logstash:6.5.1
#修改Logstash的配置文件logstash.conf中output节点下的Elasticsearch连接地址为es:9200,配置文件地址:https://github.com/macrozheng/mall/blob/master/document/elk/logstash.conf

output {
  elasticsearch {
    hosts => "es:9200"
    index => "mall-%{type}-%{+YYYY.MM.dd}"
  }
}

#创建/mydata/logstash目录,并将Logstash的配置文件logstash.conf拷贝到该目录;
mkdir /mydata/logstash
$ vim /mydata/logstash/logstash.conf

#创建好配置文件后启动Logstash服务;
docker run --name logstash -p 4560:4560 -p 4561:4561 -p 4562:4562 -p 4563:4563 \
--restart=always \
--link elasticsearch:es \
-v /mydata/logstash/logstash.conf:/usr/share/logstash/pipeline/logstash.conf \
-d logstash:7.6.2

#进入容器内部,安装json_lines插件。
docker exec -it logstash /bin/bash
logstash-plugin install logstash-codec-json_lines
#logstash.conf详情
[root@localhost logstash]# cat logstash.conf
input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
    type => "debug"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4561
    codec => json_lines
    type => "error"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4562
    codec => json_lines
    type => "business"
  }
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4563
    codec => json_lines
    type => "record"
  }
}
filter{
  if [type] == "record" {
    mutate {
      remove_field => "port"
      remove_field => "host"
      remove_field => "@version"
    }
    json {
      source => "message"
      remove_field => ["message"]
    }
  }
}
output {
  elasticsearch {
    hosts => "es:9200"
    index => "mall-%{type}-%{+YYYY.MM.dd}"
  }
}

9)kibana安装
#下载kibana6.4.0的docker镜像:
docker pull kibana:6.4.0

#使用docker命令启动:
docker run --name kibana -p 5601:5601 \
    --restart=always \
	--link elasticsearch:es \
	-e "elasticsearch.hosts=http://es:9200" \
	-d kibana:6.4.0
	
#开启防火墙:
firewall-cmd --zone=public --add-port=5601/tcp --permanent
firewall-cmd --reload

访问地址进行测试:http://192.168.4.116:5601

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UA7iRnK3-1653057559894)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5qCaveFxPzKFUckU5v.KbzJLrFk7hz3oKTjRlcQCD0mZftxlGcn3l8ZYMccj1Il0he4StxoJG7gOxZHTaU41.lc!/b&bo=PgJdAT4CXQEDFzI!&rf=viewer_4&t=5)]

10)Mongodb安装
#下载mongo3.2的docker镜像:
docker pull mongo:3.2

#使用docker命令启动:
docker run -p 27017:27017 --name mongo \
    --restart=always \
    -v /mydata/mongo/db:/data/db \
    -d mongo:3.2
11)安装minio
docker pull minio/minio
docker run -p 9000:9000 --name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=minioadmin" \
-e "MINIO_SECRET_KEY=minioadmin" \
-v /mydata/minio/data:/data \
-v /mydata/minio/config:/root/.minio \
minio/minio server /data \
--console-address ":9000" --address ":9090"
  • 访问:http://192.168.4.116:9000/login
  • 【oss存储】https://blog.csdn.net/weixin_45879810/article/details/117108821
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --reload
12)Docker全部环境完成
#所用的镜像
[root@localhost nginx]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED       SIZE
mysql           5.7       8aa4b5ffb001   2 days ago    462MB
rabbitmq        3.7.15    b3639fca0afd   2 years ago   149MB
mongo           3.2       fb885d89ea5c   3 years ago   300MB
redis           3.2       87856cc39862   3 years ago   76MB
kibana          6.4.0     a7e4cd1a7b45   3 years ago   667MB
elasticsearch   6.4.0     1ac676545731   3 years ago   791MB
nginx           1.10      0346349a1a64   5 years ago   182MB

#启动的docker容器
[root@localhost nginx]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS              NAMES
6d6ff9700259   mongo:3.2             "docker-entrypoint.s…"   11 seconds ago      Up 9 seconds       0.0.0.0:27017->27017/tcp, :::27017->27017/tcp              mongo
1a43beb74905   kibana:6.4.0          "/usr/local/bin/kiba…"   8 minutes ago       Up 8 minutes       0.0.0.0:5601->5601/tcp, :::5601->5601/tcp              kibana
32144efeca9c   elasticsearch:6.4.0   "/usr/local/bin/dock…"   25 minutes ago      Up 11 minutes      0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp              elasticsearch
d2c6e05857e5   rabbitmq:3.7.15       "docker-entrypoint.s…"   50 minutes ago      Up 50 minutes      0.0.0.0:4369->4369/tcp, :::4369->4369/tcp, 0.0.0.0:5671-5672->5671-5672/tcp, :::5671-5672->5671-5672/tcp, 0.0.0.0:15671-15672->15671-15672/tcp, :::15671-15672->15671-15672/tcp, 0.0.0.0:25672->25672/tcp, :::25672->25672/tcp   rabbitmq
50f6143a02fb   nginx:1.10            "nginx -g 'daemon of…"   53 minutes ago      Up 53 minutes      0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp              nginx
84a749f56bd3   redis:3.2             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp              redis
2ad5695e156f   mysql:5.7             "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp              mysql


三、SpringBoot应用部署

1)拉取源码
#进入/opt下载源码
cd /opt/

#拉取
#git clone https://github.com/macrozheng/mall-swarm 
#git clone https://github.com/macrozheng/mall-admin-web
git clone https://github.com/macrozheng/mall

#此处使用源码
git clone https://gitee.com/aliyunfc/mall
【老版本】
#Clone 代码仓库
git clone https://github.com/hryang/mall

#国内访问 github 网络不太好,如果 clone 太慢,可使用 Gitee 地址。
git clone https://gitee.com/aliyunfc/mall
2)组织结构
root@k8s-master-01 mall]# cd /opt/mall/mall
mall
├── mall-common -- 工具类及通用代码
├── mall-mbg -- MyBatisGenerator生成的数据库操作代码
├── mall-security -- SpringSecurity封装公用模块
├── mall-admin -- 后台商城管理系统接口
├── mall-search -- 基于Elasticsearch的商品搜索系统
├── mall-portal -- 前台商城系统接口
└── mall-demo -- 框架搭建时的测试代码
3)部署maven
#部署maven
yum install maven -y

#修改mvn配置源为阿里源配置
【地址】https://developer.aliyun.com/mvn/guide
[root@k8s-master-01 ~]# vim /etc/maven/settings.xml
159行加入
     -->
##################加入内容##

  aliyunmaven
  *
  阿里云公共仓库
  https://maven.aliyun.com/repository/public

####################加入内容##
  
  
        http://192.168.4.116:2375
        1.2.2
        1.3.0
5)配置harbor仓库认证(https暂时存在问题)
#所有需要使用harbor仓库皆需要配置
cat  >>/etc/docker/daemon.json<>/etc/hosts

#拷贝认证秘钥
scp -r /data/cert/ssl/docker.gayj.cn.crt  [email protected]:/etc/docker/certs.d/docker.gayj.cn/

#重启docker
systemctl daemon-reload
systemctl restart docker.service

#登录仓库
docker login -uadmin  -pHarbor  docker.gayj.cn

登录harbor仓库创建镜像目录

  • https://docker.gayj.cn/harbor/sign-in
  • 账户:admin
  • 密码:Harbor12345
  • 创建mall私有仓库目录
  • 打包镜像名称为:docker.gayj.cn/mall/xxx:xxx
5)修改 mall 应用配置

修改

  • mall-admin/src/main/resources/application-prod.yml
  • mall-portal/src/main/resources/application-prod.yml
  • mall-search/src/main/resources/application-prod.yml
  • 将其中的 host 字段改成您第1步安装 MySQL 等软件的节点的公网 ip。
#批量修改将IP替换为db
find . -name "*application-prod.yml" |xargs -i  sed -i "s/112.124.39.109/db/g" {}

#批量查看
find . -name "application-prod.yml" |xargs -i  cat {}

注意:yaml文件中的地址都需要修改,所以不能用批量修改。

6)生成 mall 应用容器镜像
  • 执行 maven 打包命令,生成 docker 镜像。
  • 本地是 java8 或者 java11 环境均可
#在应用的pom.xml文件中添加docker-maven-plugin的依赖详情(源码中存在不用添加)
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
                
                    com.spotify
                    docker-maven-plugin
                    ${docker.maven.plugin.version}
                    
                        
                            build-image
                            package
                            
                                build
                            
                        
                    
                    
                        mall/${project.artifactId}:${project.version}
                        ${docker.host}
                        java:8
                        ["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]
                        
                        
                            
                                /
                                ${project.build.directory}
                                ${project.build.finalName}.jar
                            
                        
                    
                
            
        
    

  • executions.execution.phase:此处配置了在maven打包应用时构建docker镜像;
  • imageName:用于指定镜像名称,mall是仓库名称, p r o j e c t . a r t i f a c t I d 为镜像名称, {project.artifactId}为镜像名称, project.artifactId为镜像名称,{project.version}为镜像版本号;
  • dockerHost:打包后上传到的docker服务器地址;
  • baseImage:该应用所依赖的基础镜像,此处为java;
  • entryPoint:docker容器启动时执行的命令;
  • resources.resource.targetPath:将打包后的资源文件复制到该目录;
  • resources.resource.directory:需要复制的文件所在目录,maven打包的应用jar包保存在target目录下面;
  • resources.resource.include:需要复制的文件,打包好的应用jar包。
#部署Java环境
yum install java-1.8.0-openjdk maven -y
#清理构建
cd /opt/mall/
mvn clean package -Dmaven.test.skip=true

【报错】
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:1.2.2:build (build-image) on project mall-admin: Exception caught: url has null scheme -> [Help 1]

【原因】
Docker未开启2375端口

【解决】
$  vim /usr/lib/systemd/system/docker.service
#修改前
ExecStart=/usr/bin/dockerd

#修改后
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

systemctl daemon-reload 
systemctl restart docker
firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload

如果不能解决问题,需要查看pom.xml中docker地址是否正确

        http://192.168.4.116:2375
[root@localhost mall]# mvn clean package -Dmaven.test.skip=true
。。。。。。
[INFO] mall-security ..................................... SUCCESS [1.073s]
[INFO] mall-demo ......................................... SUCCESS [6.960s]
[INFO] mall-admin ........................................ SUCCESS [22.166s]
[INFO] mall-search ....................................... SUCCESS [10.760s]
[INFO] mall-portal ....................................... SUCCESS [9.791s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:32.102s
[INFO] Finished at: Sat Apr 30 17:22:06 CST 2022
[INFO] Final Memory: 87M/1056M
[INFO] ------------------------------------------------------------------------

#生成镜像
[root@localhost mall]# docker images
REPOSITORY         TAG            IMAGE ID       CREATED          SIZE
mall/mall-portal   1.0-SNAPSHOT   1f4d35e6800f   5 minutes ago    708MB
mall/mall-search   1.0-SNAPSHOT   a900979ac21b   5 minutes ago    727MB
mall/mall-admin    1.0-SNAPSHOT   18c25905a078   5 minutes ago    707MB
                      976fdd88297e   19 minutes ago   707MB
mysql              5.7            8aa4b5ffb001   2 days ago       462MB
registry           2              2e200967d166   3 weeks ago      24.2MB
java               8              d23bdf5b1b1b   5 years ago      643MB
7)部署mall-admin
docker run -p 8080:8080 --name mall-admin \
    --restart=always \
	--link mysql:db \
	-v /etc/localtime:/etc/localtime \
	-v /mydata/app/admin/logs:/var/logs \
	-d mall/mall-admin:1.0-SNAPSHOT

注意:CenterOS7.2版本需要加入此行,否则容器时区和宿主机无法同步

  • -v /etc/timezone:/etc/timezone \
8)部署mall-search
docker run -p 8081:8081 --name mall-search \
    --restart=always \
	--link elasticsearch:es \
	--link mysql:db \
	-v /etc/localtime:/etc/localtime \
	-v /mydata/app/search/logs:/var/logs \
	-d mall/mall-search:1.0-SNAPSHOT
9)部署mall-port
docker run -p 8085:8085 --name mall-portal \
    --restart=always \
	--link mysql:db \
	--link redis:redis \
	--link mongo:mongo \
	--link rabbitmq:rabbit \
	-v /etc/localtime:/etc/localtime \
	-v /mydata/app/portal/logs:/var/logs \
	-d mall/mall-portal:1.0-SNAPSHOT
#开启防火墙
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-port=8085/tcp --permanent
firewall-cmd --reload
10)访问测试

all-admin的api接口文档地址:http://192.168.4.116:8080/swagger-ui.html
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rS1T7xvu-1653057559895)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5tnhRI48U4WtGgJmwH3SPlonv7m2DhtmhDXHFt1SrX7cusC0ZGAu0fNE2x308opJC7kY9vMPYOh9ref4WStpRyU!/b&bo=PgLkAT4C5AEDFzI!&rf=viewer_4&t=5)]

mall-search的api接口文档地址:http://192.168.4.116:8081/swagger-ui.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hhS6lDJu-1653057559895)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5tnhRI48U4WtGgJmwH3SPlokFNMHq8pE4SntxA6X5eOmcmKy0ikIzLDV2UeZg2KO8RPeQNICj7nudwo9VOb*VC0!/b&bo=PgKqAT4CqgEDFzI!&rf=viewer_4&t=5)]

mall-portal的api接口文档地址:http://192.168.4.116:8085/swagger-ui.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-raFCute5-1653057559895)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5tnhRI48U4WtGgJmwH3SPlqIzDZQJLTyzz6lWMsFIlAySbiLY9Dt7sAUnhleikg2BgJ2TWHiwnT5a12xUIl9I!/b&bo=PgLqAT4C6gEDFzI!&rf=viewer_4&t=5)]

四、mall-tiny-docker

【源码地址】https://github.com/macrozheng/mall-learning.git

  • https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-docker
    【部署参考】
  • http://www.macrozheng.com/mall/reference/docker_maven.html#docker-registry
    【发布部署文档】
  • https://github.com/macrozheng/mall-learning/commit/d7f571f8f817229583f89b239976551e6dc85a8c
1)下载源码
#到/opt下
cd /opt
git clone https://github.com/macrozheng/mall-learning.git
mvn clean package -Dmaven.test.skip=true
2)修改仓库地址
#修改仓库地址
$ vim /opt/mall-learning/mall-tiny-docker/pom.xml
  
                    mall-tiny/${project.artifactId}:${project.version}
                    http://192.168.4.116:2375
                    java:8
                    ["java", "-jar","/${project.build.finalName}.jar"]

3)修改mysql数据库连接名

可以把docker中的容器看作独立的虚拟机,mall-tiny-docker访问localhost自然会访问不到mysql,docker容器之间可以通过指定好的服务名称db进行访问,至于db这个名称可以在运行mall-tiny-docker容器的时候指定。

#修改application.yml,将localhost改为192.168.4.116

#批量修改
find /opt/mall-learning/mall-tiny-docker/ -name "*application.yml" |xargs -i  sed -i  "s/localhost:3306/192.168.4.116:3306/g" {}

#批量查看
find /opt/mall-learning/mall-tiny-docker/  -name "application.yml" |xargs -i  cat {}

注意需要测试mysql能否远程连接,能远程地址即IP地址。

#查看详情
[root@localhost mall-portal]# cat /opt/mall-learning/mall-tiny-docker/src/main/resources/application.yml
server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://192.168.4.116:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: root

mybatis:
  mapper-locations:
    - classpath:mapper/*.xml
    - classpath*:com/**/mapper/*.xml

4)给mysql授权

因为连接数据库的是用户名是root,密码是root

#进入mysql
docker exec -it mysql /bin/bash
mysql -uroot -proot --default-character-set=utf8

#修改root帐号的权限,使得任何ip都能访问:
grant all privileges on *.* to 'root'@'%';
flush privileges;
exit
5)构建镜像
#构建
cd /opt/mall-learning/mall-tiny-docker
[root@localhost mall-tiny-docker]# mvn clean package -Dmaven.test.skip=true
。。。。。。
Successfully built d24b0dd74bdf
Successfully tagged mall-tiny/mall-tiny-docker:0.0.1-SNAPSHOT
[INFO] Built mall-tiny/mall-tiny-docker:0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:16.588s
[INFO] Finished at: Sat Apr 30 20:27:29 CST 2022
[INFO] Final Memory: 55M/369M
[INFO] ------------------------------------------------------------------------
6)部署mall-tiny-docker应用服务
docker run -p 8080:8083 --name mall-tiny-docker \
    --restart=always \
    --link mysql:db \
    -v /etc/localtime:/etc/localtime \
    -v /mydata/app/mall-tiny-docker/logs:/var/logs \
    -d mall-tiny/mall-tiny-docker:0.0.1-SNAPSHOT

由于8080端口占用冲突,所以启动8083

  • 访问:http://192.168.4.116:8083/swagger-ui.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BeOMOF7X-1653057559896)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5vLz3PAnnghodxmas9UmQFMwKTMdX6N7QggCgr5Jacy6UKTQP7Fagri.4T.rPxpaSU*zLP7XX1CZcTjzpegFHak!/b&bo=PgIKAT4CCgEDFzI!&rf=viewer_4&t=5)]

五、mall前端部署

1)安装npm命令

wget -c  https://nodejs.org/dist/v12.14.0/node-v12.14.0-linux-x64.tar.xz
tar xvf node-v12.14.0-linux-x64.tar.xz -C  /usr/local/

ln -s /usr/local/node-v12.14.0-linux-x64/bin/node /usr/local/bin/node
ln -s /usr/local/node-v12.14.0-linux-x64/bin/npm /usr/local/bin/npm

#由于官方镜像比较慢,直接执行以下命令,将镜像地址改为淘宝的npm镜像地址
npm install -g cnpm --registry=https://registry.npm.taobao.org
npm -v

#全局更新
npm install npm -g 
2)修改prod.env.js配置
  • 注意:如果不改则是使用线上作者提供的API地址。
  • 未搭建mall后台的需要使用线上api进行访问,线上API地址:https://admin-api.macrozheng.com
#修改为自己后台API地址
[root@localhost config]# cat  /opt/mall-admin-web/config/prod.env.js
'use strict'
module.exports = {
  NODE_ENV: '"production"',
  BASE_API: '"https://admin-api.macrozheng.com"'
}

此次后端API地址是:192.168.4.116

[root@localhost opt]# sed -i 's#https://admin-api.macrozheng.com#http://192.168.4.116:8080#g' /opt/mall-admin-web/config/prod.env.js
[root@localhost config]# cat  /opt/mall-admin-web-master/config/prod.env.js
'use strict'
module.exports = {
  NODE_ENV: '"production"',
  BASE_API: '"http://192.168.4.116:8080"'
}

3)构建打包代码
cd /opt/mall-admin-web
#清除缓存
npm cache clean --force
npm install
npm run build
#安装详情
[root@localhost mall-admin-web]# npm install

up to date, audited 1275 packages in 47s

1 package is looking for funding
  run `npm fund` for details

123 vulnerabilities (5 low, 70 moderate, 39 high, 9 critical)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

#构建详情
[root@localhost mall-admin-web]# npm run build
.......
                               static/tinymce4.7.5/tinymce.min.js     834 kB          [emitted]  [big]

  Build complete.

  Tip: built files are meant to be served over an HTTP server.
  Opening index.html over file:// won't work.

#打包后的代码
[root@localhost mall-admin-web]# ll /opt/mall-admin-web/dist/
总用量 4
-rw-r--r-- 1 root root 1159 4月  30 22:44 index.html
drwxr-xr-x 7 root root   71 4月  30 22:44 static
4)修改Nginx发布代码
#找到Nginx的发布目录
[root@localhost mall-admin-web]# ls /mydata/nginx/html/
[root@localhost mall-admin-web]# mv dist html

#将打包好前端文件放nginx目录下
[root@localhost mall-admin-web]# mv /opt/mall-admin-web/html/  /mydata/nginx/
mv:是否覆盖"/mydata/nginx/html"? yes
[root@localhost mall-admin-web]# ls /mydata/nginx/html/
index.html  static

#重启
docker restart  mall-admin
docker restart nginx

访问:http://192.168.4.116

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pt2lnsEA-1653057559896)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5kD3tGoiX64SCzyQEi7siYA78sMR.rmTH3OwdnCGG0LEDeC6m1NrCuETtSE4QLRlEuKL1aZmvHsqxYDmucH9Y!/b&bo=PgJUAT4CVAEDFzI!&rf=viewer_4&t=5)]

进行登录操作

  • 账户:admin
  • 密码:macro123

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YHSNJwU0-1653057559896)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5kD3tGoiX64SCzyQEi7siYBRIfXL0ppLlCEWWHban1AY3fKbn8j29jRwANgwStn2cdJS0rZ74BXAlQ01LyLf6o0!/b&bo=PgKaAT4CmgEDFzI!&rf=viewer_4&t=5)]

登录过程查看,此时为作者后端地址登录

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GxxRK3UJ-1653057559897)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5nTTecQWogYjuNlPtJPfhn*KNwVjZAnHMtyEYusbz6b1vV0VXlUsorwomi366Sw84sGdcNXKyCPTAxJKKxzuDxY!/b&bo=PgKuAj4CrgIDJwI!&rf=viewer_4&t=5)]

修改为自己后端API地址时登录如下图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZpTG5iyV-1653057559897)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5iXOh8tIZtZvixQDhdf07vrsgLCxVx2FsaNnOwKkld77jeaALg1Eagdeu.QUleyCZaTtdSUkPYqedvDOBFv7yhY!/b&bo=MALiATAC4gEDFzI!&rf=viewer_4&t=5)]

后端测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EH63iFlZ-1653057559897)(http://m.qpic.cn/psc?/V53GU2po3e4Vc929i8d01BP45G32VjvB/ruAMsa53pVQWN7FLK88i5hZHi9wPSAqmZlX9Jrk7K6OyFaSEEsyKqQpBvmAx2R7n30j7zoxqsmRUDi*BVd2sL7.ipEIiozb4m.ghSMxoE!/b&bo=PgIuAT4CLgEDFzI!&rf=viewer_4&t=5)]

六、mall后端和后端参考地址

【制作镜像可参考】

  • https://www.icode9.com/content-4-847241.html
  • https://www.cnblogs.com/wangluf/p/15674509.html
  • https://blog.csdn.net/qq_46162321/article/details/122462429

【重点参考】

  • https://www.cnblogs.com/jinsheng1027/p/12232927.html
  • http://www.macrozheng.com/mall/deploy/mall_deploy_docker_compose.html#%E9%83%A8%E7%BD%B2%E7%9B%B8%E5%85%B3%E6%96%87%E4%BB%B6
  • 【构建镜像】http://www.macrozheng.com/mall/reference/docker_maven.html#docker-registry
    【官方部署文档】
  • https://www.macrozheng.com/mall/catalog/mall_catalog.html#%E5%8F%8B%E6%83%85%E6%8F%90%E7%A4%BA
  • 【官方镜像地址】https://hub.docker.com/

  • 此文档是博主原创,转载需附上地址,商用需获得博主同意方可,如有问题可微信联系jiawenchao666666,简单问题可免费解决或留言,全程操作有偿指导非诚勿扰。
  • 作者已完成k8s部署mall微服务,可有偿提供。
访问:http://192.168.4.116

[外链图片转存中...(img-Pt2lnsEA-1653057559896)]

进行登录操作
- 账户:admin
- 密码:macro123

[外链图片转存中...(img-YHSNJwU0-1653057559896)]

登录过程查看,此时为作者后端地址登录

[外链图片转存中...(img-GxxRK3UJ-1653057559897)]

修改为自己后端API地址时登录如下图

[外链图片转存中...(img-ZpTG5iyV-1653057559897)]

后端测试

[外链图片转存中...(img-EH63iFlZ-1653057559897)]

### 六、mall后端和后端参考地址
【制作镜像可参考】
- https://www.icode9.com/content-4-847241.html
- https://www.cnblogs.com/wangluf/p/15674509.html
- https://blog.csdn.net/qq_46162321/article/details/122462429

【重点参考】
 - https://www.cnblogs.com/jinsheng1027/p/12232927.html
 - http://www.macrozheng.com/mall/deploy/mall_deploy_docker_compose.html#%E9%83%A8%E7%BD%B2%E7%9B%B8%E5%85%B3%E6%96%87%E4%BB%B6
 - 【构建镜像】http://www.macrozheng.com/mall/reference/docker_maven.html#docker-registry
【官方部署文档】
 - https://www.macrozheng.com/mall/catalog/mall_catalog.html#%E5%8F%8B%E6%83%85%E6%8F%90%E7%A4%BA
 - 【官方镜像地址】https://hub.docker.com/




你可能感兴趣的:(docker学习,docker,容器,运维,服务器,linux,k8s)