go-zero学习之订单rpc服务

Golang
477
0
0
2022-07-11
标签   go-zero

订单服务

创建订单表
> 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...