Skip to content

Commit

Permalink
feat: proxima book
Browse files Browse the repository at this point in the history
  • Loading branch information
oldme-git committed Dec 16, 2024
1 parent ed45541 commit dccad1b
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ slug: '/course/proxima-book/about-arch'
## 代码仓库
---
微服务将单体服务拆开,代码也自然分离。它的代码仓库,有两种常见的管理方式:
- **Polyrepo** 多仓库模式,每个微服务都有独立的仓库。优点是每个仓库相对较小,易于管理。缺点是需要额外的工具,流程协调各个服务之间的依赖和版本。
- **Multirepo** 多仓库模式,每个微服务都有独立的仓库。优点是每个仓库相对较小,易于管理。缺点是需要额外的工具,流程协调各个服务之间的依赖和版本。
- **Monorepo:** 单一仓库模式,所有微服务的代码都存放在一个仓库中。优点是可以统一管理版本和依赖,缺点是仓库可能会变得庞大,管理复杂度增加。

我们的项目采用`Monorepo`模式。`Polyrepo`模式下一个服务一个目录,无需多言。
我们的项目采用`Monorepo`模式。`Multirepo`模式下一个服务一个目录,无需多言。
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ slug: '/course/proxima-book/word-protocol'

*app/word/manifest/protobuf/words/v1/words.proto*
```proto
// protoc --go_out=plugins=grpc:. *.proto
syntax = "proto3";
package words.v1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ slug: '/course/proxima-book/user-protocol'

*app/user/manifest/protobuf/account/v1/account.proto*
```proto
// protoc --go_out=plugins=grpc:. *.proto
syntax = "proto3";
package account.v1;
Expand Down Expand Up @@ -62,8 +60,6 @@ message UserRegisterRes {

*app/user/manifest/protobuf/account/v1/account.proto*
```proto
// protoc --go_out=plugins=grpc:. *.proto
syntax = "proto3";
package account.v1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ slug: /course/proxima-book/appendix
---
- 用户认证授权:在微服务中完善用户认证授权,并在网关服务中使用它;
- 调用多个微服务:在同一个控制器中,调用多个微服务完成业务功能;
- 中间件/拦截器:使用`GoFrame`提供的`gRPC`中间件
- 服务端拦截器:使用`GoFrame`提供的`gRPC`服务端拦截器
- 负载策略:了解微服务负载均衡;
- 服务配置管理:对接一些配置中心,为微服务提供更灵活的配置方式。

Expand Down
139 changes: 139 additions & 0 deletions docs/course/proxima-book/第四章-业务网关/4.4.gRPC客户端.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
title: "4.4.封装gRPC客户端"
hide_title: true
slug: '/course/proxima-book/gateway-client'
---

## 客户端
---
业务网关相当于`gRPC`客户端,各个微服务相当于`gRPC`服务端。我们将在控制器属性里,定义`gRPC client`,供后续使用。

定义`client``grpcx.Client.MustNewGrpcClientConn(service, opts...)`完成。

*app/gateway/internal/controller/user/user_new.go*
```go
package user

import (
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"proxima/app/gateway/api/user"
v1 "proxima/app/user/api/account/v1"
)

type ControllerV1 struct {
AccountClient v1.AccountClient
}

func NewV1() user.IUserV1 {
var conn = grpcx.Client.MustNewGrpcClientConn("user")

return &ControllerV1{
AccountClient: v1.NewAccountClient(conn),
}
}
```

*app/gateway/internal/controller/words/words_new.go*
```go
package words

import (
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"proxima/app/gateway/api/words"
v1 "proxima/app/word/api/words/v1"
)

type ControllerV1 struct {
WordsClient v1.WordsClient
}

func NewV1() words.IWordsV1 {
var conn = grpcx.Client.MustNewGrpcClientConn("word")

return &ControllerV1{
WordsClient: v1.NewWordsClient(conn),
}
}
```

## 拦截器
---
当前客户端没有超时处理,`gRPC`的默认超时时间阈值又非常大。如果`gRPC`服务端、`etcd`服务,或者网络出现异常,业务网关会无限卡死。我们来添加超时拦截器,以应对这种情况。

### 定义拦截器
超时机制很简单,通过`Go`的上下文提供。

*app/gateway/utility/grpc.go*
```go
package utility

import (
"context"
"time"
"google.golang.org/grpc"
)

func GrpcClientTimeout(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()

err := invoker(ctx, method, req, reply, cc, opts...)
return err
}
```

### 调用拦截器

*app/gateway/internal/controller/user/user_new.go*
```go
package user

import (
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"proxima/app/gateway/api/user"
"proxima/app/gateway/utility"
v1 "proxima/app/user/api/account/v1"
)

type ControllerV1 struct {
AccountClient v1.AccountClient
}

func NewV1() user.IUserV1 {
var conn = grpcx.Client.MustNewGrpcClientConn("user", grpcx.Client.ChainUnary(
utility.GrpcClientTimeout,
))

return &ControllerV1{
AccountClient: v1.NewAccountClient(conn),
}
}
```

*app/gateway/internal/controller/words/words_new.go*
```go
package words

import (
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"proxima/app/gateway/api/words"
"proxima/app/gateway/utility"
v1 "proxima/app/word/api/words/v1"
)

type ControllerV1 struct {
WordsClient v1.WordsClient
}

func NewV1() words.IWordsV1 {
var conn = grpcx.Client.MustNewGrpcClientConn("word", grpcx.Client.ChainUnary(
utility.GrpcClientTimeout,
))

return &ControllerV1{
WordsClient: v1.NewWordsClient(conn),
}
}
```

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,13 @@ package user
import (
"context"

"proxima/app/gateway/utility"
account "proxima/app/user/api/account/v1"

"proxima/app/gateway/api/user/v1"
)

func (c *ControllerV1) Login(ctx context.Context, req *v1.LoginReq) (res *v1.LoginRes, err error) {
var (
conn = utility.UserClientConn()
client = account.NewAccountClient(conn)
)

ctx, cancel := context.WithTimeout(ctx, utility.Timeout)
defer cancel()

user, err := client.UserLogin(ctx, &account.UserLoginReq{
user, err := c.AccountClient.UserLogin(ctx, &account.UserLoginReq{
Username: req.Username,
Password: req.Password,
})
Expand Down Expand Up @@ -76,22 +67,13 @@ package words
import (
"context"

"proxima/app/gateway/utility"
words "proxima/app/word/api/words/v1"

"proxima/app/gateway/api/words/v1"
)

func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
var (
conn = utility.WordClientConn()
client = words.NewWordsClient(conn)
)

ctx, cancel := context.WithTimeout(ctx, utility.Timeout)
defer cancel()

_, err = client.Create(ctx, &words.CreateReq{
_, err = c.WordsClient.Create(ctx, &words.CreateReq{
Uid: 1,
Word: req.Word,
Definition: req.Definition,
Expand All @@ -113,21 +95,13 @@ import (
"context"

"github.com/gogf/gf/v2/errors/gerror"
"proxima/app/gateway/utility"
words "proxima/app/word/api/words/v1"

"proxima/app/gateway/api/words/v1")
"proxima/app/gateway/api/words/v1"
)

func (c *ControllerV1) Detail(ctx context.Context, req *v1.DetailReq) (res *v1.DetailRes, err error) {
var (
conn = utility.WordClientConn()
client = words.NewWordsClient(conn)
)

ctx, cancel := context.WithTimeout(ctx, utility.Timeout)
defer cancel()

word, err := client.Get(ctx, &words.GetReq{
word, err := c.WordsClient.Get(ctx, &words.GetReq{
Id: uint32(req.Id),
})

Expand Down

0 comments on commit dccad1b

Please sign in to comment.