# house **Repository Path**: LiuJinDou/house ## Basic Information - **Project Name**: house - **Description**: 租房服务 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-14 - **Last Updated**: 2023-09-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # house #### 介绍 租房服务 #### 软件架构 go-zero api 目录 对外提供访问 rpc 目录 对api提供服务 rpc操作数据 #### 安装教程 ##### rpc 1. 创建 ``` goctl rpc new user_srv(服务名称) ``` 2. 编写proto ``` # 创建pb目录 # 蒋proto文件移动到pb目录下 编辑proto文件 定义请求体和响应体 type Request { } 定义rpc服务 service User_srv { // rpc 服务 rpc Login(Request) returns(Response); } ``` 3. 生成rpc代码 ```shell goctl rpc protoc ./pb/user_srv.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=. ``` 4. 配置数据库连接 ```shell # 在yaml添加Mysql配置 Mysql: Datasource: root:password@tcp(127.0.0.1:3306)/houseeight?charset=utf8 # 在config加载Mysql配置 // 加载配置 Mysql struct { Datasource string } # 在svc目录连接Mysql并注入Model服务 type ServiceContext struct { Config config.Config UserModel model.UserModel } func NewServiceContext(c config.Config) *ServiceContext { // 连接Mysql conn := sqlx.NewMysql(c.Mysql.Datasource) return &ServiceContext{ Config: c, UserModel: model.NewUserModel(conn), } } # logic目录调用model操作数据库 is_exist, err := l.svcCtx.UserModel.FindByMobile(context.Background(), in.Mobile) ``` ##### api 1. 创建 ``` goctl api new user_api(服务名称) ``` 2. 编写api ``` # 创建api目录 # 蒋api文件移动到api目录下 编辑api文件 定义请求体和响应体 type Request { } 定义api服务 service { @handler service post /login (LoginReq) returns (LoginRsp) // 访问的路由 请求参数 响应参数 } ``` 3. 生成api代码 ```shell goctl api go --api=./api/user_api.api --dir . ``` 4. 调用rpc ```shell # 在yaml添加rpc服务 # 添加rpc服务配置 UserRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: usersrv.rpc # 在config加载rpc服务 type Config struct { rest.RestConf UserRpc zrpc.RpcClientConf } # 在svc目录注入rpc服务 type ServiceContext struct { Config config.Config UserRpc usersrvclient.UserSrv } func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ Config: c, UserRpc: usersrvclient.NewUserSrv(zrpc.MustNewClient(c.UserRpc)), } } # logic目录调用rpc服务即可 // 调用rpc go work init user_api user_srv rsp, err := l.svcCtx.UserRpc.Register(context.Background(), &user_srv.Request{ RealName: req.RealName, Name: req.UserName, Mobile: req.Mobile, Password: fmt.Sprintf("%x", md5.Sum([]byte(req.Password))), IdCard: req.IdCard, AvatarUrl: req.AvatarUrl, }) ``` #### 使用说明 1. 跨多个modules直接进行调用 ```shell go work init modules_name example go work init user_api user_srv ``` 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)