initial commit
This commit is contained in:
214
README.md
Normal file
214
README.md
Normal file
@ -0,0 +1,214 @@
|
||||
# godemo
|
||||
|
||||
这是一个使用Go语言开发的示例项目,用于演示Go语言的基础知识。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
godemo/ # 项目根目录
|
||||
├── README.md # 项目说明文档
|
||||
├── go.mod # Go模块定义
|
||||
├── go.sum # 依赖校验
|
||||
├── docker/ # Docker文件目录
|
||||
└── docker-gateway/ # gateway服务的Docker文件目录
|
||||
├── Dockerfile # gateway服务的Dockerfile文件
|
||||
└── docker-compose.yml # gateway服务的docker-compose文件
|
||||
├── sql/ # 数据库SQL定义
|
||||
└── users.sql # 用户表SQL文件
|
||||
├── api/ # HTTP服务定义
|
||||
└── gateway.api # gateway服务的HTTP API定义文件
|
||||
├── rpc/ # RPC服务定义
|
||||
└── user.proto # user服务的RPC定义文件
|
||||
├── user/ # 用户服务
|
||||
├── gateway/ # 网关服务
|
||||
... ...
|
||||
```
|
||||
|
||||
## 基于.proto生成RPC服务代码
|
||||
|
||||
```bash
|
||||
# 在项目的根目录执行(以user服务为例)
|
||||
> goctl rpc protoc [rpc/user.proto] --go-grpc_out=[user] --go_out=[user] --zrpc_out=[user]
|
||||
```
|
||||
|
||||
## 基于.api生成HTTP服务代码
|
||||
|
||||
```bash
|
||||
# 在项目的根目录执行(以gateway服务为例)
|
||||
> goctl api go -api [api/gateway.api] -dir [gateway]
|
||||
```
|
||||
|
||||
## 基于数据库生成模型层代码
|
||||
|
||||
```bash
|
||||
# 在项目的根目录执行(以category服务为例)
|
||||
> goctl model pg datasource -url="postgres://postgres:postgres@localhost:19732/godemo?sslmode=disable" -schema="public" -table="categories" -dir="./category/internal/model"
|
||||
```
|
||||
|
||||
**说明**
|
||||
|
||||
数据表的定义在sql目录下,到postgres执行对应的数据表文件,然后用上面的命令生成模型层代码。
|
||||
|
||||
后续模型层增加接口,可以在internal/model/categoriesmodel.go中增加接口定义(以category服务为例):
|
||||
)。
|
||||
|
||||
## 整合模型层代码
|
||||
|
||||
生成的模型层代码要被业务代码使用,需要引入到业务代码中。主要修改三个地方(以category服务为例):
|
||||
|
||||
```go
|
||||
# etc/category.yaml 增加下面的配置
|
||||
DB:
|
||||
DataSource: postgres://postgres:postgres@localhost:19732/godemo?sslmode=disable
|
||||
MaxOpenConns: 100
|
||||
MaxIdleConns: 20
|
||||
ConnMaxLifetime: 3600
|
||||
|
||||
# internal/config/config.go
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int
|
||||
ConnMaxLifetime int
|
||||
}
|
||||
}
|
||||
|
||||
# internal/svc/serviceContext.go
|
||||
import (
|
||||
"godemo/category/internal/config"
|
||||
"godemo/category/internal/model"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
CategoryModel model.CategoriesModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
conn := sqlx.NewSqlConn("postgres", c.DB.DataSource)
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
CategoryModel: model.NewCategoriesModel(conn),
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 创建Dockerfile
|
||||
|
||||
```bash
|
||||
# 在项目的根目录执行(以category服务为例)
|
||||
> goctl docker -go [category/category.go]
|
||||
```
|
||||
|
||||
生成的Dockerfile里面有些路径需要修改,也有些内容需要增加。
|
||||
|
||||
然后再docker目录创建[docker-category]文件夹,把Dockerfile移动到docker-category文件夹下。
|
||||
|
||||
## 创建docker-compose.yml
|
||||
|
||||
在Dockerfile同级目录创建docker-compose.yaml,内容可以参考下面的category服务的。另外需要在当前目录创建etc文件夹,把服务的配置文件拷贝到etc文件夹下,并命名为config.yaml。
|
||||
|
||||
```yml
|
||||
services:
|
||||
category:
|
||||
image: category:v1
|
||||
container_name: category
|
||||
restart: always
|
||||
volumes:
|
||||
- ./etc:/app/etc
|
||||
networks:
|
||||
- godemo_network
|
||||
# 端口映射,生产环境非必须,开发环境用于方便调试
|
||||
ports:
|
||||
- "60300:60300"
|
||||
|
||||
networks:
|
||||
godemo_network:
|
||||
name: godemo_network
|
||||
external: true
|
||||
```
|
||||
|
||||
## 编译Docker镜像
|
||||
|
||||
```bash
|
||||
# 在项目的根目录执行(以file服务为例)
|
||||
> docker build -t [file:v1] -f [docker/docker-file/Dockerfile] .
|
||||
```
|
||||
|
||||
## 运行Docker容器
|
||||
|
||||
```bash
|
||||
# 在对应服务的docker目录执行(以file服务为例)
|
||||
> docker/docker-file> docker compose up -d
|
||||
```
|
||||
|
||||
## RPC服务测试/调试
|
||||
|
||||
```bash
|
||||
# 查看60200端口下的RPC服务提供的服务
|
||||
> grpcurl -plaintext localhost:60200 list
|
||||
|
||||
# 查看某个服务提供的接口
|
||||
> grpcurl -plaintext localhost:60200 list file.File
|
||||
|
||||
# 调用某个接口
|
||||
> grpcurl -d '{\"file_id\": \"aaaa/333333333333.png\"}' -plaintext localhost:60200 file.File/GetFileUrl
|
||||
```
|
||||
|
||||
## 网关服务
|
||||
|
||||
## 用户服务
|
||||
|
||||
**接口定义**
|
||||
|
||||
- 用户登录
|
||||
- 获取用户信息
|
||||
- 用户注册
|
||||
- 用户注销
|
||||
|
||||
## 文件服务
|
||||
|
||||
## 分类服务
|
||||
|
||||
分类服务(Category Service),这是一个用于统一管理多系统/多模块分类体系的微服务。
|
||||
- 支持多级分类
|
||||
- 通过system_id支持多系统分类
|
||||
|
||||
```dev
|
||||
# 增
|
||||
grpcurl -plaintext -d '{\"system_id\": \"ecommerce\",\"name\": \"Electronics\",\"alias\": \"electronics\",\"parent_id\": \"\"}' localhost:60300 category.Category/CreateCategory
|
||||
|
||||
# 删
|
||||
grpcurl -plaintext -d '{\"id\": \"fe8d31d5-11f4-4ddd-a4ac-76d358faed2b\"}' localhost:60300 category.Category/DeleteCategory
|
||||
|
||||
# 改
|
||||
grpcurl -plaintext -d '{\"id\": \"d628cf35-539f-4533-a5a2-492d729ecf3b\",\"name\": \"New Electronics Name\"
|
||||
}' localhost:60300 category.Category/UpdateCategory
|
||||
|
||||
# 查
|
||||
grpcurl -plaintext -d '{\"id\": \"d628cf35-539f-4533-a5a2-492d729ecf3b\"}' localhost:60300 category.Category/GetCategory
|
||||
|
||||
```
|
||||
|
||||
## 图库服务
|
||||
|
||||
**接口定义**
|
||||
|
||||
- 获取图片列表
|
||||
- 获取图片详情
|
||||
- 获取分类
|
||||
- 上传图片
|
||||
- 删除图片
|
||||
- 修改图片
|
||||
- 获取图片URL
|
||||
- 下载图片
|
||||
|
||||
/api/gallery/v1/
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user