1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-24 20:52:22 +00:00

Merge pull request #377 from LaoK996/master

update 5.5
This commit is contained in:
Xargin 2018-12-01 21:47:18 +08:00 committed by GitHub
commit 3fe7a61958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 7 deletions

View File

@ -60,7 +60,7 @@ import (
func main() { func main() {
// db 是一个 sql.DB 类型的对象 // db 是一个 sql.DB 类型的对象
// 该对象线程安全,且内部已包含了一个连接池 // 该对象线程安全,且内部已包含了一个连接池
// 连接池的选项可以在 sql.Open 中设置,这里为了简单省略了 // 连接池的选项可以在 sql.DB 的方法中设置,这里为了简单省略了
db, err := sql.Open("mysql", db, err := sql.Open("mysql",
"user:password@tcp(127.0.0.1:3306)/hello") "user:password@tcp(127.0.0.1:3306)/hello")
if err != nil { if err != nil {
@ -79,7 +79,8 @@ func main() {
defer rows.Close() defer rows.Close()
// 必须要把 rows 里的内容读完,否则连接永远不会释放 // 必须要把 rows 里的内容读完,或者显式调用 Close() 方法,
// 否则在 defer 的 rows.Close() 执行之前,连接永远不会释放
for rows.Next() { for rows.Next() {
err := rows.Scan(&id, &name) err := rows.Scan(&id, &name)
if err != nil { if err != nil {

View File

@ -18,7 +18,7 @@
2. Logic/Service逻辑(服务)层,一般是业务逻辑的入口,可以认为从这里开始,所有的请求参数一定是合法的。业务逻辑和业务流程也都在这一层中。常见的设计中会将该层称为 Business Rules。 2. Logic/Service逻辑(服务)层,一般是业务逻辑的入口,可以认为从这里开始,所有的请求参数一定是合法的。业务逻辑和业务流程也都在这一层中。常见的设计中会将该层称为 Business Rules。
3. DAO/Repository这一层主要负责和数据、存储打交道。将下层存储以更简单的函数、接口形式暴露给 Logic 层来使用。负责数据的持久化工作。 3. DAO/Repository这一层主要负责和数据、存储打交道。将下层存储以更简单的函数、接口形式暴露给 Logic 层来使用。负责数据的持久化工作。
每一层都会做好自己的工作,然后用请求当前的上下文构造下一层工作所需要的结构体或其它类型参数,然后调用下一的函数。在工作完成之后,再把处理结果一层层地传出到入口。 每一层都会做好自己的工作,然后用请求当前的上下文构造下一层工作所需要的结构体或其它类型参数,然后调用下一的函数。在工作完成之后,再把处理结果一层层地传出到入口。
![controller-logic-dao](../images/ch6-08-controller-logic-dao.png) ![controller-logic-dao](../images/ch6-08-controller-logic-dao.png)

View File

@ -13,7 +13,7 @@
## 5.9.1 通过分批次部署实现灰度发布 ## 5.9.1 通过分批次部署实现灰度发布
假如服务部署在 15 个实例(可能是物理机,也可能是容器)上,我们把这 7 个实例分为三组,按照先后顺序,分别有 1-2-4-8 台机器,保证每次扩展时大概都是二倍的关系。 假如服务部署在 15 个实例(可能是物理机,也可能是容器)上,我们把这 15 个实例分为四组,按照先后顺序,分别有 1-2-4-8 台机器,保证每次扩展时大概都是二倍的关系。
``` ```
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐
@ -149,7 +149,7 @@ func init() {
readConfig() readConfig()
for i:=0;i<len(cityID2Open);i++ { for i:=0;i<len(cityID2Open);i++ {
if city i is opened in configs { if city i is opened in configs {
cityID2Open = true cityID2Open[i] = true
} }
} }
} }

View File

@ -84,7 +84,7 @@ node := getNodeFromPool()
resp, err := remoteRPC(ctx, params) resp, err := remoteRPC(ctx, params)
if err != nil { if err == nil {
node.Vote(status.Healthy) node.Vote(status.Healthy)
} else { } else {
node.Vote(status.Unhealthy) node.Vote(status.Unhealthy)
@ -103,9 +103,10 @@ healthcheck := func(endpoint string) {
resp, err := callRemoteHealthcheckAPI(endpoint) resp, err := callRemoteHealthcheckAPI(endpoint)
if err != nil { if err != nil {
dropThisAPINode() dropThisAPINode()
break
}
} }
} }
}()
for _, endpoint := range endpointList { for _, endpoint := range endpointList {
go healthcheck(endpoint) go healthcheck(endpoint)