grpc基于golang的客户端服务端整体预览

proto文件:

/*
* @Title: 服务接口说明
* @Description: 接口目录
*/
service SpotDataHandler {
    rpc GetProductInfo(GetProductInfoRequest) returns (GetProductInfoResponse) {}//获取所有商品信息
}

/**
* @Title: 统一说明
* @Description: 通用message定义

message SpotPriceInfo {
    double high = 2;//当日最高
    double low = 3;//当日最低
    double average = 4;//当日平均
    double vchange = 5;//当日涨跌
    double vchange_rate = 6;//当日涨跌幅
    int32 price_state = 7;//价格状态,0正常,1缺货,2休盘
    string renew_date = 8;//更新日期,YYYY-MM-DD格式
    string renew_time = 9;//更新时间,HH:MM:SS格式
    string price_declaration = 10;//价格说明
}

message PriceType {
    oneof TypeEnum {
        SpotPriceInfo spot_price = 1;//现货
    }
}

message PriceInfo {
    string product_id = 1;//产品id
    string product_name = 2;//产品名称
    string unit = 3;//单位
    repeated PriceType price_list = 4;
}

/**
* @Title: 1.获取所有分类信息
* @Description:
* @Request: GetProductInfoRequest
* @Response: GetProductInfoResponse
*/
message GetProductInfoRequest {
    string language = 1;//cn中文,en英文
}

message ProductInfo {
    string product_id = 1;//产品id
    string product_name = 2;//产品名称
    string spec = 3;//产品规格
    string brand_mark = 4;//品牌
    string area = 5;//地区
    string unit = 6;//单位
}

message GetProductInfoResponse {
    message CategoryInfo {
        int32 category_id = 1;//属性id,唯一
        string category_name = 2;//属性名称
        int32 list_order = 3;//排序id
        repeated ProductInfo products = 4;//商品列表
    }

    message ItemInfo {
        int32 item_id = 1;//品目id,唯一
        string item_name = 2;//品目名称
        int32 list_order = 3;//排序id
        repeated CategoryInfo categories = 4;
    }

    MessageProto codeMsg = 1;
    repeated ItemInfo items = 2;
}

然后由这个proto文件生成对应的pb.go:

.
.
.
type SpotDataHandlerServer interface {
    GetProductInfo(context.Context, *GetProductInfoRequest) (*GetProductInfoResponse, error)
}
.
.
.

服务端:

//实现pb.go中interface定义的方法
func (s *SpotDataHandler) GetProductInfo(c context.Context, req *spot.GetProductInfoRequest) (*spot.GetProductInfoResponse, error) {
    // check param
    switch strings.ToLower(req.Language) {
    case SpotDataHandler_Language_CN:
    case SpotDataHandler_Language_EN:
    default:
        req.Language = SpotDataHandler_Language_CN
    }

    var itemInfoList []*spot.GetProductInfoResponse_ItemInfo
    for _, item := range memory.GetItemListNoLimit() {
        if item.Id <= 0 {
            continue
        }

        var categoryList []*spot.GetProductInfoResponse_CategoryInfo
        for _, tagId := range memory.GetTagIdsByItem(item.Id) {
            relation := memory.GetRealtionByTagId(tagId)
            if relation == nil {
                continue
            }

            cat := memory.GetCategoryTag(relation.CategoryId)
            if cat == nil {
                continue
            }

            var proudctList []*spot.ProductInfo
            for _, productId := range memory.GetProductIdsByTagId(tagId) {
                productDetail := memory.GetProductInfoNoLimit(productId)
                if productDetail == nil {
                    continue
                }

                if productDetail.PriceType > constant.Price_Type_Premium {
                    continue
                }

                name := productDetail.ProductName
                spec := productDetail.Spec
                brand := productDetail.BrandMark
                unit := productDetail.Unit
                area := productDetail.Area

                if strings.ToLower(req.Language) == SpotDataHandler_Language_EN {
                    productDetailEn, err := cache.GRedis[cache.REDIS_EN].CacheGetEnProductDetailByID(productId)
                    if err != nil {
                        productDetailEn, err = db.DBGetEnProductDetailByProductID(productId)
                        if err != nil {
                            logger.Warnning(err)
                            continue
                        }

                        go command.LoadEnProducts(constant.PROGRAM_ENV_EN)
                    }

                    name = productDetailEn.ProductName
                    spec = productDetailEn.Spec
                    brand = ""
                    unit = productDetailEn.Unit
                    area = ""
                }

                proudctList = append(proudctList, &spot.ProductInfo{
                    ProductId:   productId,
                    ProductName: name,
                    Spec:        spec,
                    BrandMark:   brand,
                    Unit:        unit,
                    Area:        area,
                })
            }

            if len(proudctList) == 0 {
                continue
            }

            categoryList = append(categoryList, &spot.GetProductInfoResponse_CategoryInfo{
                CategoryId:   int32(cat.Id),
                CategoryName: cat.CategoryName,
                ListOrder:    int32(cat.ListOrder),
                Products:     proudctList,
            })
        }

        if len(categoryList) == 0 {
            continue
        }

        itemInfoList = append(itemInfoList, &spot.GetProductInfoResponse_ItemInfo{
            ItemId:     int32(item.Id),
            ItemName:   item.ItemName,
            ListOrder:  int32(item.ListOrder),
            Categories: categoryList,
        })
    }

    return &spot.GetProductInfoResponse{
        CodeMsg: &spot.MessageProto{
            Code: protocol.RESPONSE_CODE_SUCCESS,
            Msg:  protocol.RESPONSE_MSG_SUCCESS,
        },
        Items: itemInfoList,
    }, nil
}

客户端:

func initSpotGRPCConn() {
    // Set up a connection to the server.
    logger.Debug("connect GRPC service", common.GetConfigs().API.SpotCenterGRPC)
    conn, err := grpc.Dial(common.GetConfigs().API.SpotCenterGRPC, grpc.WithInsecure(), grpc.WithReadBufferSize(10*1024*1024), grpc.WithWriteBufferSize(1*1024*1024))
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    DefaultSpotDataConn = conn

    logger.Debug("init spot api")
    DefaultSpotDataClient = spot.NewSpotDataHandlerClient(DefaultSpotDataConn)
}


func getSpotItems(pr *spot.GetProductInfoRequest) ([]*spot.GetProductInfoResponse_ItemInfo, error) {
    con, _ := context.WithTimeout(context.Background(), time.Second*utility.TIME_OUT)
    replay, err := invoke.DefaultSpotDataClient.GetProductInfo(con, pr)
    if err != nil {
        return nil, err
    }
    if replay.CodeMsg == nil {
        return nil, utility.NewError(utility.ERROR_RPC_CODE, utility.ERROR_MSG_RPC_DATA_NULL)
    }
    // success == 0
    if replay.CodeMsg.Code != utility.SMMSPOT_DATABASE_SUCCESS {
        return nil, utility.NewError(utility.ERROR_RPC_CODE, replay.CodeMsg.Msg)
    }
    if replay.Items == nil {
        return nil, utility.NewError(utility.ERROR_RPC_CODE, utility.ERROR_MSG_RPC_DATA_NULL)
    }
    return replay.Items, nil
}

猜你喜欢

转载自blog.csdn.net/jeffrey11223/article/details/80946121