go-zero學習之訂單rpc服務

charliecen發表於2021-10-13

訂單服務

建立訂單表
> vim .\service\order\model\order.sql

CREATE TABLE `order` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `user_id` BIGINT NOT NULL DEFAULT 0 COMMENT '使用者id',
    `money` BIGINT NOT NULL DEFAULT 0 COMMENT '訂單金額',
    `goods_id` BIGINT NOT NULL DEFAULT 0 COMMENT '商品id',
    `create_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    `update_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `uid_m_g` (`user_id`,`money`,`goods_id`)
) ENGINE=INNODB  DEFAULT CHARSET=utf8mb4;
生成程式碼
❯ goctl.exe model mysql ddl -src order.sql -dir .
Done.
編寫rpc程式碼
> vim .\service\order\rpc\order.proto

syntax = "proto3";

package template;

option go_package = "order";

message OrderReq {
  int64 id = 1;
}

message OrderResp {
  int64 id  = 1;
  int64 user_id = 2;
  int64 age = 3;
  int64 goods_id = 4;
}

service order {
  rpc getOrder(OrderReq) returns (OrderResp);
}
生成程式碼
❯ goctl.exe rpc proto  -src .\order.proto -dir .
protoc  --proto_path=C:\go\src\go-zero-demo1\service\order\rpc order.proto --go_out=plugins=grpc:C:\go\src\go-zero-demo1\service\order\rpc\order --go_opt=Morder.proto=../order
Done.
新增配置
> vim .\service\order\rpc\etc\order.yaml

Name: order.rpc
ListenOn: 127.0.0.1:8080
Etcd:
  Hosts:
  - 127.0.0.1:2379
  Key: order.rpc
Mysql:
  DataSource: root:root@tcp(172.15.0.11:3306)/go_zero?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
宣告配置型別
> vim .\service\order\rpc\etc\order.yaml

package config

import "github.com/tal-tech/go-zero/zrpc"

type Config struct {
    zrpc.RpcServerConf
    Mysql struct{
        DataSource string
    }
}
填充依賴
> vim .\service\order\rpc\internal\svc\servicecontext.go

package svc

import (
    "github.com/tal-tech/go-zero/core/stores/sqlx"
    "go-zero-demo1/service/order/model"
    "go-zero-demo1/service/order/rpc/internal/config"
)

type ServiceContext struct {
    Config config.Config
    OrderModel model.OrderModel
}

func NewServiceContext(c config.Config) *ServiceContext {
    conn := sqlx.NewMysql(c.Mysql.DataSource)
    return &ServiceContext{
        Config: c,
        OrderModel: model.NewOrderModel(conn),
    }
}
填充邏輯
> vim .\service\order\rpc\internal\logic\getorderlogic.go

package logic

import (
    "context"

    "go-zero-demo1/service/order/rpc/internal/svc"
    "go-zero-demo1/service/order/rpc/order"

    "github.com/tal-tech/go-zero/core/logx"
)

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

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

func (l *GetOrderLogic) GetOrder(in *order.OrderReq) (*order.OrderResp, error) {
    // todo: add your logic here and delete this line
    orderInfo, err := l.svcCtx.OrderModel.FindOne(in.Id)
    if err != nil {
        return nil, err
    }
    return &order.OrderResp{
        Id: orderInfo.Id,
        UserId: orderInfo.UserId,
        Money: orderInfo.Money,
        GoodsId: orderInfo.GoodsId,
    }, nil
}
執行rpc服務
❯ go run .\order.go -f .\etc\order.yaml
Starting rpc server at 127.0.0.1:8080...
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章