go-zero docker-compose 搭建課件服務(五):完善user服務

slowquery發表於2022-08-29

0、索引

go-zero docker-compose 搭建課件服務(五):完善user服務

0.1原始碼地址

github.com/liuyuede123/go-zero-cou...

1、生成model

到專案根目錄下建立model目錄,並新建user.sql

mkdir user/rpc/model
touch user/rpc/model/user.sql
CREATE TABLE `user`
(
    `id`          bigint unsigned NOT NULL AUTO_INCREMENT,
    `login_name`  varchar(255) NOT NULL DEFAULT '' COMMENT '登入名',
    `username`        varchar(255) NOT NULL DEFAULT '' COMMENT '使用者姓名',
    `sex`      tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '使用者性別',
    `password`    varchar(255) NOT NULL DEFAULT '' COMMENT '使用者密碼',
    `is_delete`   tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除 0-未刪除 1-已刪除',
    `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `udx_login_name` (`login_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;

到model目錄下生成model

cd user/rpc/model
goctl model mysql ddl -src="./*.sql" -dir="./" -c

2、生成rpc檔案

到user/rpc目錄下生成rpc檔案

goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=.

3、增加mysql配置

user目錄下初始化module

go mod init
go mod tidy

user/rpc/etc/user.yaml中增加資料來源和快取配置

Name: user.rpc
ListenOn: 127.0.0.1:8300
Etcd:
  Hosts:
  - etcd:2379
  Key: user.rpc

# mysql資料來源
Mysql:
  DataSource: root:liufutian@tcp(192.168.0.110:3306)/go_zero_courseware?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai

# redis快取
CacheRedis:
  - Host: 192.168.0.110:6379
    Pass:

修改user/rpc/internal/config/config.go中配置

package config

import (
    "github.com/zeromicro/go-zero/core/stores/cache"
    "github.com/zeromicro/go-zero/zrpc"
)

type Config struct {
    zrpc.RpcServerConf

    Mysql struct {
        DataSource string
    }

    CacheRedis cache.CacheConf
}

修改courseware/rpc/internal/svc/servicecontext.go相關配置

package svc

import (
    "github.com/zeromicro/go-zero/core/stores/sqlx"
    "go-zero-courseware/user/rpc/internal/config"
    "go-zero-courseware/user/rpc/model"
)

type ServiceContext struct {
    Config config.Config

    UserModel model.UserModel
}

func NewServiceContext(c config.Config) *ServiceContext {
    conn := sqlx.NewMysql(c.Mysql.DataSource)
    return &ServiceContext{
        Config: c,
        UserModel: model.NewUserModel(conn, c.CacheRedis),
    }
}

4、新增使用者邏輯

先走通,後續會最佳化使用者邏輯

user/rpc/internal/logic/registerlogic.go增加註冊邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/internal/svc"
    "go-zero-courseware/user/rpc/model"
    "go-zero-courseware/user/rpc/user"
    "google.golang.org/grpc/status"

    "github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
    ctx    context.Context
    svcCtx *svc.ServiceContext
    logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
    return &RegisterLogic{
        ctx:    ctx,
        svcCtx: svcCtx,
        Logger: logx.WithContext(ctx),
    }
}

func (l *RegisterLogic) Register(in *user.RegisterRequest) (*user.RegisterResponse, error) {
    _, err := l.svcCtx.UserModel.FindOneByLoginName(l.ctx, in.LoginName)
    if err == nil {
        return nil, status.Error(5000, "登入名已存在")
    }

    if err != model.ErrNotFound {
        return nil, status.Error(500, err.Error())
    }
    newUser := model.User{
        LoginName: in.LoginName,
        Username:  in.Username,
        Sex:       in.Sex,
        Password:  in.Password,
    }
    _, err = l.svcCtx.UserModel.Insert(l.ctx, &newUser)
    if err != nil {
        return nil, status.Error(500, err.Error())
    }

    return &user.RegisterResponse{}, nil
}

user/rpc/internal/logic/loginlogic.go增加登入邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/model"
    "google.golang.org/grpc/status"

    "go-zero-courseware/user/rpc/internal/svc"
    "go-zero-courseware/user/rpc/user"

    "github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
    ctx    context.Context
    svcCtx *svc.ServiceContext
    logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
    return &LoginLogic{
        ctx:    ctx,
        svcCtx: svcCtx,
        Logger: logx.WithContext(ctx),
    }
}

func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
    userInfo, err := l.svcCtx.UserModel.FindOneByLoginName(l.ctx, in.LoginName)
    if err == model.ErrNotFound {
        return nil, status.Error(5000, "使用者不存在")
    }
    if err != nil {
        return nil, status.Error(500, err.Error())
    }

    if in.Password != userInfo.Password {
        return nil, status.Error(5000, "密碼錯誤")
    }

    return &user.LoginResponse{
        Id:    userInfo.Id,
        Token: "a.b.c",
    }, nil
}

增加使用者資訊邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/model"
    "google.golang.org/grpc/status"

    "go-zero-courseware/user/rpc/internal/svc"
    "go-zero-courseware/user/rpc/user"

    "github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
    ctx    context.Context
    svcCtx *svc.ServiceContext
    logx.Logger
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
    return &UserInfoLogic{
        ctx:    ctx,
        svcCtx: svcCtx,
        Logger: logx.WithContext(ctx),
    }
}

func (l *UserInfoLogic) UserInfo(in *user.UserInfoRequest) (*user.UserInfoResponse, error) {
    userInfo, err := l.svcCtx.UserModel.FindOne(l.ctx, in.Id)
    if err == model.ErrNotFound {
        return nil, status.Error(5000, "使用者不存在")
    }
    if err != nil {
        return nil, status.Error(500, err.Error())
    }

    return &user.UserInfoResponse{
        Id:        userInfo.Id,
        Username:  userInfo.Username,
        LoginName: userInfo.LoginName,
        Sex:       userInfo.Sex,
    }, nil
}

5、完善api程式碼

到user/api目錄下,生成api端程式碼

goctl api go -api user.api -dir . -style gozero

user/api/etc/user.yaml配置

Name: user
Host: 0.0.0.0
Port: 8300

UserRpc:
  Etcd:
    Hosts:
      - etcd:2379
    Key: user.rpc

user/api/internal/config/config.go配置

package config

import (
    "github.com/zeromicro/go-zero/rest"
    "github.com/zeromicro/go-zero/zrpc"
)

type Config struct {
    rest.RestConf

    UserRpc zrpc.RpcClientConf
}

user/api/internal/svc/servicecontext.go配置

package svc

import (
    "github.com/zeromicro/go-zero/zrpc"
    "go-zero-courseware/user/api/internal/config"
    "go-zero-courseware/user/rpc/userclient"
)

type ServiceContext struct {
    Config config.Config

    UserRpc userclient.User
}

func NewServiceContext(c config.Config) *ServiceContext {
    return &ServiceContext{
        Config:  c,
        UserRpc: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
    }
}

user/api/internal/logic/userregisterlogic.go增加註冊邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/userclient"
    "google.golang.org/grpc/status"

    "go-zero-courseware/user/api/internal/svc"
    "go-zero-courseware/user/api/internal/types"

    "github.com/zeromicro/go-zero/core/logx"
)

type UserRegisterLogic struct {
    logx.Logger
    ctx    context.Context
    svcCtx *svc.ServiceContext
}

func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
    return &UserRegisterLogic{
        Logger: logx.WithContext(ctx),
        ctx:    ctx,
        svcCtx: svcCtx,
    }
}

func (l *UserRegisterLogic) UserRegister(req *types.RegisterRequest) (resp *types.RegisterResponse, err error) {
    _, err = l.svcCtx.UserRpc.Register(l.ctx, &userclient.RegisterRequest{
        LoginName: req.LoginName,
        Username:  req.Username,
        Password:  req.Password,
        Sex:       req.Sex,
    })
    if err != nil {
        return nil, status.Error(500, err.Error())
    }

    return &types.RegisterResponse{}, nil
}

user/api/internal/logic/userloginlogic.go增加登入邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/userclient"

    "go-zero-courseware/user/api/internal/svc"
    "go-zero-courseware/user/api/internal/types"

    "github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
    logx.Logger
    ctx    context.Context
    svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
    return &UserLoginLogic{
        Logger: logx.WithContext(ctx),
        ctx:    ctx,
        svcCtx: svcCtx,
    }
}

func (l *UserLoginLogic) UserLogin(req *types.LoginRequest) (resp *types.LoginResponse, err error) {
    login, err := l.svcCtx.UserRpc.Login(l.ctx, &userclient.LoginRequest{
        LoginName: req.LoginName,
        Password:  req.Password,
    })
    if err != nil {
        return nil, err
    }

    return &types.LoginResponse{
        Id:    login.Id,
        Token: login.Token,
    }, nil
}

user/api/internal/logic/userinfologic.go增加使用者資訊邏輯

package logic

import (
    "context"
    "go-zero-courseware/user/rpc/userclient"
    "google.golang.org/grpc/status"

    "go-zero-courseware/user/api/internal/svc"
    "go-zero-courseware/user/api/internal/types"

    "github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
    logx.Logger
    ctx    context.Context
    svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
    return &UserInfoLogic{
        Logger: logx.WithContext(ctx),
        ctx:    ctx,
        svcCtx: svcCtx,
    }
}

func (l *UserInfoLogic) UserInfo(req *types.UserInfoRequest) (resp *types.UserInfoResponse, err error) {
    info, err := l.svcCtx.UserRpc.UserInfo(l.ctx, &userclient.UserInfoRequest{
        Id: req.Id,
    })
    if err != nil {
        return nil, status.Error(500, err.Error())
    }

    return &types.UserInfoResponse{
        Id:        info.Id,
        Username:  info.Username,
        LoginName: info.LoginName,
        Sex:       info.Sex,
    }, nil
}

6、docker-compose增加配置

user/rpc目錄下生成rpc的Dockerfile

goctl docker -go user.go

user/api目錄下生成api的Dockerfile

goctl docker -go user.go

根目錄下docker-compose.yml增加使用者服務api和rpc配置

version: '3.5'
# 網路配置
networks:
  backend:
    driver: bridge

# 服務容器配置
services:
  etcd:                                  # 自定義容器名稱
    build:
      context: etcd                    # 指定構建使用的 Dockerfile 檔案
    environment:
      - TZ=Asia/Shanghai
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
    ports:                               # 設定埠對映
      - "2379:2379"
    networks:
      - backend
    restart: always

  etcd-manage:
    build:
      context: etcd-manage
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "7000:8080"                    # 設定容器8080埠對映指定宿主機埠,用於宿主機訪問視覺化web
    depends_on:                                       # 依賴容器
      - etcd                                          # 在 etcd 服務容器啟動後啟動
    networks:
      - backend
    restart: always

  courseware-rpc: # 自定義容器名稱
    build:
      context: courseware                 # 指定構建使用的 Dockerfile 檔案
      dockerfile: rpc/Dockerfile
    environment: # 設定環境變數
      - TZ=Asia/Shanghai
    privileged: true
    ports: # 設定埠對映
      - "9400:9400"  # 課件服務rpc埠
    stdin_open: true                     # 開啟標準輸入,可以接受外部輸入
    tty: true
    networks:
      - backend
    restart: always                      # 指定容器退出後的重啟策略為始終重啟

  courseware-api: # 自定義容器名稱
    build:
      context: courseware                  # 指定構建使用的 Dockerfile 檔案
      dockerfile: api/Dockerfile
    environment: # 設定環境變數
      - TZ=Asia/Shanghai
    privileged: true
    ports: # 設定埠對映
      - "8400:8400"  # 課件服務api埠
    stdin_open: true                     # 開啟標準輸入,可以接受外部輸入
    tty: true
    networks:
      - backend
    restart: always                      # 指定容器退出後的重啟策略為始終重啟

  user-rpc: # 自定義容器名稱
    build:
      context: user                 # 指定構建使用的 Dockerfile 檔案
      dockerfile: rpc/Dockerfile
    environment: # 設定環境變數
      - TZ=Asia/Shanghai
    privileged: true
    ports: # 設定埠對映
      - "9300:9300"  # 課件服務rpc埠
    stdin_open: true                     # 開啟標準輸入,可以接受外部輸入
    tty: true
    networks:
      - backend
    restart: always                      # 指定容器退出後的重啟策略為始終重啟

  user-api: # 自定義容器名稱
    build:
      context: user                  # 指定構建使用的 Dockerfile 檔案
      dockerfile: api/Dockerfile
    environment: # 設定環境變數
      - TZ=Asia/Shanghai
    privileged: true
    ports: # 設定埠對映
      - "8300:8300"  # 課件服務api埠
    stdin_open: true                     # 開啟標準輸入,可以接受外部輸入
    tty: true
    networks:
      - backend
    restart: always                      # 指定容器退出後的重啟策略為始終重啟

6、執行user服務

user目錄下

go mod tidy

到專案根目錄

docker-compose up -d

7、測試介面

```json
localhost:8300/api/user/register
{
“loginName”: “liuyuede”,
“username”: “liuyuede”,
“sex”: 1,
“password”: “123”
}
localhost:8300/api/user/login
{
“loginName”: “liuyuede”,
“password”: “123”
}
localhost:8300/api/user/userInfo
{
“id”: 2
}

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章