diff --git a/SUMMARY.md b/SUMMARY.md index 99b7892..e5d7cb5 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/ch6-web/ch6-01-introduction.md b/ch5-web/ch5-01-introduction.md similarity index 99% rename from ch6-web/ch6-01-introduction.md rename to ch5-web/ch5-01-introduction.md index 0f220d7..4a41464 100644 --- a/ch6-web/ch6-01-introduction.md +++ b/ch5-web/ch5-01-introduction.md @@ -1,4 +1,4 @@ -# 6.1. web 开发简介 +# 5.1. web 开发简介 由于 golang 的 `net/http` 提供了基础的路由函数组合,并且也提供了丰富的功能函数。所以在 golang 社区里有一种观点认为用 golang 写 api 不需要框架。其看法也存在一定的道理,如果你的项目路由在个位数,URI 固定且不通过 URI 来传递参数,那么使用官方库也就足够。但在复杂场景下,官方的 http 库还是有些力不从心。例如下面这样的路由: diff --git a/ch6-web/ch6-02-router.md b/ch5-web/ch5-02-router.md similarity index 99% rename from ch6-web/ch6-02-router.md rename to ch5-web/ch5-02-router.md index 0c987b3..532560c 100644 --- a/ch6-web/ch6-02-router.md +++ b/ch5-web/ch5-02-router.md @@ -1,4 +1,4 @@ -# 6.2. router 请求路由 +# 5.2. router 请求路由 在常见的 web 框架中,router 是必备的组件。golang 圈子里 router 也时常被称为 http 的 multiplexer。在上一节中我们通过对 Burrow 代码的简单学习,已经知道如何用 http 标准库中内置的 mux 来完成简单的路由功能了。如果开发 web 系统对路径中带参数没什么兴趣的话,用 http 标准库中的 mux 就可以。 @@ -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。 diff --git a/ch6-web/ch6-03-middleware.md b/ch5-web/ch5-03-middleware.md similarity index 99% rename from ch6-web/ch6-03-middleware.md rename to ch5-web/ch5-03-middleware.md index a58413b..7dd0338 100644 --- a/ch6-web/ch6-03-middleware.md +++ b/ch5-web/ch5-03-middleware.md @@ -1,4 +1,4 @@ -# 6.3. middleware 中间件 +# 5.3. middleware 中间件 本章将对现在流行的 web 框架中的中间件技术原理进行分析,并介绍如何使用中间件技术将业务和非业务代码功能进行解耦。 @@ -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 diff --git a/ch6-web/ch6-04-validator.md b/ch5-web/ch5-04-validator.md similarity index 99% rename from ch6-web/ch6-04-validator.md rename to ch5-web/ch5-04-validator.md index 0fe67fe..bb3c054 100644 --- a/ch6-web/ch6-04-validator.md +++ b/ch5-web/ch5-04-validator.md @@ -1,4 +1,4 @@ -# 6.4. validator 请求校验 +# 5.4. validator 请求校验 社区里曾经有人用这张图来嘲笑 PHP: diff --git a/ch6-web/ch6-05-database.md b/ch5-web/ch5-05-database.md similarity index 99% rename from ch6-web/ch6-05-database.md rename to ch5-web/ch5-05-database.md index 54ed203..0e78d8b 100644 --- a/ch6-web/ch6-05-database.md +++ b/ch5-web/ch5-05-database.md @@ -1,4 +1,4 @@ -# 6.4. Database 和数据库打交道 +# 5.4. Database 和数据库打交道 本节将对 db/sql 官方标准库作一些简单分析,并介绍一些应用比较广泛的开源 orm 和 sql builder。并从企业级应用开发和公司架构的角度来分析哪种技术栈对于现代的企业级应用更为合适。 @@ -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 想从设计上隐去太多的细节。而方便的代价是其背后的运行完全失控。这样的项目在经过几任维护人员之后,将变得面目全非,难以维护。 @@ -240,4 +240,4 @@ func GetAllByProductIDsAndCustomerID(ctx context.Context, productIDs []uint64, c 像这样的代码,在上线之前把 dao 层的变更集的 const 部分直接拿给 dba 来进行审核,就比较方便了。代码中的 sqlutil.Named 是类似于 sqlx 中的 Named 函数,同时支持 where 表达式中的比较操作符和 in。 -这里为了说明简便,函数写得稍微复杂一些,仔细思考一下的话查询的导出函数还可以进一步进行简化。请读者朋友们自行尝试。 \ No newline at end of file +这里为了说明简便,函数写得稍微复杂一些,仔细思考一下的话查询的导出函数还可以进一步进行简化。请读者朋友们自行尝试。 diff --git a/ch6-web/ch6-06-rpc.md b/ch5-web/ch5-06-rpc.md similarity index 100% rename from ch6-web/ch6-06-rpc.md rename to ch5-web/ch5-06-rpc.md diff --git a/ch5-web/ch5-07-ratelimit.md b/ch5-web/ch5-07-ratelimit.md new file mode 100644 index 0000000..53f9f4d --- /dev/null +++ b/ch5-web/ch5-07-ratelimit.md @@ -0,0 +1 @@ +# 5.7. Ratelimit 服务流量限制 diff --git a/ch6-web/ch6-08-layout-of-web-project.md b/ch5-web/ch5-08-layout-of-web-project.md similarity index 99% rename from ch6-web/ch6-08-layout-of-web-project.md rename to ch5-web/ch5-08-layout-of-web-project.md index 281a57c..797760e 100644 --- a/ch6-web/ch6-08-layout-of-web-project.md +++ b/ch5-web/ch5-08-layout-of-web-project.md @@ -1,4 +1,4 @@ -# 6.8. layout 常见大型 web 项目分层 +# 5.8. layout 常见大型 web 项目分层 流行的 web 框架大多数是 MVC 框架,MVC 这个概念最早由 Trygve Reenskaug 在 1978 年提出,为了能够对 GUI 类型的应用进行方便扩展,将程序划分为: diff --git a/ch6-web/ch6-09-interface-and-web.md b/ch5-web/ch5-09-interface-and-web.md similarity index 93% rename from ch6-web/ch6-09-interface-and-web.md rename to ch5-web/ch5-09-interface-and-web.md index 807464f..1ace33c 100644 --- a/ch6-web/ch6-09-interface-and-web.md +++ b/ch5-web/ch5-09-interface-and-web.md @@ -1,4 +1,4 @@ -# 6.8. interface 和 web 编程 +# 5.8. interface 和 web 编程 在项目中我们有可能遇到这样的场景:公司内的基础架构因为技术实力原因,最早是从别人那里借来的 kv 存储方案。随着公司的发展,渐渐有大牛加入,想要甩掉这个借来的包袱自研 kv 存储,但接口与之前的 kv 存储不兼容。接入时需要业务改动接入代码,怎么写代码才能让我的核心业务逻辑不受这些外部资源变化影响呢。 diff --git a/ch5-web/ch5-10-dist-config.md b/ch5-web/ch5-10-dist-config.md new file mode 100644 index 0000000..d321488 --- /dev/null +++ b/ch5-web/ch5-10-dist-config.md @@ -0,0 +1 @@ +# 5.8. Dist-config 分布式配置服务 diff --git a/ch6-web/ch6-11-service-discovery.md b/ch5-web/ch5-11-service-discovery.md similarity index 99% rename from ch6-web/ch6-11-service-discovery.md rename to ch5-web/ch5-11-service-discovery.md index a5aa1eb..8429ff5 100644 --- a/ch6-web/ch6-11-service-discovery.md +++ b/ch5-web/ch5-11-service-discovery.md @@ -1,4 +1,4 @@ -# 6.11. Service Discovery 服务发现 +# 5.11. Service Discovery 服务发现 在微服务架构中,服务之间是存在依赖的。例如在订单系统中创建订单时,需要对用户信息做快照,这时候也就意味着这个流程要依赖: 订单、用户两个系统。当前大型网站的语境下,多服务分布式共存,单个服务也可能会跑在多台物理/虚拟机上。所以即使你知道你需要依赖的是“订单服务”这个具体的服务,实际面对的仍然是多个 ip+port 组成的集群。因此你需要: 1. 通过“订单服务”这个名字找到它对应的 ip+port 列表;2. 决定把这个请求发到哪一个 ip+port 上的订单服务。 @@ -84,4 +84,4 @@ TODO,这里是 go-zookeeper 的临时节点使用 demo。 ls /platform/order-system/create-order-service-http [] -``` \ No newline at end of file +``` diff --git a/ch6-web/ch6-12-load-balance.md b/ch5-web/ch5-12-load-balance.md similarity index 99% rename from ch6-web/ch6-12-load-balance.md rename to ch5-web/ch5-12-load-balance.md index c691ff1..f0d7bf0 100644 --- a/ch6-web/ch6-12-load-balance.md +++ b/ch5-web/ch5-12-load-balance.md @@ -1,4 +1,4 @@ -# 6.12. Load-Balance 负载均衡 +# 5.12. Load-Balance 负载均衡 本节将会讨论常见的 web 后端服务之间的负载均衡手段。 diff --git a/ch5-web/ch5-13-circuit-breaker.md b/ch5-web/ch5-13-circuit-breaker.md new file mode 100644 index 0000000..1425217 --- /dev/null +++ b/ch5-web/ch5-13-circuit-breaker.md @@ -0,0 +1 @@ +# 5.9. Circuit-Breaker 熔断保护 diff --git a/ch5-web/ch5-14-monitor.md b/ch5-web/ch5-14-monitor.md new file mode 100644 index 0000000..1707c15 --- /dev/null +++ b/ch5-web/ch5-14-monitor.md @@ -0,0 +1 @@ +# 5.8. Monitor metrics 和服务监控 diff --git a/ch6-web/ch6-15-extend.md b/ch5-web/ch5-15-extend.md similarity index 100% rename from ch6-web/ch6-15-extend.md rename to ch5-web/ch5-15-extend.md diff --git a/ch6-web/ch6-16-test-and-mock.md b/ch5-web/ch5-16-test-and-mock.md similarity index 100% rename from ch6-web/ch6-16-test-and-mock.md rename to ch5-web/ch5-16-test-and-mock.md diff --git a/ch6-web/readme.md b/ch5-web/readme.md similarity index 92% rename from ch6-web/readme.md rename to ch5-web/readme.md index d92531b..3707858 100644 --- a/ch6-web/readme.md +++ b/ch5-web/readme.md @@ -1,4 +1,4 @@ -# 第六章 go 和 web +# 第五章 go 和 web 本章将会阐述 go 在 web 开发方面的现状,并以几个典型的开源 web 框架为例,带大家深入 web 框架本身的执行流程。 diff --git a/ch5-wtf/readme.md b/ch5-wtf/readme.md deleted file mode 100644 index bc60b20..0000000 --- a/ch5-wtf/readme.md +++ /dev/null @@ -1,3 +0,0 @@ -# 第五章 这是一个坑 - -TODO diff --git a/ch6-web/ch6-07-ratelimit.md b/ch6-web/ch6-07-ratelimit.md deleted file mode 100644 index 1afe162..0000000 --- a/ch6-web/ch6-07-ratelimit.md +++ /dev/null @@ -1 +0,0 @@ -# 6.7. Ratelimit 服务流量限制 diff --git a/ch6-web/ch6-10-dist-config.md b/ch6-web/ch6-10-dist-config.md deleted file mode 100644 index af8adc7..0000000 --- a/ch6-web/ch6-10-dist-config.md +++ /dev/null @@ -1 +0,0 @@ -# 6.8. Dist-config 分布式配置服务 \ No newline at end of file diff --git a/ch6-web/ch6-13-circuit-breaker.md b/ch6-web/ch6-13-circuit-breaker.md deleted file mode 100644 index 29bacb3..0000000 --- a/ch6-web/ch6-13-circuit-breaker.md +++ /dev/null @@ -1 +0,0 @@ -# 6.9. Circuit-Breaker 熔断保护 diff --git a/ch6-web/ch6-14-monitor.md b/ch6-web/ch6-14-monitor.md deleted file mode 100644 index 2262563..0000000 --- a/ch6-web/ch6-14-monitor.md +++ /dev/null @@ -1 +0,0 @@ -# 6.8. Monitor metrics 和服务监控