Skip to content

Commit

Permalink
ch6 移到 ch5
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Jun 8, 2018
1 parent e7e8c45 commit 45970e7
Show file tree
Hide file tree
Showing 23 changed files with 27 additions and 31 deletions.
17 changes: 8 additions & 9 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@
* [4.6. Protobuf扩展(TODO)](ch4-rpc/ch4-06-pb-option.md)
* [4.7. 基于pb的rpc定制(TODO)](ch4-rpc/ch4-07-pb-rpc.md)
* [4.8. 补充说明(TODO)](ch4-rpc/ch4-08-faq.md)
* [第五章 这是一个坑(TODO)](ch5-wtf/readme.md)
* [第六章 Go和Web](ch6-web/readme.md)
* [6.1. Web开发简介](ch6-web/ch6-01-introduction.md)
* [6.2. Router请求路由](ch6-web/ch6-02-router.md)
* [6.3. Middleware中间件](ch6-web/ch6-03-middleware.md)
* [6.4. Validator请求校验](ch6-web/ch6-04-validator.md)
* [6.5. Database和数据库打交道](ch6-web/ch6-05-database.md)
* [6.8. Layout大型web项目分层](ch6-web/ch6-08-layout-of-web-project.md)
* [6.12. Load-balance负载均衡](ch6-web/ch6-12-load-balance.md)
* [第五章 Go和Web](ch5-web/readme.md)
* [5.1. Web开发简介](ch5-web/ch5-01-introduction.md)
* [5.2. Router请求路由](ch5-web/ch5-02-router.md)
* [5.3. Middleware中间件](ch5-web/ch5-03-middleware.md)
* [5.4. Validator请求校验](ch5-web/ch5-04-validator.md)
* [5.5. Database和数据库打交道](ch5-web/ch5-05-database.md)
* [5.8. Layout大型web项目分层](ch5-web/ch5-08-layout-of-web-project.md)
* [5.12. Load-balance负载均衡](ch5-web/ch5-12-load-balance.md)
* [第七章 Go和ast](ch7-ast/readme.md)
* [第八章 Go和那些生产力工具](ch8-tools/readme.md)
* [附录](appendix/readme.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.1. web 开发简介
# 5.1. web 开发简介

由于 golang 的 `net/http` 提供了基础的路由函数组合,并且也提供了丰富的功能函数。所以在 golang 社区里有一种观点认为用 golang 写 api 不需要框架。其看法也存在一定的道理,如果你的项目路由在个位数,URI 固定且不通过 URI 来传递参数,那么使用官方库也就足够。但在复杂场景下,官方的 http 库还是有些力不从心。例如下面这样的路由:

Expand Down
4 changes: 2 additions & 2 deletions ch6-web/ch6-02-router.md → ch5-web/ch5-02-router.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.2. router 请求路由
# 5.2. router 请求路由

在常见的 web 框架中,router 是必备的组件。golang 圈子里 router 也时常被称为 http 的 multiplexer。在上一节中我们通过对 Burrow 代码的简单学习,已经知道如何用 http 标准库中内置的 mux 来完成简单的路由功能了。如果开发 web 系统对路径中带参数没什么兴趣的话,用 http 标准库中的 mux 就可以。

Expand Down Expand Up @@ -200,7 +200,7 @@ indices: 子节点索引,当子节点为非参数类型,即本节点的 wild

子节点的冲突处理很简单,分几种情况:

1. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildChild 被设置为 false。例如:`GET /user/getAll``GET /user/:id/getAddr`,或者 `GET /user/*aaa``GET /user/:id`
1. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildChild 被设置为 false。例如:`GET /user/getAll``GET /user/:id/getAddr`,或者 `GET /user/*aaa``GET /user/:id`
2. 在插入 wildcard 节点时,父节点的 children 数组非空且 wildChild 被设置为 true,但该父节点的 wildcard 子节点要插入的 wildcard 名字不一样。例如:`GET /user/:id/info``GET /user/:name/info`
3. 在插入 catchAll 节点时,父节点的 children 非空。例如:`GET /src/abc``GET /src/*filename`,或者 `GET /src/:id``GET /src/*filename`
4. 在插入 static 节点时,父节点的 wildChild 字段被设置为 true。
Expand Down
4 changes: 2 additions & 2 deletions ch6-web/ch6-03-middleware.md → ch5-web/ch5-03-middleware.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.3. middleware 中间件
# 5.3. middleware 中间件

本章将对现在流行的 web 框架中的中间件技术原理进行分析,并介绍如何使用中间件技术将业务和非业务代码功能进行解耦。

Expand Down Expand Up @@ -267,7 +267,7 @@ logger.go
profiler.go
=> 挂载 pprof 需要的路由,如 /pprof、/pprof/trace 到系统中
realip.go
=> 从请求头中读取 X-Forwarded-For 和 X-Real-IP,将 http.Request 中的 RemoteAddr 修改为得到的 RealIP
=> 从请求头中读取 X-Forwarded-For 和 X-Real-IP,将 http.Request 中的 RemoteAddr 修改为得到的 RealIP
requestid.go
=> 为本次请求生成单独的 requestid,可一路透传,用来生成分布式调用链路,也可用于在日志中串连单次请求的所有逻辑
timeout.go
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.4. validator 请求校验
# 5.4. validator 请求校验

社区里曾经有人用这张图来嘲笑 PHP:

Expand Down
6 changes: 3 additions & 3 deletions ch6-web/ch6-05-database.md → ch5-web/ch5-05-database.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.4. Database 和数据库打交道
# 5.4. Database 和数据库打交道

本节将对 db/sql 官方标准库作一些简单分析,并介绍一些应用比较广泛的开源 orm 和 sql builder。并从企业级应用开发和公司架构的角度来分析哪种技术栈对于现代的企业级应用更为合适。

Expand Down Expand Up @@ -151,7 +151,7 @@ num, err := o.QueryTable("cardgroup").Filter("Cards__Card__Name", cardName).All(
除了 limit 的问题,我们再看一遍这个 beego orm 的查询:

```go
num, err := o.QueryTable("cardgroup").Filter("Cards__Card__Name", cardName).All(&cardgroups)
num, err := o.QueryTable("cardgroup").Filter("Cards__Card__Name", cardName).All(&cardgroups)
```

你可以看得出来这个 Filter 是有表 join 的操作么?当然了,对 beego orm 有过深入使用经验的用户还是会觉得这是在吹毛求疵。但这样的分析想证明的是,orm 想从设计上隐去太多的细节。而方便的代价是其背后的运行完全失控。这样的项目在经过几任维护人员之后,将变得面目全非,难以维护。
Expand Down Expand Up @@ -240,4 +240,4 @@ func GetAllByProductIDsAndCustomerID(ctx context.Context, productIDs []uint64, c

像这样的代码,在上线之前把 dao 层的变更集的 const 部分直接拿给 dba 来进行审核,就比较方便了。代码中的 sqlutil.Named 是类似于 sqlx 中的 Named 函数,同时支持 where 表达式中的比较操作符和 in。

这里为了说明简便,函数写得稍微复杂一些,仔细思考一下的话查询的导出函数还可以进一步进行简化。请读者朋友们自行尝试。
这里为了说明简便,函数写得稍微复杂一些,仔细思考一下的话查询的导出函数还可以进一步进行简化。请读者朋友们自行尝试。
File renamed without changes.
1 change: 1 addition & 0 deletions ch5-web/ch5-07-ratelimit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 5.7. Ratelimit 服务流量限制
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.8. layout 常见大型 web 项目分层
# 5.8. layout 常见大型 web 项目分层

流行的 web 框架大多数是 MVC 框架,MVC 这个概念最早由 Trygve Reenskaug 在 1978 年提出,为了能够对 GUI 类型的应用进行方便扩展,将程序划分为:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.8. interface 和 web 编程
# 5.8. interface 和 web 编程

在项目中我们有可能遇到这样的场景:公司内的基础架构因为技术实力原因,最早是从别人那里借来的 kv 存储方案。随着公司的发展,渐渐有大牛加入,想要甩掉这个借来的包袱自研 kv 存储,但接口与之前的 kv 存储不兼容。接入时需要业务改动接入代码,怎么写代码才能让我的核心业务逻辑不受这些外部资源变化影响呢。

1 change: 1 addition & 0 deletions ch5-web/ch5-10-dist-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 5.8. Dist-config 分布式配置服务
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.11. Service Discovery 服务发现
# 5.11. Service Discovery 服务发现

在微服务架构中,服务之间是存在依赖的。例如在订单系统中创建订单时,需要对用户信息做快照,这时候也就意味着这个流程要依赖: 订单、用户两个系统。当前大型网站的语境下,多服务分布式共存,单个服务也可能会跑在多台物理/虚拟机上。所以即使你知道你需要依赖的是“订单服务”这个具体的服务,实际面对的仍然是多个 ip+port 组成的集群。因此你需要: 1. 通过“订单服务”这个名字找到它对应的 ip+port 列表;2. 决定把这个请求发到哪一个 ip+port 上的订单服务。

Expand Down Expand Up @@ -84,4 +84,4 @@ TODO,这里是 go-zookeeper 的临时节点使用 demo。
ls /platform/order-system/create-order-service-http

[]
```
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 6.12. Load-Balance 负载均衡
# 5.12. Load-Balance 负载均衡

本节将会讨论常见的 web 后端服务之间的负载均衡手段。

Expand Down
1 change: 1 addition & 0 deletions ch5-web/ch5-13-circuit-breaker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 5.9. Circuit-Breaker 熔断保护
1 change: 1 addition & 0 deletions ch5-web/ch5-14-monitor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 5.8. Monitor metrics 和服务监控
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion ch6-web/readme.md → ch5-web/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 第六章 go 和 web
# 第五章 go 和 web

本章将会阐述 go 在 web 开发方面的现状,并以几个典型的开源 web 框架为例,带大家深入 web 框架本身的执行流程。

Expand Down
3 changes: 0 additions & 3 deletions ch5-wtf/readme.md

This file was deleted.

1 change: 0 additions & 1 deletion ch6-web/ch6-07-ratelimit.md

This file was deleted.

1 change: 0 additions & 1 deletion ch6-web/ch6-10-dist-config.md

This file was deleted.

1 change: 0 additions & 1 deletion ch6-web/ch6-13-circuit-breaker.md

This file was deleted.

1 change: 0 additions & 1 deletion ch6-web/ch6-14-monitor.md

This file was deleted.

0 comments on commit 45970e7

Please sign in to comment.