Browse Source

update example doc

kevin 4 years ago
parent
commit
c0d0e00803

+ 5 - 1
doc/bookstore.md

@@ -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. 总结
 
 我们一直强调**工具大于约定和文档**。
 

+ 15 - 13
doc/shorturl.md

@@ -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. 总结
 
 我们一直强调**工具大于约定和文档**。

+ 5 - 0
example/shorturl/api/etc/shorturl-api.yaml

@@ -1,3 +1,8 @@
 Name: shorturl-api
 Host: 0.0.0.0
 Port: 8888
+Transform:
+  Etcd:
+    Hosts:
+      - localhost:2379
+    Key: transform.rpc

+ 5 - 1
example/shorturl/api/internal/config/config.go

@@ -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
 }

+ 11 - 1
example/shorturl/api/internal/logic/expandlogic.go

@@ -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
 }

+ 11 - 1
example/shorturl/api/internal/logic/shortenlogic.go

@@ -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
 }

+ 12 - 3
example/shorturl/api/internal/svc/servicecontext.go

@@ -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)),
+	}
 }

+ 3 - 1
readme.md

@@ -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工具