The third generation microservice architecture: a practical case of blog microservice based on Go, supporting distributed transactions

Posted by srividya on Wed, 05 Jan 2022 13:01:54 +0100

This is a blog micro service Demo based on Golang, which can be deployed in kubernetes istio cluster with one click, and supports distributed transactions.

Project address: github.com/jxlwqq/blog-microservic... , welcome to Star and PR.

framework

Kiali Console

directory structure

Mainly follow Standard Go Project Layout Recommended directory hierarchy.

Dependencies used:

Introduction to Makefile

command explain
make init Install all kinds of protocol gen - *, wire and migrate
make protoc Based on * proto files to generate various*_ pb.go
make wire Based on wire Go file to generate wire_gen.go
make test test
make migrate-up Migrate database
make migrate-down Rollback Database
make blog-server Start blog service (local development environment)
make user-server Start user service (local development environment)
make post-server Start the post service (local development environment)
make comment-server Start the comment service (local development environment)
make auth-server Start auth service (local development environment)
make dtm-server DTM is an external dependency. To start the local service, please browse Official documents
make docker-build Build Docker image
make kube-deploy Deploy blog, user, post, comment, auth and dtm services in the cluster
make kube-delete Delete the above services in the cluster
make kube-redeploy Redeploy services in a cluster( ⚠️ Database service will not be redeployed)

Local environment construction

  • docker-desktop >= 4.3.2
  • kubernetes >= 1.22.4
  • go >= 1.17
  • istioctl >= 1.12.1
  • protobuf >= 3.19.1

Download and install Docker Desktop and start the built-in Kubernetes cluster.

# Install Go
brew install go
# Installing Protobuf
brew install protobuf
# Installing Istio
brew install istioctl
kubectl config use-context docker-desktop
istioctl install -y
kubectl label namespace default istio-injection=enabled

Users of ARM64 architecture install Istio, please check here For more information.

One click deployment to local cluster

Database initialization, project related configurations have been set in the yaml file in the deployments directory, and can be deployed directly with one click.

There are a lot of resources deployed. Please increase the CPU and Memory of Docker Desktop as much as possible to avoid that Pod cannot complete scheduling. See here.

make docker-build
make kube-deploy

To view Pod resources:

kubectl get pods -A

return:

NAMESPACE      NAME                                     READY   STATUS    RESTARTS          AGE
default        auth-server-678f9d4b4-lhkf8              2/2     Running   0                 101s
default        blog-server-6475d7bdd5-6t9jk             2/2     Running   0                 101s
default        comment-db-59665d87f8-plswq              2/2     Running   0                 101s
default        comment-server-857445775b-qkpm5          2/2     Running   3 (74s ago)       100s
default        dtm-db-cb8c45698-qms8q                   2/2     Running   0                 102s
default        dtm-server-c548d67cc-xcv2z               2/2     Running   0                 102s
default        post-db-867f5f85db-zz64g                 2/2     Running   0                 101s
default        post-server-5448dc5b67-mdhcc             2/2     Running   4 (54s ago)       101s
default        user-db-fcfd47bf8-dh9kp                  2/2     Running   0                 101s
default        user-server-78688485b8-zf5tc             2/2     Running   3 (76s ago)       101s
istio-system   grafana-6ccd56f4b6-tp84j                 1/1     Running   0                 100s
istio-system   istio-egressgateway-67dc4b96cd-2gm6s     1/1     Running   5                 13d
istio-system   istio-ingressgateway-68d897fbcd-thvt9    1/1     Running   5                 13d
istio-system   istiod-66dc55cd96-r6cnv                  1/1     Running   5                 13d
istio-system   jaeger-9dd685668-wdzsz                   1/1     Running   0                 100s
istio-system   kiali-79b86ff5bc-jh6v8                   1/1     Running   0                 100s
istio-system   prometheus-64fd8ccd65-4c5nc              2/2     Running   0                 99s
kube-system    coredns-558bd4d5db-5nlbz                 1/1     Running   5                 13d
kube-system    coredns-558bd4d5db-m46lb                 1/1     Running   5                 13d
kube-system    etcd-docker-desktop                      1/1     Running   13                13d
kube-system    kube-apiserver-docker-desktop            1/1     Running   13                13d
kube-system    kube-controller-manager-docker-desktop   1/1     Running   13                13d
kube-system    kube-proxy-mj6x8                         1/1     Running   5                 13d
kube-system    kube-scheduler-docker-desktop            1/1     Running   13                13d
kube-system    storage-provisioner                      1/1     Running   11                13d
kube-system    vpnkit-controller                        1/1     Running   177 (2m56s ago)   13d

View Service resources:

kubectl get services -A

return:

NAMESPACE      NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                      AGE
default        auth-server            ClusterIP      10.99.64.145     <none>        50054/TCP,8054/TCP,9054/TCP                                                  15m
default        blog-server            ClusterIP      10.96.207.114    <none>        50050/TCP,8050/TCP,9050/TCP                                                  15m
default        comment-db             ClusterIP      10.99.195.67     <none>        3306/TCP                                                                     15m
default        comment-server         ClusterIP      10.96.179.191    <none>        50053/TCP,9053/TCP                                                           15m
default        dtm-db                 ClusterIP      10.106.31.52     <none>        3306/TCP                                                                     15m
default        dtm-server             ClusterIP      10.99.170.244    <none>        36790/TCP,36789/TCP                                                          15m
default        kubernetes             ClusterIP      10.96.0.1        <none>        443/TCP                                                                      13d
default        post-db                ClusterIP      10.103.103.106   <none>        3306/TCP                                                                     15m
default        post-server            ClusterIP      10.96.232.141    <none>        50052/TCP,9052/TCP                                                           15m
default        user-db                ClusterIP      10.99.144.168    <none>        3306/TCP                                                                     15m
default        user-server            ClusterIP      10.100.66.36     <none>        50051/TCP,9051/TCP                                                           15m
istio-system   grafana                ClusterIP      10.105.48.42     <none>        3000/TCP                                                                     15m
istio-system   istio-egressgateway    ClusterIP      10.99.128.200    <none>        80/TCP,443/TCP                                                               13d
istio-system   istio-ingressgateway   LoadBalancer   10.101.116.168   localhost     15021:30898/TCP,80:32417/TCP,443:32343/TCP,31400:32210/TCP,15443:31544/TCP   13d
istio-system   istiod                 ClusterIP      10.108.250.181   <none>        15010/TCP,15012/TCP,443/TCP,15014/TCP                                        13d
istio-system   jaeger-collector       ClusterIP      10.103.107.169   <none>        14268/TCP,14250/TCP,9411/TCP                                                 15m
istio-system   kiali                  ClusterIP      10.111.157.82    <none>        20001/TCP,9090/TCP                                                           15m
istio-system   prometheus             ClusterIP      10.100.104.250   <none>        9090/TCP                                                                     15m
istio-system   tracing                ClusterIP      10.108.139.227   <none>        80/TCP,16685/TCP                                                             15m
istio-system   zipkin                 ClusterIP      10.101.235.179   <none>        9411/TCP                                                                     15m
kube-system    kube-dns               ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP                                                       13d

Access services

Recommended use BloomRPC perhaps Insomnia , import API / protobuf / blog After the proto file, the service address can be accessed by filling in the localhost:80 port, as shown in the following figure:

Registration:

Create article:

Topics: Go Kubernetes Prometheus grpc istio