diff --git a/README.md b/README.md index fede3ec..bc0196f 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,16 @@ https://www.bilibili.com/video/BV11u4y1Y7GC/ #### 文档 https://pwmzlkcu3p.feishu.cn/docx/QZcKdB4VXoUCRDxGilfcTaw7n8e #### 视频 +https://www.bilibili.com/video/BV1u64y177rL +### 第十三课 +#### 文档 +https://pwmzlkcu3p.feishu.cn/docx/BDNUdhmP4oec6ix1P1ZcqF3rnIc +#### 视频 +https://www.bilibili.com/video/BV1bH4y1C7Uj + +### 第十四课 +#### 文档 +https://pwmzlkcu3p.feishu.cn/docx/Ydd4dG8OSobJ1rxJFgacOBKvnSg diff --git a/application/article/api/article.api b/application/article/api/article.api deleted file mode 100644 index 89b824e..0000000 --- a/application/article/api/article.api +++ /dev/null @@ -1,66 +0,0 @@ -syntax = "v1" - -type ( - UploadCoverResponse { - CoverUrl string `json:"cover_url"` - } - - PublishRequest { - Title string `json:"title"` - Content string `json:"content"` - Description string `json:"description"` - Cover string `json:"cover"` - } - - PublishResponse { - ArticleId int64 `json:"article_id"` - } - - ArticleDetailRequest { - ArticleId int64 `form:"article_id"` - } - - ArticleDetailResponse { - Title string `json:"title"` - Content string `json:"content"` - Description string `json:"description"` - Cover string `json:"cover"` - AuthorId string `json:"author_id"` - AuthorName string `json:"author_name"` - } - - ArticleListRequest { - AuthorId int64 `form:"author_id"` - Cursor int64 `form:"cursor"` - PageSize int64 `form:"page_size"` - SortType int32 `form:"sort_type"` - ArticleId int64 `form:"article_id"` - } - - ArticleInfo { - ArticleId int64 `json:"article_id"` - Title string `json:"title"` - Content string `json:"content"` - Description string `json:"description"` - Cover string `json:"cover"` - } - - ArticleListResponse { - Articles []ArticleInfo `json:"articles"` - } -) - -@server ( - prefix: /v1/article - jwt: Auth -) -service article-api { - @handler UploadCoverHandler - post /upload/cover returns (UploadCoverResponse) - @handler PublishHandler - post /publish (PublishRequest) returns (PublishResponse) - @handler ArticleDetailHandler - get /detail (ArticleDetailRequest) returns (ArticleDetailResponse) - @handler ArticleListHandler - get /list (ArticleListRequest) returns (ArticleListResponse) -} \ No newline at end of file diff --git a/application/article/api/article.go b/application/article/api/article.go index 308ca5b..9771fe0 100644 --- a/application/article/api/article.go +++ b/application/article/api/article.go @@ -7,7 +7,6 @@ import ( "beyond/application/article/api/internal/config" "beyond/application/article/api/internal/handler" "beyond/application/article/api/internal/svc" - "beyond/pkg/consul" "beyond/pkg/xcode" "github.com/zeromicro/go-zero/core/conf" @@ -32,12 +31,6 @@ func main() { // 自定义错误处理方法 httpx.SetErrorHandler(xcode.ErrHandler) - // 服务注册 - err := consul.Register(c.Consul, fmt.Sprintf("%s:%d", c.ServiceConf.Prometheus.Host, c.ServiceConf.Prometheus.Port)) - if err != nil { - fmt.Printf("register consul error: %v\n", err) - } - fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) server.Start() } diff --git a/application/article/api/etc/article-api-dev.yaml b/application/article/api/etc/article-api-dev.yaml new file mode 100644 index 0000000..bc28c03 --- /dev/null +++ b/application/article/api/etc/article-api-dev.yaml @@ -0,0 +1,24 @@ +Name: article-api +Host: 0.0.0.0 +Port: 80 +Auth: + AccessSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + AccessExpire: 604800 + RefreshSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + RefreshExpire: 2592000 + RefreshAfter: 604800 +Oss: + Endpoint: oss-cn-shanghai.aliyuncs.com + AccessKeyId: xxxxxxxxxxxxxxxxxxxx + AccessKeySecret: xxxxxxxxxxxxxxxxxxxx + BucketName: beyond-article +ArticleRPC: + Target: k8s://beyond/article-rpc:9090 + NonBlock: false +UserRPC: + Target: k8s://beyond/user-rpc:9090 + NonBlock: false +Prometheus: + Host: 0.0.0.0 + Port: 9101 + Path: /metrics \ No newline at end of file diff --git a/application/article/api/etc/article-api.yaml b/application/article/api/etc/article-api.yaml index 83dc488..5d6edfb 100644 --- a/application/article/api/etc/article-api.yaml +++ b/application/article/api/etc/article-api.yaml @@ -1,6 +1,6 @@ Name: article-api Host: 0.0.0.0 -Port: 9092 +Port: 80 Auth: AccessSecret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx AccessExpire: 604800 @@ -24,13 +24,6 @@ UserRPC: - 127.0.0.1:2379 Key: user.rpc NonBlock: true -Consul: - Host: 127.0.0.1:8500 - Key: article-api - Meta: - env: test - service_group: beyond - service_name: article-api Prometheus: Host: 0.0.0.0 Port: 9101 diff --git a/application/article/api/internal/config/config.go b/application/article/api/internal/config/config.go index 2958faa..864533f 100644 --- a/application/article/api/internal/config/config.go +++ b/application/article/api/internal/config/config.go @@ -1,8 +1,6 @@ package config import ( - "beyond/pkg/consul" - "github.com/zeromicro/go-zero/rest" "github.com/zeromicro/go-zero/zrpc" ) @@ -23,5 +21,4 @@ type Config struct { ConnectTimeout int64 `json:",optional"` ReadWriteTimeout int64 `json:",optional"` } - Consul consul.Conf } diff --git a/application/article/api/internal/svc/servicecontext.go b/application/article/api/internal/svc/servicecontext.go index 12c4b46..e4dc466 100644 --- a/application/article/api/internal/svc/servicecontext.go +++ b/application/article/api/internal/svc/servicecontext.go @@ -6,7 +6,6 @@ import ( "beyond/application/user/rpc/user" "github.com/aliyun/aliyun-oss-go-sdk/oss" - "github.com/zeromicro/go-zero/zrpc" ) const ( @@ -35,9 +34,9 @@ func NewServiceContext(c config.Config) *ServiceContext { } return &ServiceContext{ - Config: c, - OssClient: oc, - ArticleRPC: article.NewArticle(zrpc.MustNewClient(c.ArticleRPC)), - UserRPC: user.NewUser(zrpc.MustNewClient(c.UserRPC)), + Config: c, + OssClient: oc, + //ArticleRPC: article.NewArticle(zrpc.MustNewClient(c.ArticleRPC)), + //UserRPC: user.NewUser(zrpc.MustNewClient(c.UserRPC)), } } diff --git a/application/article/rpc/etc/article-rpc-dev.yaml b/application/article/rpc/etc/article-rpc-dev.yaml new file mode 100644 index 0000000..bf27435 --- /dev/null +++ b/application/article/rpc/etc/article-rpc-dev.yaml @@ -0,0 +1,22 @@ +Name: article-rpc +ListenOn: 0.0.0.0:9090 +DataSource: root:123456@tcp(10.111.196.140:3306)/beyond_article?parseTime=true&loc=Local +CacheRedis: + - Host: 10.103.107.10:6379 + Pass: + Type: node +BizRedis: + Host: 10.103.107.10:6379 + Pass: + Type: node +Consul: + Host: 127.0.0.1:8500 + Key: article-rpc + Meta: + env: test + service_group: beyond + service_name: article-rpc +Prometheus: + Host: 0.0.0.0 + Port: 9102 + Path: /metrics diff --git a/application/article/rpc/etc/article.yaml b/application/article/rpc/etc/article.yaml index df05f93..4710e05 100644 --- a/application/article/rpc/etc/article.yaml +++ b/application/article/rpc/etc/article.yaml @@ -1,5 +1,5 @@ Name: article.rpc -ListenOn: 0.0.0.0:8686 +ListenOn: 0.0.0.0:9090 Etcd: Hosts: - 127.0.0.1:2379 diff --git a/application/user/rpc/etc/user-rpc-dev.yaml b/application/user/rpc/etc/user-rpc-dev.yaml new file mode 100644 index 0000000..96263ed --- /dev/null +++ b/application/user/rpc/etc/user-rpc-dev.yaml @@ -0,0 +1,22 @@ +Name: user-rpc +ListenOn: 0.0.0.0:9090 +DataSource: root:123456@tcp(10.111.196.140:3306)/beyond_user?parseTime=true +CacheRedis: + - Host: 10.103.107.10:6379 + Pass: + Type: node +BizRedis: + Host: 10.103.107.10:6379 + Pass: + Type: node +Consul: + Host: 127.0.0.1:8500 + Key: user-rpc + Meta: + env: test + service_group: beyond + service_name: user-rpc +Prometheus: + Host: 0.0.0.0 + Port: 9103 + Path: /metrics \ No newline at end of file diff --git a/article-api.dockerfile b/article-api.dockerfile new file mode 100644 index 0000000..68f384f --- /dev/null +++ b/article-api.dockerfile @@ -0,0 +1,30 @@ +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 application/article/api/etc/article-api-dev.yaml /app/etc/article-api.yaml +RUN go build -ldflags="-s -w" -o /app/article-api application/article/api/article.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/article-api /app/article-api +COPY --from=builder /app/etc /app/etc + +CMD ["./article-api", "-f", "etc/article-api.yaml"] diff --git a/article-api.yaml b/article-api.yaml new file mode 100644 index 0000000..2a6995e --- /dev/null +++ b/article-api.yaml @@ -0,0 +1,61 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: article-api + namespace: beyond + labels: + app: article-api +spec: + replicas: 1 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: article-api + template: + metadata: + labels: + app: article-api + spec: + serviceAccountName: endpoints-reader + containers: + - name: article-api + image: article-api:v1.0.0 + ports: + - containerPort: 80 + readinessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 5 + periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 80 + initialDelaySeconds: 15 + periodSeconds: 20 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 200m + memory: 256Mi + volumeMounts: + - name: timezone + mountPath: /etc/localtime + volumes: + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai + +--- +apiVersion: v1 +kind: Service +metadata: + name: article-api-svc + namespace: beyond +spec: + ports: + - port: 80 + targetPort: 80 + selector: + app: article-api diff --git a/article-rpc.dockerfile b/article-rpc.dockerfile new file mode 100644 index 0000000..f7b8367 --- /dev/null +++ b/article-rpc.dockerfile @@ -0,0 +1,30 @@ +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 application/article/rpc/etc/article-rpc-dev.yaml /app/etc/article-rpc.yaml +RUN go build -ldflags="-s -w" -o /app/article-rpc application/article/rpc/article.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/article-rpc /app/article-rpc +COPY --from=builder /app/etc /app/etc + +CMD ["./article-rpc", "-f", "etc/article-rpc.yaml"] diff --git a/article-rpc.yaml b/article-rpc.yaml new file mode 100644 index 0000000..1876648 --- /dev/null +++ b/article-rpc.yaml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: article-rpc + namespace: beyond + labels: + app: article-rpc +spec: + replicas: 3 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: article-rpc + template: + metadata: + labels: + app: article-rpc + spec: + containers: + - name: article-rpc + image: article-rpc:v1.0.0 + ports: + - containerPort: 9090 + readinessProbe: + tcpSocket: + port: 9090 + initialDelaySeconds: 5 + periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 9090 + initialDelaySeconds: 15 + periodSeconds: 20 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 200m + memory: 256Mi + volumeMounts: + - name: timezone + mountPath: /etc/localtime + volumes: + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai \ No newline at end of file diff --git a/clusterrole.yaml b/clusterrole.yaml new file mode 100644 index 0000000..d95e737 --- /dev/null +++ b/clusterrole.yaml @@ -0,0 +1,9 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: endpoints-reader +rules: + - apiGroups: [""] + resources: ["endpoints"] + verbs: ["get", "watch", "list"] + diff --git a/clusterrolebinding.yaml b/clusterrolebinding.yaml new file mode 100644 index 0000000..a950e1c --- /dev/null +++ b/clusterrolebinding.yaml @@ -0,0 +1,12 @@ +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: endpoints-reader +subjects: + - kind: ServiceAccount + name: endpoints-reader + namespace: beyond +roleRef: + kind: ClusterRole + name: endpoints-reader + apiGroup: rbac.authorization.k8s.io diff --git a/mysql.yaml b/mysql.yaml new file mode 100644 index 0000000..99a71b1 --- /dev/null +++ b/mysql.yaml @@ -0,0 +1,39 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mysql + namespace: beyond +spec: + replicas: 1 + selector: + matchLabels: + app: mysql + template: + metadata: + labels: + app: mysql + namespace: beyond + spec: + containers: + - name: mysql + image: mysql:5.7 + env: + - name: MYSQL_ROOT_PASSWORD + value: "123456" + ports: + - name: mysql + containerPort: 3306 + +--- +apiVersion: v1 +kind: Service +metadata: + name: mysql-service + namespace: beyond +spec: + selector: + app: mysql + ports: + - protocol: TCP + port: 3306 + targetPort: 3306 \ No newline at end of file diff --git a/redis.yaml b/redis.yaml new file mode 100644 index 0000000..f792205 --- /dev/null +++ b/redis.yaml @@ -0,0 +1,34 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: redis + namespace: beyond +spec: + replicas: 1 + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - name: redis + image: redis:latest + ports: + - containerPort: 6379 + +--- +apiVersion: v1 +kind: Service +metadata: + name: redis-service + namespace: beyond +spec: + selector: + app: redis + ports: + - protocol: TCP + port: 6379 + targetPort: 6379 \ No newline at end of file diff --git a/serviceaccount.yaml b/serviceaccount.yaml new file mode 100644 index 0000000..71b3d55 --- /dev/null +++ b/serviceaccount.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: endpoints-reader + namespace: beyond diff --git a/user-rpc.dockerfile b/user-rpc.dockerfile new file mode 100644 index 0000000..994af75 --- /dev/null +++ b/user-rpc.dockerfile @@ -0,0 +1,30 @@ +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 application/user/rpc/etc/user-rpc-dev.yaml /app/etc/user-rpc.yaml +RUN go build -ldflags="-s -w" -o /app/user-rpc application/user/rpc/user.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/user-rpc /app/user-rpc +COPY --from=builder /app/etc /app/etc + +CMD ["./user-rpc", "-f", "etc/user-rpc.yaml"] diff --git a/user-rpc.yaml b/user-rpc.yaml new file mode 100644 index 0000000..858813c --- /dev/null +++ b/user-rpc.yaml @@ -0,0 +1,47 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: user-rpc + namespace: beyond + labels: + app: user-rpc +spec: + replicas: 3 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: user-rpc + template: + metadata: + labels: + app: user-rpc + spec: + containers: + - name: user-rpc + image: user-rpc:v1.0.0 + ports: + - containerPort: 9090 + readinessProbe: + tcpSocket: + port: 9090 + initialDelaySeconds: 5 + periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 9090 + initialDelaySeconds: 15 + periodSeconds: 20 + resources: + requests: + cpu: 200m + memory: 256Mi + limits: + cpu: 200m + memory: 256Mi + volumeMounts: + - name: timezone + mountPath: /etc/localtime + volumes: + - name: timezone + hostPath: + path: /usr/share/zoneinfo/Asia/Shanghai \ No newline at end of file