golang 微服务部署 容器编排(go-zero) docker-compose容器编排centos 实战

go-zero微服务部署

docker安装docker-compose安装请自行百度。

1.服务

1.user-api	:对外暴露接口服务

2.user-rpc	:给user-api提供用户服务

user-api项目结构插图:

在这里插入图片描述

2.服务依赖

1.kafka
2.mysql
3.redis
4.etcd		服务发现
5.zookeeper
6.nginx 网关

3.部署流程

1.user-api 打包
dockerfile

FROM golang:1.18 AS build-env
COPY . /app
WORKDIR /app
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/user-api github.com/f1748x/WerewolfKill/api

FROM alpine:latest
WORKDIR /app
# RUN apk add -U tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
COPY --from=build-env  /app/user-api /app/user-api
COPY --from=build-env  /app/api/controller/config-test.yaml /app/api/controller/config.yaml
COPY --from=build-env  /app/api/etc/kuber-api-test.yaml /app/etc/kuber-api-test.yaml

CMD ["/app/user-api", "-f", "/app/etc/kuber-api-test.yaml"]

2.user-rpc 打包

FROM golang:1.18 AS build-env
COPY . /app
WORKDIR /app
# ADD go.mod .
# ADD go.sum .
RUN go env -w GOPROXY=https://goproxy.cn,direct
# WORKDIR /go/
# RUN pwd
# RUN ls
RUN go mod download

# RUN ls
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/user-rpc github.com/f1748x/WerewolfKill/usercenter

FROM alpine:latest
# RUN apk add -U tzdata
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
COPY --from=build-env  /app/user-rpc /app/user-rpc
COPY --from=build-env  /app/usercenter/etc/usercenter-test.yaml /app/etc/usercenter-test.yaml
# EXPOSE 10001
CMD ["/app/user-rpc", "-f", "/app/etc/usercenter-test.yaml"]

3.docker-compose编排容器
1.docker-compose-env.yaml
依赖服务

version: '3'

######## 项目依赖的环境,启动项目之前要先启动此环境 #######
services:
  # 服务发现
  
  etcd-server:
    image: bitnami/etcd:latest
    container_name: etcd-server
    environment:
      - TZ=Asia/Shanghai
      - ALLOW_NONE_AUTHENTICATION=yes
    ports: # 设置端口映射
      - "2379:2379"
    networks:
      - center_net
    restart: always
  #zookeeper是kafka的依赖
  zookeeper:
   image: wurstmeister/zookeeper
   container_name: zookeeper
   environment:
     # 时区上海
     TZ: Asia/Shanghai
   restart: always
   ports:
     - "2181:2181"
   networks:
     - center_net

 #消息队列
  kafka:
   image: wurstmeister/kafka
   container_name: kafka
   ports:
     - "9092:9092"
   environment:
     KAFKA_ADVERTISED_HOST_NAME: kafka
     KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
     TZ: Asia/Shanghai
   restart: always
   volumes:
     - /var/run/docker.sock:/var/run/docker.sock
   networks:
     - center_net
   depends_on:
     - zookeeper


  mysql:
#    image: mysql:8.0.24
    image: mysql/mysql-server:8.0.28
    container_name: mysql
    environment:
      # 时区上海
      TZ: Asia/Shanghai
      # root 密码
      MYSQL_ROOT_PASSWORD: xxxxxxxxxxx
    ports:
      - "33069:3306"
    volumes:
      # 数据挂载
      - ./data/mysql/data:/var/lib/mysql
      # 日志
    command:
      # 将mysql8.0默认密码策略 修改为 原先 策略 (mysql8.0对其默认策略做了更改 会导致密码无法匹配)
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    privileged: true
    restart: always
    networks:
      - center_net
  #redis容器
  redis:
   image: bitnami/redis
   container_name: redis
   ports:
     - "63799:6379"
   environment:
      # 时区上海
     TZ: Asia/Shanghai
   volumes:
      # 数据文件
     - ./data/redis/data:/data:rw
     - ./deploy/source/buster/sources.list:/etc/apt/sources.list
   command: "redis-server --requirepass G62m50oigInC30sf  --appendonly yes"
   privileged: true
   restart: always
   networks:
     - center_net
# 网络设置

networks:
  center_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.30.0.0/16


2.docker-compose 主要服务


version: '3.0'

services:
  #前端网关nginx-center (只代理center,admin-api不在这里做代理)
  #前端api + 业务rpc

  kuber-user:
#    image: lyumikael/gomodd:v1.0.0 # if you "macOs intel" or "linux amd"
    image: 405ee2b24845  # if you macOs m1
    container_name: kuber-user
    ports:
     - "10001:10001"
    environment:
      # 时区上海
      TZ: Asia/Shanghai
      GOPROXY: https://goproxy.cn,direct
    working_dir: /user-center
    volumes:
      - .:/user-center
    privileged: true
    restart: always
    networks:
      - center_net

  kuber-api:
    image: ea502ffe0c23
    container_name: kuber-api
    ports:
     - "10000:10000"
    environment:
      # 时区上海
      TZ: Asia/Shanghai
      GOPROXY: https://goproxy.cn,direct
    depends_on:
      - kuber-user
    networks:
      - center_net

networks:
  center_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.30.0.0/16

到服务区先执行1.docker-compose -f docker-compose-env.yaml up -d
再执行2.docker-compose -f docker-compose.yaml up -d
这个时候部署就成功了,注意docker网络要一致不然会出现容器无法通讯的问题;
如果你看了本篇文章没有成功请留言,技术支持哦。

  • 0
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页
评论

打赏作者

托马斯的冰淇淋

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值