Go项目(商品微服务-1)
news /
2024/4/20 13:30:59 /
文章来源:https://blog.csdn.net/weixin_39757637/article/details/129222068
简介
商品微服务主要在于表的设计,建哪些表?表之间的关系是怎样的? 主要代码就是 CURD 表和字段的设计是一个比较有挑战性的工作,比较难说清楚,也需要经验的积累,这里关注点在微服务,暂时先 “借鉴” 别人的设计 一般需要多次迭代才能设计出比较合理的表结构 我们将商品相关的服务都放在这部分,方便建表和管理 ;微服务并不是越多越好,分成太多服务可能会让系统太复杂难以管理 如何划分微服务需要积累经验,也有针对性的方法可以套用,需要单独学习
建表
proto
proto 文件的设计又是一个重点,首先要根据页面进行需求分析(前端页面和后台管理页面),需要哪些接口?传递哪些参数?会有什么响应? 接口定义也不是一蹴而就的,后面可能会修改,不必在一开始追求完美 protoc -I . goods.proto --go_out=plugins=grpc:.
生成 stub 文件 遇到点小问题,empty.proto 找不到,拷过来放到 proto/google/protobuf 下面试试 定义 Service 层(Server端)接口(handler),直接搜 GoodsServer
,将接口定义拷过来 这里接口太多,我们分在不同 handler 文件实现,不着急实现,先把接口定义拷过去修改一下引用,因为没有实现的话项目无法启动, 再修改 main.go 和 nacos 配置文件(本地/中心配置),启动项目看能否注册成功
handler
具体实现各接口,先从品牌和轮播图开始,这里放个获取品牌列表的例子
func ( s * GoodsServer) BrandList ( ctx context. Context, req * proto. BrandFilterRequest) ( * proto. BrandListResponse, error ) { brandListResponse := proto. BrandListResponse{ } var brands [ ] model. Brandsresult := global. DB. Scopes ( Paginate ( int ( req. Pages) , int ( req. PagePerNums) ) ) . Find ( & brands) if result. Error != nil { return nil , result. Error} var total int64 global. DB. Model ( & model. Brands{ } ) . Count ( & total) brandListResponse. Total = int32 ( total) var brandResponses [ ] * proto. BrandInfoResponsefor _ , brand := range brands { brandResponses = append ( brandResponses, & proto. BrandInfoResponse{ Id: brand. ID, Name: brand. Name, Logo: brand. Logo, } ) } brandListResponse. Data = brandResponsesreturn & brandListResponse, nil
}
回答一个上一篇的问题:查询数据时不传表名,传入 model 定义的 struct 就可以吗? 是的,比如 DB.First(&model.Brands{})
,因为 struct 名就是表名,改表名也是通过绑在 struct 上的函数进行的 商品分类 先看 GetAllCategorysList ,这里比较 tricky,因为有多级分类,需要同时查询两级 subCategory 我们希望得到的数据格式[ { "id" : xxx, "name" : "" , "level" : 1 , "is_tab" : false , "parent" : 13xxx, "sub_category" : [ "id" : xxx, "name" : "" , "level" : 1 , "is_tab" : false , "sub_category" : [ "id" : xxx, "name" : "" , ] ] }
]
通过 gorm 提供的预加载(反向查询)实现 条件查询一级目录:global.DB.Where(&model.Category{Level: 1})
反向查询两级子类目:Preload("SubCategory.SubCategory")
再贴一个获取子分类的函数,grpc 的接口实现都是三步走
func ( s * GoodsServer) GetSubCategory ( ctx context. Context, req * proto. CategoryListRequest) ( * proto. SubCategoryListResponse, error ) { categoryListResponse := proto. SubCategoryListResponse{ } var category model. Categoryif result := global. DB. First ( & category, req. Id) ; result. RowsAffected == 0 { return nil , status. Errorf ( codes. NotFound, "商品分类不存在" ) } categoryListResponse. Info = & proto. CategoryInfoResponse{ Id: category. ID, Name: category. Name, Level: category. Level, IsTab: category. IsTab, ParentCategory: category. ParentCategoryID, } var subCategorys [ ] model. Categoryvar subCategoryResponse [ ] * proto. CategoryInfoResponseglobal. DB. Where ( & model. Category{ ParentCategoryID: req. Id} ) . Find ( & subCategorys) for _ , subCategory := range subCategorys { subCategoryResponse = append ( subCategoryResponse, & proto. CategoryInfoResponse{ Id: subCategory. ID, Name: subCategory. Name, Level: subCategory. Level, IsTab: subCategory. IsTab, ParentCategory: subCategory. ParentCategoryID, } ) } categoryListResponse. SubCategorys = subCategoryResponsereturn & categoryListResponse, nil
}
这里需要写接口测试 ,可以在 test 目录写 UT 或者每个 handler 新建目录在 main 函数运行看效果(一个目录下只能有一个main) 品牌分类 查询品牌和分类数据表,这是个中间表 ,内容较多,相关操作也较为复杂一点 model 定义如下type GoodsCategoryBrand struct { BaseModelCategoryID int32 `gorm:"type:int;index:idx_category_brand,unique"` Category Category BrandsID int32 `gorm:"type:int;index:idx_category_brand,unique"` Brands Brands
}
这些接口都是给 web 层调用,但有的是给用户准备数据,有的是给后台管理员 准备
商品
商品接口,关系到最终展示给用户的数据,最重要 注:这部分有很多入口 :搜索、新品、热门、价格区间、商品分类,都需要调用接口获取对应商品,这些都可以看做是查询条件 注意下面几点 条件是可以叠加的,所有用 localDB
接收前面过滤出的数据体 分类条件要考虑层级 后续会集成 elastic 用于搜索
小结
快速开发了商品微服务的 Service 层,服务配置项还是和 user_srv 相同,包括 nacos,mysql,consul,serverConfig,不需要更改定义 接下来是商品微服务 API 层的快速开发
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_74496.aspx
如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!