Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sample_douyin demo #40

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ header:
- easy_note/cmd/api/router_gen.go
- book-shop/kitex_gen/**
- book-shop/docs/**
- sample_douyin/kitex_gen/**
- sample_douyin/cmd/api/biz/model/**
- sample_douyin/cmd/api/biz/router/**
- sample_douyin/cmd/api/router_gen.go

comment: on-failure
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,54 @@ The book-shop application is divided into four microservices:

#### Contributors & Maintainers
[@bodhisatan](https://github.com/bodhisatan)

### 5. Sample Douyin

#### Description
##### What is it about and what problem does it solve?
- How to coordinate multiple Kitex services using Hertz?
- How to integrate OSS (such as aliyun-oss...) in Hertz project?

##### What is the business scenario?

> The application shows a short video platform system that includes user login and registration, uploading videos, getting video feed streams, favoriting, commenting, following, and chatting.

The sample_douyin application is divided into seven microservices:

- **api** is an HTTP service that handles HTTP requests and calls other services via RPC.
- **user** is an RPC service that handles user managements.
- **video** is an RPC service that handles video managements.
- **comment** is an RPC service that handles comment managements.
- **favorite** is an RPC service that handles relationship between users and videos.
- **message** is an RPC service that handles message managements.
- **relation** is an RPC service that handles relationships between users.

##### What are the core technologies/projects used?

- [x] Use `hz` and `kitex` to generate code
- [x] Use Hertz `requestid`, `jwt`, `pprof`, `gzip` middlewares
- [x] Use `go-tagexpr` and `thrift-gen-validator` for validating HTTP and RPC request
- [x] Use `obs-opentelemetry` for tracing
- [x] Use `etcd` as service registry.
- [x] Use `GORM` for implementing repository.
- [x] Use `MySQL` as RDBMS.
- [x] Use `Redis` as cache and message queue.
- [x] Use `aliyun-oss` as object storage service.

##### Which CloudWeGo subprojects are used? List all technologies used.
- [Hertz](https://github.com/cloudwego/hertz)
- [obs-opentelemetry](https://github.com/hertz-contrib/obs-opentelemetry)
- [requestid](https://github.com/hertz-contrib/requestid)
- [jwt](https://github.com/hertz-contrib/jwt)
- [pprof](https://github.com/hertz-contrib/pprof)
- [gzip](https://github.com/hertz-contrib/gzip)
- [Kitex](https://github.com/cloudwego/kitex)
- [obs-opentelemetry](https://github.com/kitex-contrib/obs-opentelemetry)
- [registry-etcd](https://github.com/kitex-contrib/registry-etcd)

##### Detailed documentation
[Sample Douyin](./sample_douyin/README.md)

#### Contributors & Maintainers
[@demoasm](https://github.com/demoasm)

32 changes: 32 additions & 0 deletions sample_douyin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
install_kitex_latest:
go install github.com/cloudwego/kitex/tool/cmd/kitex@latest

install_thriftgo:
go install github.com/cloudwego/thriftgo

kitex_gen_user:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/user.thrift # execute in the project root directory

kitex_gen_video:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/video.thrift # execute in the project root directory

kitex_gen_relation:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/relation.thrift # execute in the project root directory

kitex_gen_message:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/message.thrift # execute in the project root directory

kitex_gen_favorite:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/favorite.thrift # execute in the project root directory

kitex_gen_comment:
kitex --thrift-plugin validator -module github.com/cloudwego/biz-demo/sample_douyin idl/comment.thrift # execute in the project root directory

install_hz_latest:
go install github.com/cloudwego/hertz/cmd/hz@latest

hertz_gen_model:
hz model --idl=idl/api.thrift --mod=github.com/cloudwego/biz-demo/sample_douyin --model_dir=hertz_gen

hertz_gen_client:
hz client --idl=idl/api.thrift --base_domain=http://127.0.0.1:8080 --client_dir=api_request --mod=github.com/cloudwego/biz-demo/sample_douyin --model_dir=hertz_gen
162 changes: 162 additions & 0 deletions sample_douyin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Sample Douyin

## Introduction
A simple Douyin service built with `Kitex` and `Hertz` which is divided into seven microservices.

| Service Name | Usage | Framework | protocol | Path | IDL |
|--------------|--------------------------|-------------|----------|--------------|---------------------|
| api | HTTP interface | kitex/hertz | http | cmd/api | idl/api.thrift |
| user | user data management | kitex/gorm | thrift | cmd/user | idl/user.thrift |
| video | video data management | kitex/gorm | thrift | cmd/video | idl/video.thrift |
| favorite | favorite data management | kitex/gorm | thrift | cmd/favorite | idl/favorite.thrift |
| comment | comment data management | kitex/gorm | thrift | cmd/comment | idl/comment.thrift |
| message | message data management | kitex/gorm | thrift | cmd/message | idl/message.thrift |
| relation | relation data management | kitex/gorm | thrift | cmd/relation | idl/relation.thrift |

### Architecture

![Architecture](./images/architecture.jpg)

### Call Relations

![Call Relations](./images/call-relation.jpg)

### Basic Features

- Hertz
- Use `thrift` IDL to define HTTP interface
- Use `hz` to generate server/client code
- Use `Hertz` binding and validate
- Use `obs-opentelemetry` and `jarger` for `tracing`, `metrics`, `logging`
- Middleware
- Use `requestid`, `jwt`, `recovery`, `pprof`, `gzip`
- Kitex
- User `thrift` IDL to define `RPC` interface
- Use `kitex` to generate code
- Use `obs-opentelemetry` and `jarger` for `tracing`, `metrics`, `logging`
- Use `registry-etcd` for service discovery and register

### Catalog Introduce

| catalog | introduce |
|---------------|-------------------------|
| handler | HTTP handler |
| service | business logic |
| rpc | RPC call logic |
| dal | DB operation |
| pack | data pack |
| apimodel | struct definition |
| cache | Redis operation |
| videoHandler | Video stream processing |
| pkg/mw | RPC middleware |
| pkg/consts | constants |
| pkg/errno | customized error number |
| pkg/configs | SQL and Tracing configs |

## Code Generation

| catalog | command |
|-----------------------|--------------------------------------|
| hertz_api_model | make hertz_gen_model |
| hertz_api_client | make hertz_gen_client |
| hertz_api_new | cd cmd/api && make hertz_new_api |
| hertz_api_update | cd cmd/api && make hertz_update_api |
| kitex_user_client | make kitex_gen_user |
| kitex_video_client | make kitex_gen_video |
| kitex_favorite_client | make kitex_gen_favorite |
| kitex_comment_client | make kitex_gen_comment |
| kitex_message_client | make kitex_gen_message |
| kitex_relation_client | make kitex_gen_relation |
| kitex_user_server | cd cmd/user && make kitex_gen_server |
| kitex_video_server | cd cmd/video && make kitex_gen_server |
| kitex_favorite_server | cd cmd/favorite && make kitex_gen_server |
| kitex_comment_server | cd cmd/comment && make kitex_gen_server |
| kitex_message_server | cd cmd/message && make kitex_gen_server |
| kitex_relation_server | cd cmd/relation && make kitex_gen_server |

## Quick Start

### Setup Basic Dependence

```shell
docker-compose up -d
```

### Change config
Edit the following code block in pkg/consts/consts.go and replace it with your own Alibaba Cloud OSS AKID & ASK etc.
```go
Endpoint = "oss-c**************cs.com"
AKID = "LTAI****************92kxo"
ASK = "SmEa**************LuS9N3K9"
Bucket = "douy******************67"
CDNURL = "http://*************.cn/"
```

### Run User RPC Server

```shell
cd cmd/user
sh build.sh
sh output/bootstrap.sh
```

### Run Video RPC Server

```shell
cd cmd/video
sh build.sh
sh output/bootstrap.sh
```

### Run Relation RPC Server

```shell
cd cmd/relation
sh build.sh
sh output/bootstrap.sh
```

### Run Favorite RPC Server

```shell
cd cmd/favorite
sh build.sh
sh output/bootstrap.sh
```

### Run Comment RPC Server

```shell
cd cmd/comment
sh build.sh
sh output/bootstrap.sh
```
### Run Message RPC Server

```shell
cd cmd/message
sh build.sh
sh output/bootstrap.sh
```
### Run API Server

```shell
cd cmd/api
sh build.sh
sh output/bootstrap.sh
```

### Jaeger

Visit `http://127.0.0.1:16686/` on browser

### Grafana

Visit `http://127.0.0.1:3000/` on browser

### APK

[:memo:Sample Douyin APP Package Instructions](https://bytedance.feishu.cn/docs/doccnM9KkBAdyDhg8qaeGlIz7S7#quPkfu)

## [:point_right: MORE](https://a6i0rzkzjm.feishu.cn/docx/Xa8sdTIGJopWrNxYgeVcrmxPnKe#doxcnPDVQrEZq14hwckf1K1Taqg)

Loading