共计 4843 个字符,预计需要花费 13 分钟才能阅读完成。
0、索引
go-zero docker-compose 搭建课件服务(一):编写服务 api 和 proto
go-zero docker-compose 搭建课件服务(二):编写 courseware rpc 服务
go-zero docker-compose 搭建课件服务(三):编写 courseware api 服务
go-zero docker-compose 搭建课件服务(四):生成 Dockerfile 并在 docker-compose 中启动
go-zero docker-compose 搭建课件服务(五):欠缺 user 服务
go-zero docker-compose 搭建课件服务(六):欠缺 jwt 鉴权和返回构造
go-zero docker-compose 搭建课件服务(七):prometheus+grafana 服务监控
0.1 源码地址
https://github.com/liuyuede123/go-zero-courseware
1、目前的目录文件构造
目前只是创立好了 courseware 服务,这一章节我咱们先在 docker-compose 跑通课件服务
.
├── courseware
│ ├── api
│ │ ├── courseware.api
│ │ ├── courseware.go
│ │ ├── etc
│ │ │ └── courseware.yaml
│ │ └── internal
│ │ ├── config
│ │ │ └── config.go
│ │ ├── handler
│ │ │ ├── coursewareaddhandler.go
│ │ │ ├── coursewaredeletehandler.go
│ │ │ ├── coursewaregethandler.go
│ │ │ ├── coursewareupdatehandler.go
│ │ │ └── routes.go
│ │ ├── logic
│ │ │ ├── coursewareaddlogic.go
│ │ │ ├── coursewaredeletelogic.go
│ │ │ ├── coursewaregetlogic.go
│ │ │ └── coursewareupdatelogic.go
│ │ ├── svc
│ │ │ └── servicecontext.go
│ │ └── types
│ │ └── types.go
│ ├── go.mod
│ ├── go.sum
│ └── rpc
│ ├── courseware
│ │ ├── courseware.pb.go
│ │ └── courseware_grpc.pb.go
│ ├── courseware.go
│ ├── courseware.proto
│ ├── coursewareclient
│ │ └── courseware.go
│ ├── etc
│ │ └── courseware.yaml
│ ├── internal
│ │ ├── config
│ │ │ └── config.go
│ │ ├── logic
│ │ │ ├── addlogic.go
│ │ │ ├── deletelogic.go
│ │ │ ├── getlogic.go
│ │ │ └── updatelogic.go
│ │ ├── server
│ │ │ └── coursewareserver.go
│ │ └── svc
│ │ └── servicecontext.go
│ └── model
│ ├── courseware.sql
│ ├── coursewaremodel.go
│ ├── coursewaremodel_gen.go
│ └── vars.go
└── user
├── api
│ └── user.api
└── rpc
└── user.proto
2、我的项目根目录下创立依赖的服务
创立 etcd Dockerfile
mkdir etcd
vim etcd/Dockerfile
Dockerfile 中的内容,保留并退出
FROM bitnami/etcd:latest
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
创立 etcd-manage Dockerfile
mkdir etcd-manage
vim etcd-manage/Dockerfile
Dockerfile 中的内容,保留并退出
FROM evildecay/etcdkeeper
LABEL maintainer="liuyuede123 <liufutianoppo@163.com>"
3、courseware api 和 rpc 生成 Dockerfile
到 courseware/api 目录下生成 Dockerfile
cd courseware/api
goctl docker -go courseware.go
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update --no-cache && apk add --no-cache tzdata
WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY api/etc /app/etc
RUN go build -ldflags="-s -w" -o /app/courseware api/courseware.go
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/courseware /app/courseware
COPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
到 courseware/rpc 目录下生成 Dockerfile
cd ../rpc
goctl docker -go courseware.go
FROM golang:alpine AS builder
LABEL stage=gobuilder
ENV CGO_ENABLED 0
ENV GOPROXY https://goproxy.cn,direct
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update --no-cache && apk add --no-cache tzdata
WORKDIR /build
ADD go.mod .
ADD go.sum .
RUN go mod download
COPY . .
COPY rpc/etc /app/etc
RUN go build -ldflags="-s -w" -o /app/courseware rpc/courseware.go
FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo/Asia/Shanghai /usr/share/zoneinfo/Asia/Shanghai
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/courseware /app/courseware
COPY --from=builder /app/etc /app/etc
CMD ["./courseware", "-f", "etc/courseware.yaml"]
4、创立 docker-compose.yml 文件
到我的项目根目录
touch docker-compose.yml
version: '3.5'
# 网络配置
networks:
backend:
driver: bridge
# 服务容器配置
services:
etcd: # 自定义容器名称
build:
context: etcd # 指定构建应用的 Dockerfile 文件
environment:
- TZ=Asia/Shanghai
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
ports: # 设置端口映射
- "2379:2379"
networks:
- backend
restart: always
etcd-manage:
build:
context: etcd-manage
environment:
- TZ=Asia/Shanghai
ports:
- "7000:8080" # 设置容器 8080 端口映射指定宿主机端口,用于宿主机拜访可视化 web
depends_on: # 依赖容器
- etcd # 在 etcd 服务容器启动后启动
networks:
- backend
restart: always
courseware-rpc: # 自定义容器名称
build:
context: courseware # 指定构建应用的 Dockerfile 文件
dockerfile: rpc/Dockerfile
environment: # 设置环境变量
- TZ=Asia/Shanghai
privileged: true
ports: # 设置端口映射
- "9400:9400" # 课件服务 rpc 端口
stdin_open: true # 关上规范输出,能够承受内部输出
tty: true
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
courseware-api: # 自定义容器名称
build:
context: courseware # 指定构建应用的 Dockerfile 文件
dockerfile: api/Dockerfile
environment: # 设置环境变量
- TZ=Asia/Shanghai
privileged: true
ports: # 设置端口映射
- "8400:8400" # 课件服务 api 端口
stdin_open: true # 关上规范输出,能够承受内部输出
tty: true
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
而后我的项目根目录执行:
docker-compose up -d
Creating go-zero-courseware_courseware-rpc_1 ... done
Creating go-zero-courseware_etcd_1 ... done
Creating go-zero-courseware_courseware-api_1 ... done
Creating go-zero-courseware_etcd-manage_1 ... done
这时候课件服务就跑起来了。
而后测试下接口是否可用:
http://localhost:8400/api/courseware/get
{"id": 1}
http://localhost:8400/api/courseware/add
{
"name": "多媒体课件 11",
"code": "CH500",
"type": 4
}
http://localhost:8400/api/courseware/update
{
"id" : 1,
"name": "多媒体课件 1133",
"code": "CH500",
"type": 4
}
http://localhost:8400/api/courseware/delete
{"id": 1
正文完