Go-zero official Short Chain project tutorial: Quickly build high and micro services

You can see the documentation for go-Zero. It is not considered the best golang microservice framework in China.

Full Go-Zero ShortUrl DevOps Demo

The preparatory work

I will deploy the relevant instance directly in the K8S development cluster here.

Production stability, we suggest that we still buy cloud database services.

Deploy Mysql, Redis, and Etcd.

Deploy Drone, drone-runner -Kube

Began to explore

Prepare the configuration for DevOps deployment

Dockerfile.alpine.base

FROM alpine:3.12

RUNaddgroup -S app \ && adduser -S -g app app \ && apk --no-cache add \ ca-certificates curl netcat-openbsd
Copy the code

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
Copy the code

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="Less"
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"]
Copy the code

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="Less"
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"]
Copy the code

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. The RPCCopy the code

shorturl-rpc-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: shorturl-transform-rpc
data:
  transform.yaml: | - Name: transform. RPC Log: Mode: the 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:6379Copy the code

.drone.yml

kind: pipeline
type: kubernetes
name: ShortUrl(transform.rpc)

steps:

  - name: update 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: build 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: On the cloud 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: update 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: build 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: On the cloud 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
Copy the code

validation

I’ve already deployed a development test Pod instance for you to try out.

Shorten the API call

# 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"
Copy the code

Expand the API call

# curl -i "https://shorturl.your-domain.com/expand?shorten=6d11a1"
curl -i "https://shorturl.hacker-linner.com/expand?shorten=6d11a1"
Copy the code

Use the tool to view key value pairs in Redis and Etcd

I am for less.

Wechat: uuhells123.

Public account: Hacker afternoon tea.

Thank you for your support 👍👍👍!