Skip to content
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
26 changes: 13 additions & 13 deletions go-zero.dev/cn/micro-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $ go mod init go-zero-demo
```shell
$ vim mall/user/rpc/user.proto
```

增加如下代码:

```protobuf
Expand Down Expand Up @@ -220,20 +220,20 @@ $ go mod init go-zero-demo

import (
"go-zero-demo/mall/order/api/internal/config"
"go-zero-demo/mall/user/rpc/user"
"go-zero-demo/mall/user/rpc/userclient"

"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
Config config.Config
UserRpc user.User
UserRpc userclient.User
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
UserRpc: user.NewUser(zrpc.MustNewClient(c.UserRpc)),
UserRpc: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
}
}
```
Expand All @@ -248,44 +248,44 @@ $ go mod init go-zero-demo

```go
package logic

import (
"context"
"errors"

"go-zero-demo/mall/order/api/internal/svc"
"go-zero-demo/mall/order/api/internal/types"
"go-zero-demo/mall/user/rpc/types/user"

"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetOrderLogic {
return GetOrderLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *GetOrderLogic) GetOrder(req *types.OrderReq) (*types.OrderReply, error) {
user, err := l.svcCtx.UserRpc.GetUser(l.ctx, &user.IdRequest{
Id: "1",
})
if err != nil {
return nil, err
}

if user.Name != "test" {
return nil, errors.New("用户不存在")
}

return &types.OrderReply{
Id: req.Id,
Name: "test order",
Expand Down Expand Up @@ -324,7 +324,7 @@ $ go mod init go-zero-demo
Content-Type: application/json
Date: Sun, 07 Feb 2021 03:45:05 GMT
Content-Length: 30

{"id":"1","name":"test order"}
```

Expand Down
28 changes: 14 additions & 14 deletions go-zero.dev/en/micro-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,15 @@ $ go mod init go-zero-demo
```shell
$ goctl api go -api order.api -dir .
Done.
```
```
* Add user rpc configuration

```shell
$ vim internal/config/config.go
```
```go
package config

import (
"github.com/zeromicro/go-zero/zrpc"
"github.com/zeromicro/go-zero/rest"
Expand Down Expand Up @@ -214,20 +214,20 @@ $ go mod init go-zero-demo

import (
"go-zero-demo/mall/order/api/internal/config"
"go-zero-demo/mall/user/rpc/user"
"go-zero-demo/mall/user/rpc/userclient"

"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
Config config.Config
UserRpc user.User
UserRpc userclient.User
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
UserRpc: user.NewUser(zrpc.MustNewClient(c.UserRpc)),
UserRpc: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
}
}
```
Expand All @@ -241,44 +241,44 @@ $ go mod init go-zero-demo

```go
package logic

import (
"context"
"errors"

"go-zero-demo/mall/order/api/internal/svc"
"go-zero-demo/mall/order/api/internal/types"
"go-zero-demo/mall/user/rpc/types/user"

"github.com/zeromicro/go-zero/core/logx"
)

type GetOrderLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewGetOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetOrderLogic {
return GetOrderLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *GetOrderLogic) GetOrder(req types.OrderReq) (*types.OrderReply, error) {
user, err := l.svcCtx.UserRpc.GetUser(l.ctx, &user.IdRequest{
Id: "1",
})
if err != nil {
return nil, err
}

if user.Name != "test" {
return nil, errors.New("用户不存在")
}

return &types.OrderReply{
Id: req.Id,
Name: "test order",
Expand Down Expand Up @@ -317,7 +317,7 @@ $ go mod init go-zero-demo
Content-Type: application/json
Date: Sun, 07 Feb 2021 03:45:05 GMT
Content-Length: 30

{"id": "1", "name": "test order"}
```

Expand Down