Skip to content

Commit

Permalink
update example doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Sep 4, 2020
1 parent b4aa89f commit c0d0e00
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
6 changes: 5 additions & 1 deletion doc/bookstore.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ Log:

可以看出在我的MacBook Pro上能达到3万+的qps。

## 13. 总结
## 13. 完整代码

[https://github.com/tal-tech/go-zero/tree/master/example/bookstore](https://github.com/tal-tech/go-zero/tree/master/example/bookstore)

## 14. 总结

我们一直强调**工具大于约定和文档**。

Expand Down
28 changes: 15 additions & 13 deletions doc/shorturl.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@
```go
type ServiceContext struct {
Config config.Config
Transformer rpcx.Client // 手动代码
Transformer transformer.Transformer // 手动代码
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Transformer: rpcx.MustNewClient(c.Transform), // 手动代码
Transformer: transformer.NewTransformer(rpcx.MustNewClient(c.Transform)), // 手动代码
}
}
```
Expand All @@ -305,8 +305,7 @@
```go
func (l *ExpandLogic) Expand(req types.ExpandReq) (*types.ExpandResp, error) {
// 手动代码开始
trans := transformer.NewTransformer(l.svcCtx.Transformer)
resp, err := trans.Expand(l.ctx, &transformer.ExpandReq{
resp, err := l.svcCtx.Transformer.Expand(l.ctx, &transformer.ExpandReq{
Shorten: req.Shorten,
})
if err != nil {
Expand All @@ -319,16 +318,15 @@
// 手动代码结束
}
```

通过调用`transformer`的`Expand`方法实现短链恢复到url

通过调用`transformer`的`Expand`方法实现短链恢复到url
* 修改`internal/logic/shortenlogic.go`,如下:

```go
func (l *ShortenLogic) Shorten(req types.ShortenReq) (*types.ShortenResp, error) {
// 手动代码开始
trans := transformer.NewTransformer(l.svcCtx.Transformer)
resp, err := trans.Shorten(l.ctx, &transformer.ShortenReq{
resp, err := l.svcCtx.Transformer.Shorten(l.ctx, &transformer.ShortenReq{
Url: req.Url,
})
if err != nil {
Expand All @@ -341,10 +339,10 @@
// 手动代码结束
}
```

通过调用`transformer`的`Shorten`方法实现url到短链的变换

至此,API Gateway修改完成,虽然贴的代码多,但是期中修改的是很少的一部分,为了方便理解上下文,我贴了完整代码,接下来处理CRUD+cache
通过调用`transformer`的`Shorten`方法实现url到短链的变换
至此,API Gateway修改完成,虽然贴的代码多,但是期中修改的是很少的一部分,为了方便理解上下文,我贴了完整代码,接下来处理CRUD+cache

## 8. 定义数据库表结构,并生成CRUD+cache代码

Expand Down Expand Up @@ -514,6 +512,10 @@

可以看出在我的MacBook Pro上能达到3万+的qps。

## 12. 完整代码

[https://github.com/tal-tech/go-zero/tree/master/example/shorturl](https://github.com/tal-tech/go-zero/tree/master/example/shorturl)

## 12. 总结

我们一直强调**工具大于约定和文档**。
Expand Down
5 changes: 5 additions & 0 deletions example/shorturl/api/etc/shorturl-api.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Name: shorturl-api
Host: 0.0.0.0
Port: 8888
Transform:
Etcd:
Hosts:
- localhost:2379
Key: transform.rpc
6 changes: 5 additions & 1 deletion example/shorturl/api/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package config

import "github.com/tal-tech/go-zero/rest"
import (
"github.com/tal-tech/go-zero/rest"
"github.com/tal-tech/go-zero/rpcx"
)

type Config struct {
rest.RestConf
Transform rpcx.RpcClientConf
}
12 changes: 11 additions & 1 deletion example/shorturl/api/internal/logic/expandlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"shorturl/rpc/transform/transformer"

"github.com/tal-tech/go-zero/core/logx"
)
Expand All @@ -24,5 +25,14 @@ func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic
}

func (l *ExpandLogic) Expand(req types.ExpandReq) (*types.ExpandResp, error) {
return &types.ExpandResp{}, nil
resp, err := l.svcCtx.Transformer.Expand(l.ctx, &transformer.ExpandReq{
Shorten: req.Shorten,
})
if err != nil {
return nil, err
}

return &types.ExpandResp{
Url: resp.Url,
}, nil
}
12 changes: 11 additions & 1 deletion example/shorturl/api/internal/logic/shortenlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"shorturl/api/internal/svc"
"shorturl/api/internal/types"
"shorturl/rpc/transform/transformer"

"github.com/tal-tech/go-zero/core/logx"
)
Expand All @@ -24,5 +25,14 @@ func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLog
}

func (l *ShortenLogic) Shorten(req types.ShortenReq) (*types.ShortenResp, error) {
return &types.ShortenResp{}, nil
resp, err := l.svcCtx.Transformer.Shorten(l.ctx, &transformer.ShortenReq{
Url: req.Url,
})
if err != nil {
return nil, err
}

return &types.ShortenResp{
Shorten: resp.Shorten,
}, nil
}
15 changes: 12 additions & 3 deletions example/shorturl/api/internal/svc/servicecontext.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package svc

import "shorturl/api/internal/config"
import (
"shorturl/api/internal/config"
"shorturl/rpc/transform/transformer"

"github.com/tal-tech/go-zero/rpcx"
)

type ServiceContext struct {
Config config.Config
Config config.Config
Transformer transformer.Transformer
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{Config: c}
return &ServiceContext{
Config: c,
Transformer: transformer.NewTransformer(rpcx.MustNewClient(c.Transform)),
}
}
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ go get -u github.com/tal-tech/go-zero

0. 完整示例请查看

[快速构建高并发微服务](doc/shorturl.md)
[快速构建高并发微服务](doc/shorturl.md)

[快速构建高并发微服务-多RPC服务版](doc/bookstore.md)

1. 安装goctl工具

Expand Down

0 comments on commit c0d0e00

Please sign in to comment.