黑马程序员技术交流社区
标题:
【上海校区】grpc基于golang的客户端服务端整体预览
[打印本页]
作者:
不二晨
时间:
2018-7-10 10:47
标题:
【上海校区】grpc基于golang的客户端服务端整体预览
本帖最后由 不二晨 于 2018-7-10 10:58 编辑
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
i
f strings
.ToLower
(req
.Language
) == SpotDataHandler_Language_EN { productDetailEn, err := cache
.GRedis
[cache
.REDIS
.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
), C
ategoryName: cat
.CategoryName
,
ListOrder: int32(cat
.ListOrder
),
Products: proudctList,
}) }
if len(categoryList) ==
0
{ continue }
itemInfoList = append(itemInfoList, &spot
.GetProductInfoResponse
_ItemInf
o{
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}
【转载】原文地址:
https://blog.csdn.net/jeffrey11223/article/details/80946121
作者:
wuqiong
时间:
2018-7-12 15:45
作者:
吴琼老师
时间:
2018-7-12 16:37
作者:
不二晨
时间:
2018-7-12 17:12
赞一个
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2