# 2106A **Repository Path**: LiuJinDou/2106-a ## Basic Information - **Project Name**: 2106A - **Description**: bee框架学习 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-08-25 - **Last Updated**: 2023-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### rpc 服务 创建rpc ```shell goctl rpc new name ``` 修改proto文件 ```shell 定义rpc 服务 定义请求体 定义响应体 ``` 生成rpc服务的代码 ```shell goctl rpc protoc ./pb/user.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=. ``` #### go-zero 里面的 model #### 两种方式生成增删改查 ##### 第一种、通过数据库连接的方式 ```shell goctl model mysql datasource --table="user" --dir="./internal/model" --url="root:password@tcp(127.0.0.1:3306)/zero_six" ``` ##### 第二种、通过sql文件的方式 ###### 创建sql ```shell CREATE TABLE `goods` ( `id` int NOT NULL AUTO_INCREMENT, `goods_name` varchar(150) DEFAULT '' COMMENT '商品名称', `goods_price` decimal(10,2) DEFAULT NULL COMMENT '商品价格', `status` int DEFAULT '0' COMMENT '库存', `goods_img` varchar(150) DEFAULT NULL COMMENT '图片', `detail` text COMMENT '内容', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ``` ###### 创建sql ```shell goctl model mysql ddl --src="./internal/model/goods.sql" --dir="./internal/model/" ``` #### 查询之前、连接数据库 #### rpc model里面连接数据库 1. 第一步 (在etc目录下的yaml文件) ```shell Mysql: # 数据库连接的资源 datasource: root:password@tcp(127.0.0.1:3306)/zero_six ``` 2. 第二步(加载配置 在internal目录下的config) ```shell type Config struct { zrpc.RpcServerConf Mysql struct{ Datasource string } } ``` 3. 第三步 连接mysql ```shell type ServiceContext struct { Config config.Config UserModel model.UserModel } func NewServiceContext(c config.Config) *ServiceContext { // 连接mysql NewMysqlClient conn := sqlx.NewMysql(c.Mysql.Datasource) return &ServiceContext{ Config: c, UserModel: model.NewUserModel(conn), } } ``` 4. 第四步 (连接成功后、查询数据) ```shell userInfo, err := l.svcCtx.UserModel.FindOne(context.Background(), 1) if err != nil { fmt.Println(err) } fmt.Println(userInfo) ``` 5. 第五步 #### beego 框架调用rpc服务 1. 连接ETCD (借鉴go-zero文件 服务指南 grpc服务连接 etcd) ```shell ``` 2. 调用rcp client 服务 3.