MiniMall:Linux环境下的部署(基于Docker Compose)

在这之前,我们学习了如何将mini-mall项目基于Docker部署到Linux服务器,我们一个个启动了项目运行环境所需的容器,比如nginxmysql等等。然后还通过将微服务打包成jar包,使用java -jar命令启动运行程序。

  • 项目所需的环境

MiniMall:Linux环境下的部署(基于Docker Compose)_第1张图片

  • 微服务工程

MiniMall:Linux环境下的部署(基于Docker Compose)_第2张图片

不过话说回来,这么多的容器和微服务工程,如果一个个手动启动的话,我感觉要疯。所以学习使用Docker Compose来部署我们的项目就显得很有必要了。

1. Docker Compose

Docker Compose是一个编排多容器分布式部署的工具,提供命令集管理容器化应用的完整开发周期,包括服务构建,启动和停止。通常来说,使用Docker Compose的步骤:

  1. 利用Dockerfile定义运行环境镜像。
  2. 使用docker-compose.yml定义组成应用的各服务。
  3. 运行docker-compose up启动应用。

1.1 安装Docker Compose

# Compose目前已经完全支持Linux、Mac OS和Windows,在我们安装Compose之前,需要先安装Docker。下面我 们以编译好的二进制包方式安装在Linux系统中。 
sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 设置文件可执行权限 
sudo chmod +x /usr/local/bin/docker-compose
# 查看版本信息 
docker-compose -version

1.2 卸载Docker Compose

# 二进制包方式安装的,删除二进制文件即可
rm /usr/local/bin/docker-compose

2. 环境部署

我们项目的环境组件包括:MySQL、Nginx、Redis、MongoDB、RabbitMQ、Seata、Zipkin、Elastic Stack以及各个业务微服务。使用docker-compose来部署环境组件和直接使用docker启动镜像容器类似,这里就不一一叙述,直接看docker-compose.yml是如何配置的即可:

以下./表示的路径是docker-compose.yml所在的路径。

微服务工程的jar和Dockerfile可关注公众号,并回复:mini-mall基于docker-compose部署获取。

version: '3'

services:
  mysql:
    container_name: dc-mysql
    image: mysql:5.6
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: Anbang713
    volumes:
      - "./env/mysql/conf:/etc/mysql/conf.d"
      - "./env/mysql/data:/var/lib/mysql"
      - "./env/mysql/logs:/logs"
      - "./env/mysql/init:/docker-entrypoint-initdb.d/"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  nginx:
    container_name: dc-nginx
    image: nginx
    ports:
      - "80:80"
    volumes:
      - "./env/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
      - "./env/nginx/logs:/var/log/nginx"
      - "./env/nginx/html:/usr/share/nginx/html"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  redis:
    container_name: dc-redis
    image: redis:5.0
    ports:
      - "6379:6379"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  mongodb:
    container_name: dc-mongodb
    image: mongo
    ports:
      - "27017:27017"
    volumes:
      - "./env/mongodb/db:/data/db"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  rabbitmq:
    container_name: dc-rabbitmq
    image: rabbitmq:management
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      RABBITMQ_DEFAULT_USER: root
      RABBITMQ_DEFAULT_PASS: Anbang713
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  zipkin:
    container_name: dc-zipkin
    image: openzipkin/zipkin
    ports:
      - "9411:9411"
    environment:
      STORAGE_TYPE: mysql
      MYSQL_DB: mall_zipkin
      MYSQL_USER: root
      MYSQL_PASS: Anbang713
      MYSQL_HOST: server.mall.com
      MYSQL_TCP_PORT: 3306
      RABBIT_ADDRESSES: server.mall.com:5672
      RABBIT_USER: root
      RABBIT_PASSWORD: Anbang713
      RABBIT_QUEUE: zipkin
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  elasticsearch:
    container_name: dc-elasticsearch
    image: elasticsearch:7.6.0
    ports: 
      - "9200:9200"
      - "9300:9300"
    environment:
      discovery.type: single-node
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  kibana:
    container_name: dc-kibana
    image: kibana:7.6.0
    ports:
      - "5601:5601"
    volumes:
      - "./env/elk/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  filebeat:
    container_name: dc-filebeat
    image: store/elastic/filebeat:7.6.0
    ports:
      - "5044:5044"
    volumes:
      - "./env/elk/filebeat/mini-mall.yml:/usr/share/filebeat/filebeat.yml:ro"
      - "/var/lib/docker/containers:/var/lib/docker/containers:ro"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  registry-server:
    container_name: dc-mall-registry-server
    build: ./servers/registry-server
    ports:
      - "9010:9010"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  seata:
    container_name: dc-seata-server
    image: seataio/seata-server
    ports:
      - "8091:8091"
    depends_on:
      - registry-server
    environment:
      SEATA_CONFIG_NAME: file:/root/seata-config/registry
    volumes:
      - "./env/seata/conf:/root/seata-config"
      - "./env/seata/logs:/root/logs"
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  config-server:
    container_name: dc-mall-config-server
    build: ./servers/config-server
    ports:
      - "9045:9045"
    depends_on:
      - registry-server
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  gateway-server:
    container_name: dc-mall-gateway-server
    build: ./servers/gateway-server
    ports:
      - "9015:9015"
    depends_on:
      - registry-server
      - config-server
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  basis-provider:
    container_name: dc-mall-basis-provider
    build: ./servers/basis
    ports:
      - "9020:9020"
    depends_on:
      - registry-server
      - config-server
      - seata
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  invest-provider:
    container_name: dc-mall-invest-provider
    build: ./servers/invest
    ports:
      - "9025:9025"
    depends_on:
      - registry-server
      - config-server
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  sales-provider:
    container_name: dc-mall-sales-provider
    build: ./servers/sales
    ports:
      - "9030:9030"
    depends_on:
      - registry-server
      - config-server
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  account-provider:
    container_name: dc-mall-account-provider
    build: ./servers/account
    ports:
      - "9035:9035"
    depends_on:
      - registry-server
      - config-server
      - seata
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"
  product-provider:
    container_name: dc-mall-product-provider
    build: ./servers/product
    ports:
      - "9040:9040"
    depends_on:
      - registry-server
      - config-server
      - seata
    extra_hosts:
      - "manage.mall.com:192.168.1.17"
      - "api.mall.com:192.168.1.17"
      - "server.mall.com:192.168.1.17"

3. Docker图形化

Docker图形页面管理工具常用的有三种:DockerUI 、Portainer 、Shipyard 。DockerUI 是 Portainer 的前身,这三个工具通过docker api来获取管理的资源信息。平时我们常常对着shell对着这些命令行客户端,审美会很疲劳,如果有漂亮的图形化界面可以直观查看docker资源信息,也是非常方便的。其中这三种图形化管理工具以Portainer最为受欢迎,今天我们要使用的也是Portainer。

3.1 拉取镜像

docker pull portainer/portainer

3.2 创建并启动容器

docker run -d --name portainerUI -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

3.3 测试

容器启动成功之后,通过浏览器访问http://ip:9000即可。首次访问会让你创建一个初始化的管理员用户,用户名默认为admin,密码至少8位。我们搭建的是单机版,直接选择Local,点击连接即可。

MiniMall:Linux环境下的部署(基于Docker Compose)_第3张图片

连接成功之后,展示首页:

MiniMall:Linux环境下的部署(基于Docker Compose)_第4张图片

容器页面:

MiniMall:Linux环境下的部署(基于Docker Compose)_第5张图片

——End——
更多精彩分享,可扫码关注微信公众号哦。

在这里插入图片描述

你可能感兴趣的:(项目实战/Mini-Mall)