mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
update example doc
This commit is contained in:
parent
b4aa89fc25
commit
c0d0e00803
@ -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. 总结
|
||||
|
||||
我们一直强调**工具大于约定和文档**。
|
||||
|
||||
|
@ -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)), // 手动代码
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
@ -341,10 +339,10 @@
|
||||
// 手动代码结束
|
||||
}
|
||||
```
|
||||
|
||||
通过调用`transformer`的`Shorten`方法实现url到短链的变换
|
||||
|
||||
至此,API Gateway修改完成,虽然贴的代码多,但是期中修改的是很少的一部分,为了方便理解上下文,我贴了完整代码,接下来处理CRUD+cache
|
||||
|
||||
通过调用`transformer`的`Shorten`方法实现url到短链的变换
|
||||
|
||||
至此,API Gateway修改完成,虽然贴的代码多,但是期中修改的是很少的一部分,为了方便理解上下文,我贴了完整代码,接下来处理CRUD+cache
|
||||
|
||||
## 8. 定义数据库表结构,并生成CRUD+cache代码
|
||||
|
||||
@ -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. 总结
|
||||
|
||||
我们一直强调**工具大于约定和文档**。
|
||||
|
@ -1,3 +1,8 @@
|
||||
Name: shorturl-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
Transform:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- localhost:2379
|
||||
Key: transform.rpc
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
)
|
||||
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
)
|
||||
@ -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
|
||||
}
|
||||
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user