Go-Zero 官方短鏈專案教程:快速構建高併發微服務
關於 go-zero,大家可以看文件。為少認為它是中國目前最好用的 golang 微服務框架。
完整的 Go-Zero ShortUrl DevOps Demo
準備工作
我這裡直接在 K8S 開發叢集中部署相關例項。
生產求穩,建議大家還是買雲資料庫服務。
部署 Mysql、Redis、Etcd。
部署 Drone、Drone-Runner-Kube
開始探索
準備 DevOps 部署相關配置
Dockerfile.alpine.base
FROM alpine:3.12
RUN addgroup -S app \
&& adduser -S -g app app \
&& apk --no-cache add \
ca-certificates curl netcat-openbsd
Dockerfile.base
FROM golang:1.15-alpine
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
RUN mkdir -p /shorturl/
WORKDIR /shorturl
COPY go.mod go.mod
RUN go mod download
Dockerfile.prod.rpc
### shorturl:base
FROM hub.your-domain.com/library/shorturl:base as builder
WORKDIR /shorturl-rpc
COPY . .
RUN CGO_ENABLED=0 go build -a -o bin/shorturl-rpc rpc/transform/*.go
### shorturl-alpine:base
FROM hub.your-domain.com/library/shorturl-alpine:base
LABEL maintainer="為少"
WORKDIR /home/app
COPY --from=builder /shorturl-rpc/bin/shorturl-rpc .
COPY ./rpc/transform/etc ./rpc/transform/etc
RUN chown -R app:app ./
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
USER app
CMD ["./shorturl-rpc", "-f", "rpc/transform/etc/transform.yaml"]
Dockerfile.prod.api
### shorturl:base
FROM hub.your-domain.com/library/shorturl:base as builder
WORKDIR /shorturl-api
COPY . .
RUN CGO_ENABLED=0 go build -a -o bin/shorturl-api api/*.go
### shorturl-alpine:base
FROM hub.your-domain.com/library/shorturl-alpine:base
LABEL maintainer="為少"
WORKDIR /home/app
COPY --from=builder /shorturl-api/bin/shorturl-api .
COPY ./api/etc ./api/etc
RUN chown -R app:app ./
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
USER app
CMD ["./shorturl-api", "-f", "api/etc/shorturl-api.yaml"]
shorturl-api-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: shorturl-api
data:
shorturl-api.yaml: |-
Name: shorturl-api
Host: 0.0.0.0
Port: 8888
Transform:
Etcd:
Hosts:
- your-ip:2379
Key: transform.rpc
shorturl-rpc-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: shorturl-transform-rpc
data:
transform.yaml: |-
Name: transform.rpc
Log:
Mode: console
ListenOn: 0.0.0.0:8081
Etcd:
Hosts:
- your-ip:2379
Key: transform.rpc
DataSource: root:123456@tcp(your-ip:3306)/shorturl?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
Table: shorturl
Cache:
- Host: your-ip:6379
.drone.yml
kind: pipeline
type: kubernetes
name: ShortUrl(transform.rpc)
steps:
- name: 更新 Charts(transform.rpc)
image: busybox
commands:
- echo $DRONE_COMMIT
- '[ -n "$DRONE_COMMIT" ] && (
sed -i "s/APP_VERSION/${DRONE_COMMIT}/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
sed -i "s/SHORTURL/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
sed -i "s/IMAGE/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/APICONFIGMAP//g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/RPCCONFIGMAP/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/CONTAINERPORT/8081/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/INGRESS/false/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/8080/8081/g" k8s-devops/helm-shorturl/shorturl/templates/NOTES.txt;
)'
- cat k8s-devops/helm-shorturl/shorturl/Chart.yaml
- cat k8s-devops/helm-shorturl/shorturl/values.yaml
- name: 構建 transform.rpc Image
image: plugins/docker
settings:
debug: true
dockerfile: Dockerfile.prod.rpc
repo: hub.your-domain.com/library/shorturl-transform-rpc
tags: ${DRONE_COMMIT}
registry: hub.your-domain.com
username:
from_secret: docker_user
password:
from_secret: docker_pass
- name: 上雲 HelmV3(transform.rpc) -> K8S
image: pelotech/drone-helm3
settings:
helm_command: upgrade
chart: ./k8s-devops/helm-shorturl/shorturl
release: shorturl-transform-rpc
namespace: shorturl
api_server:
from_secret: api_server
kubernetes_token:
from_secret: k8s_token
skip_tls_verify: true
trigger:
branch:
- main
---
kind: pipeline
type: kubernetes
name: ShortUrl(shorturl-api)
steps:
- name: 更新 Charts(shorturl-api)
image: busybox
commands:
- echo $DRONE_COMMIT
- '[ -n "$DRONE_COMMIT" ] && (
sed -i "s/APP_VERSION/${DRONE_COMMIT}/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
sed -i "s/SHORTURL/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
sed -i "s/IMAGE/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/APICONFIGMAP/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/RPCCONFIGMAP//g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/CONTAINERPORT/8888/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/INGRESS/true/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
sed -i "s/8080/8888/g" k8s-devops/helm-shorturl/shorturl/templates/NOTES.txt;
)'
- cat k8s-devops/helm-shorturl/shorturl/Chart.yaml
- cat k8s-devops/helm-shorturl/shorturl/values.yaml
- name: 構建 shorturl-api Image
image: plugins/docker
settings:
debug: true
dockerfile: Dockerfile.prod.api
repo: hub.your-domain.com/library/shorturl-api
tags: ${DRONE_COMMIT}
registry: hub.your-domain.com
username:
from_secret: docker_user
password:
from_secret: docker_pass
- name: 上雲 HelmV3(shorturl-api) -> K8S
image: pelotech/drone-helm3
settings:
helm_command: upgrade
chart: ./k8s-devops/helm-shorturl/shorturl
release: shorturl-api
namespace: shorturl
api_server:
from_secret: api_server
kubernetes_token:
from_secret: k8s_token
skip_tls_verify: true
trigger:
branch:
- main
驗證
我這裡已經部署好了一個開發測試的 Pod 例項,大家可以試用。
shorten api 呼叫
# curl -i "https://shorturl.your-domain.com/shorten?url=https://www.your-domain.com"
curl -i "https://shorturl.hacker-linner.com/shorten?url=https://www.hacker-linner.com"
expand api 呼叫
# curl -i "https://shorturl.your-domain.com/expand?shorten=6d11a1"
curl -i "https://shorturl.hacker-linner.com/expand?shorten=6d11a1"
利用工具檢視 Redis 與 Etcd 中的鍵值對
我是為少。
微信:uuhells123。
公眾號:黑客下午茶。
謝謝點贊支援???!