At the beginning of this article, we will publish a series of articles to show you a go-zero microservice example in detail. The whole series is divided into ten articles. The directory structure is as follows:
- environment construction (this article)
- service split
- User service
- product service
- Order service
- payment service
- RPC Service Auth Authentication
- service monitoring
- link tracking
- Distributed transaction
I hope that through this series, you can quickly develop a mall system using go-zero in the Docker environment on the local machine, so that you can quickly get started with microservices.
Full sample code: https://github.com/nivin-studio/go-zero-mall
1 Environmental requirements
Golang
1.15+Etcd
Redis
Mysql
Prometheus
Grafana
Jaeger
DTM
2 Docker
local development environment to build
In order to facilitate development and debugging, we use Docker
to build a local development environment. Windows
and macOS
systems can be downloaded and installed using Docker Desktop
. For specific download and installation methods, you can search for relevant tutorials by yourself.
Here we use Docker Compose
to orchestrate and manage our containers and create the following directories:
gonivinck
├── dtm # DTM 分布式事务管理器
│ ├── config.yml # DTM 配置文件
│ └── Dockerfile
├── etcd # Etcd 服务注册发现
│ └── Dockerfile
├── golang # Golang 运行环境
│ └── Dockerfile
├── grafana # Grafana 可视化数据监控
│ └── Dockerfile
├── jaeger # Jaeger 链路追踪
│ └── Dockerfile
├── mysql # Mysql 服务
│ └── Dockerfile
├── mysql-manage # Mysql 可视化管理
│ └── Dockerfile
├── prometheus # Prometheus 服务监控
│ ├── Dockerfile
│ └── prometheus.yml # Prometheus 配置文件
├── redis # Redis 服务
│ └── Dockerfile
├── redis-manage # Redis 可视化管理
│ └── Dockerfile
├── .env # env 配置
└── docker-compose.yml
2.1 Write Dockerfile
In the microservice of go-zero
, grpc
is used for communication between services, and the writing of grpc
requires the use protoc
and the plug-in protoc-gen-go
translated into go
language rpc stub
code.
In order to improve development efficiency, reduce code error rate, and shorten the workload of business development, go-zero
also provides goctl
code generation tools.
Therefore, we need to install protoc
, protoc-gen-go
, goctl
, into the golang
container in advance for subsequent use.
So the Dockerfile
code for the golang
container is as follows:
FROM golang:1.17
LABEL maintainer="Ving <ving@nivin.cn>"
ENV GOPROXY https://goproxy.cn,direct
# 安装必要的软件包和依赖包
USER root
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \
sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \
sed -i 's/security-cdn.debian.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apt/sources.list && \
apt-get update && \
apt-get upgrade -y && \
apt-get install -y --no-install-recommends \
curl \
zip \
unzip \
git \
vim
# 安装 goctl
USER root
RUN GOPROXY=https://goproxy.cn/,direct go install github.com/tal-tech/go-zero/tools/goctl@cli
# 安装 protoc
USER root
RUN curl -L -o /tmp/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.19.1/protoc-3.19.1-linux-x86_64.zip && \
unzip -d /tmp/protoc /tmp/protoc.zip && \
mv /tmp/protoc/bin/protoc $GOPATH/bin
# 安装 protoc-gen-go
USER root
RUN go get -u github.com/golang/protobuf/protoc-gen-go@v1.4.0
# $GOPATH/bin添加到环境变量中
ENV PATH $GOPATH/bin:$PATH
# 清理垃圾
USER root
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
rm /var/log/lastlog /var/log/faillog
# 设置工作目录
WORKDIR /usr/src/code
EXPOSE 8000
EXPOSE 8001
EXPOSE 8002
EXPOSE 8003
EXPOSE 9000
EXPOSE 9001
EXPOSE 9002
EXPOSE 9003
Other service containers Dockerfile
do not need special treatment, as long as they are based on existing images.
Serve | mirror based |
---|---|
DTM | yedf/dtm |
Etcd | bitnami/etcd |
Mysql | mysql:5.7 |
Redis | redis:5.0 |
Mysql Manage | phpmyadmin/phpmyadmin |
Redis Manage | erikdubbelboer/phpredisadmin |
Prometheus | bitnami/prometheus |
Grafana | grafana/grafana |
Jaeger | jaegertracing/all-in-one:1.28 |
2.2 Write .env
configuration file
# 设置时区
TZ=Asia/Shanghai
# 设置网络模式
NETWORKS_DRIVER=bridge
# PATHS ##########################################
# 宿主机上代码存放的目录路径
CODE_PATH_HOST=./code
# 宿主机上Mysql Reids数据存放的目录路径
DATA_PATH_HOST=./data
# MYSQL ##########################################
# Mysql 服务映射宿主机端口号,可在宿主机127.0.0.1:3306访问
MYSQL_PORT=3306
MYSQL_USERNAME=admin
MYSQL_PASSWORD=123456
MYSQL_ROOT_PASSWORD=123456
# Mysql 可视化管理用户名称,同 MYSQL_USERNAME
MYSQL_MANAGE_USERNAME=admin
# Mysql 可视化管理用户密码,同 MYSQL_PASSWORD
MYSQL_MANAGE_PASSWORD=123456
# Mysql 可视化管理ROOT用户密码,同 MYSQL_ROOT_PASSWORD
MYSQL_MANAGE_ROOT_PASSWORD=123456
# Mysql 服务地址
MYSQL_MANAGE_CONNECT_HOST=mysql
# Mysql 服务端口号
MYSQL_MANAGE_CONNECT_PORT=3306
# Mysql 可视化管理映射宿主机端口号,可在宿主机127.0.0.1:1000访问
MYSQL_MANAGE_PORT=1000
# REDIS ##########################################
# Redis 服务映射宿主机端口号,可在宿主机127.0.0.1:6379访问
REDIS_PORT=6379
# Redis 可视化管理用户名称
REDIS_MANAGE_USERNAME=admin
# Redis 可视化管理用户密码
REDIS_MANAGE_PASSWORD=123456
# Redis 服务地址
REDIS_MANAGE_CONNECT_HOST=redis
# Redis 服务端口号
REDIS_MANAGE_CONNECT_PORT=6379
# Redis 可视化管理映射宿主机端口号,可在宿主机127.0.0.1:2000访问
REDIS_MANAGE_PORT=2000
# ETCD ###########################################
# Etcd 服务映射宿主机端口号,可在宿主机127.0.0.1:2379访问
ETCD_PORT=2379
# PROMETHEUS #####################################
# Prometheus 服务映射宿主机端口号,可在宿主机127.0.0.1:3000访问
PROMETHEUS_PORT=3000
# GRAFANA ########################################
# Grafana 服务映射宿主机端口号,可在宿主机127.0.0.1:4000访问
GRAFANA_PORT=4000
# JAEGER #########################################
# Jaeger 服务映射宿主机端口号,可在宿主机127.0.0.1:5000访问
JAEGER_PORT=5000
# DTM #########################################
# DTM HTTP 协议端口号
DTM_HTTP_PORT=36789
# DTM gRPC 协议端口号
DTM_GRPC_PORT=36790
2.3 Write docker-compose.yml
configuration file
version: '3.5'
# 网络配置
networks:
backend:
driver: ${NETWORKS_DRIVER}
# 服务容器配置
services:
golang: # 自定义容器名称
build:
context: ./golang # 指定构建使用的 Dockerfile 文件
environment: # 设置环境变量
- TZ=${TZ}
volumes: # 设置挂载目录
- ${CODE_PATH_HOST}:/usr/src/code # 引用 .env 配置中 CODE_PATH_HOST 变量,将宿主机上代码存放的目录挂载到容器中 /usr/src/code 目录
ports: # 设置端口映射
- "8000:8000"
- "8001:8001"
- "8002:8002"
- "8003:8003"
- "9000:9000"
- "9001:9001"
- "9002:9002"
- "9003:9003"
stdin_open: true # 打开标准输入,可以接受外部输入
tty: true
networks:
- backend
restart: always # 指定容器退出后的重启策略为始终重启
etcd: # 自定义容器名称
build:
context: ./etcd # 指定构建使用的 Dockerfile 文件
environment:
- TZ=${TZ}
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
ports: # 设置端口映射
- "${ETCD_PORT}:2379"
networks:
- backend
restart: always
mysql:
build:
context: ./mysql
environment:
- TZ=${TZ}
- MYSQL_USER=${MYSQL_USERNAME} # 设置 Mysql 用户名称
- MYSQL_PASSWORD=${MYSQL_PASSWORD} # 设置 Mysql 用户密码
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} # 设置 Mysql root 用户密码
volumes:
- ${DATA_PATH_HOST}/mysql:/var/lib/mysql # 引用 .env 配置中 DATA_PATH_HOST 变量,将宿主机上存放 Mysql 数据的目录挂载到容器中 /var/lib/mysql 目录
ports:
- "${MYSQL_PORT}:3306" # 设置容器3306端口映射指定宿主机端口
networks:
- backend
restart: always
redis:
build:
context: ./redis
environment:
- TZ=${TZ}
volumes:
- ${DATA_PATH_HOST}/redis:/data # 引用 .env 配置中 DATA_PATH_HOST 变量,将宿主机上存放 Redis 数据的目录挂载到容器中 /data 目录
ports:
- "${REDIS_PORT}:6379" # 设置容器6379端口映射指定宿主机端口
networks:
- backend
restart: always
mysql-manage:
build:
context: ./mysql-manage
environment:
- TZ=${TZ}
- PMA_ARBITRARY=1
- MYSQL_USER=${MYSQL_MANAGE_USERNAME} # 设置连接的 Mysql 服务用户名称
- MYSQL_PASSWORD=${MYSQL_MANAGE_PASSWORD} # 设置连接的 Mysql 服务用户密码
- MYSQL_ROOT_PASSWORD=${MYSQL_MANAGE_ROOT_PASSWORD} # 设置连接的 Mysql 服务 root 用户密码
- PMA_HOST=${MYSQL_MANAGE_CONNECT_HOST} # 设置连接的 Mysql 服务 host,可以是 Mysql 服务容器的名称,也可以是 Mysql 服务容器的 ip 地址
- PMA_PORT=${MYSQL_MANAGE_CONNECT_PORT} # 设置连接的 Mysql 服务端口号
ports:
- "${MYSQL_MANAGE_PORT}:80" # 设置容器80端口映射指定宿主机端口,用于宿主机访问可视化web
depends_on: # 依赖容器
- mysql # 在 Mysql 服务容器启动后启动
networks:
- backend
restart: always
redis-manage:
build:
context: ./redis-manage
environment:
- TZ=${TZ}
- ADMIN_USER=${REDIS_MANAGE_USERNAME} # 设置 Redis 可视化管理的用户名称
- ADMIN_PASS=${REDIS_MANAGE_PASSWORD} # 设置 Redis 可视化管理的用户密码
- REDIS_1_HOST=${REDIS_MANAGE_CONNECT_HOST} # 设置连接的 Redis 服务 host,可以是 Redis 服务容器的名称,也可以是 Redis 服务容器的 ip 地址
- REDIS_1_PORT=${REDIS_MANAGE_CONNECT_PORT} # 设置连接的 Redis 服务端口号
ports:
- "${REDIS_MANAGE_PORT}:80" # 设置容器80端口映射指定宿主机端口,用于宿主机访问可视化web
depends_on: # 依赖容器
- redis # 在 Redis 服务容器启动后启动
networks:
- backend
restart: always
prometheus:
build:
context: ./prometheus
environment:
- TZ=${TZ}
volumes:
- ./prometheus/prometheus.yml:/opt/bitnami/prometheus/conf/prometheus.yml # 将 prometheus 配置文件挂载到容器里
ports:
- "${PROMETHEUS_PORT}:9090" # 设置容器9090端口映射指定宿主机端口,用于宿主机访问可视化web
networks:
- backend
restart: always
grafana:
build:
context: ./grafana
environment:
- TZ=${TZ}
ports:
- "${GRAFANA_PORT}:3000" # 设置容器3000端口映射指定宿主机端口,用于宿主机访问可视化web
networks:
- backend
restart: always
jaeger:
build:
context: ./jaeger
environment:
- TZ=${TZ}
ports:
- "${JAEGER_PORT}:16686" # 设置容器16686端口映射指定宿主机端口,用于宿主机访问可视化web
networks:
- backend
restart: always
dtm:
build:
context: ./dtm
environment:
- TZ=${TZ}
entrypoint:
- "/app/dtm/dtm"
- "-c=/app/dtm/configs/config.yaml"
volumes:
- ./dtm/config.yml:/app/dtm/configs/config.yaml # 将 dtm 配置文件挂载到容器里
ports:
- "${DTM_HTTP_PORT}:36789"
- "${DTM_GRPC_PORT}:36790"
networks:
- backend
restart: always
2.4 Build and run
- Use the
docker-compose
command to build and start our service container, execute the following command in the root directory:
$ docker-compose up -d
- container under construction
- In the
Windows
system container build appears as shown below, please selectShare it
This will allow the file directory ofWindows
to be mounted into the container directory. The container is up and running
2.5 Container Description
container name | exposed port | host address | instruction |
---|---|---|---|
golang | 8000:8000 8001:8001 8002:8002 8003:8003 9000:9000 9001:9001 9002:9002 9003:9003 | golang | In the production environment, microservices are generally deployed in clusters, which may be one server per microservice, or one container per microservice. In order to facilitate development and debugging, we will start all microservices in the golang container, and assign them different port numbers to listen to to distinguish them. 80: The port number starting with api service 90: The port number starting with rpc service |
dtm | 36789:36789 36790:36790 | dtm | dtm protocol and grpc protocol service port number of http for client interaction. this project, we only access and use it between the internal containers of Docker , so we can also not expose the port number to the host. |
etcd | 2379:2379 | etcd | Etcd http api Service port number for client interaction. this project, we only access and use it between the internal containers of Docker , so it is not necessary to expose the port number to the host. |
mysql | 3306:3306 | mysql | Mysql service default port number, the host can connect to the database through 127.0.0.1:3306 |
redis | 6379:6379 | redis | Redis Service default port number, the host can connect to the database through 127.0.0.1:6379 |
mysql-manage | 1000:80 | mysql-manage | phpMyAdmin web service port number, which can be accessed on the host machine 127.0.0.1:1000 |
redis-manage | 2000:80 | redis-manage | phpRedisAdmin web service port number, which can be accessed on the host machine 127.0.0.1:2000 |
prometheus | 3000:9090 | prometheus | Prometheus web service port number, which can be accessed on the host 127.0.0.1:3000 |
grafana | 4000:3000 | grafana | Grafana web service port number, which can be accessed on the host 127.0.0.1:4000 |
jaeger | 5000:16686 | jaeger | Jaeger web service port number, which can be accessed on the host machine 127.0.0.1:5000 |
2.6 Access Verification
Mysql
Access VerificationRedis
Access VerificationPrometheus
Access VerificationGrafana
Access VerificationJaeger
Access Verification
project address
https://github.com/zeromicro/go-zero
Welcome to go-zero
and support us with star !
WeChat exchange group
Follow the official account of " Practice " and click exchange group to get the QR code of the community group.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。