1. 安装

1
2
3
4
5
6
7
8
9
10
su - root
pip3 install supervisor

/usr/local/python37/bin/echo_supervisord_conf > /etc/supervisord.conf

#supervisord -c /etc/supervisord.conf

ln -s /usr/local/python37/bin/supervisord /usr/bin/supervisord

ln -s /usr/local/python37/bin/supervisorctl /usr/bin/supervisorctl

1. gRPC 回顾总结

1.1 gRPC

1
go get -u google.golang.org/grpc
  • 强大的 IDL,使用 Protocol Buffers 作为数据交换格式
  • 跨语言、跨平台
  • 支持HTTP2,双向传输、多路复用、认证等

grpc下常用包:

  • metadata: 提供方法对 grpc 元数据结构MD 进行获取和处理
  • credentials: 封装了客户端对服务端进行身份验证所需的所有状态,并做出各种断言
  • codes: grpc 标准错误码

1. RPC

1.1 什么是RPC

RPC: Remote Procedure Call,远程过程调用。调用过程包括传输协议和对象编码(序列化)。

1.2 RPC框架

  • 负载均衡
  • 服务注册和发现
  • 服务治理

1.3 为什么使用RPC

简单、通用、安全、效率

1. Go Modules

1.1 简介

Go Modules 是官方最新的包管理方式,它解决了如下问题:

  • 所有的依赖包必须在 GOPATH 下,但同一个库只能保存一个版本
  • 工作目录必须在 GOPATH/src 目录下

使用 Go Modules 之后,可在 GOPATH/src 之外创建目录和管理包

设置 go mod 和 go proxy:

1
2
3
4
5
go env -w GOBIN=/Users/eli/go/bin
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

go env

GO111MODULE:

  • on: 强制使用modules, 不再去GOPATH下查找
  • off: 不使用modules,去GOPATH和vendor下查找
  • auto: 默认值,如果当前目录下有go.mod文件,则使用modules

1. 入门

1.1 安装

1
go get -u github.com/go-redis/redis

1.2 初始化连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const (
REDIS_IP = "127.0.0.1"
REDIS_PORT = "6379"
REDIS_PWD = ""
REDIS_DB = 0
)

var (
ctx = context.Background()
rdb *redis.Client
)

func init() {
rdb = redis.NewClient(&redis.Options{
Addr: REDIS_IP + ":" + REDIS_PORT,
Password: REDIS_PWD,
DB: REDIS_DB,
PoolSize: 20,
})
}

1. 入门

1.1 安装

1
go get -u github.com/jinzhu/gorm

1.2 驱动

1
2
3
4
import _ "github.com/jinzhu/gorm/dialects/mysql"
import _ "github.com/jinzhu/gorm/dialects/postgres"
import _ "github.com/jinzhu/gorm/dialects/sqlite"
import _ "github.com/jinzhu/gorm/dialects/mssql"

1. Gin简介

1.1 核心术语

  • Engine: 实现 ServeHTTP 接口的 Handler
  • MethodTree: 根据http请求方法分别维护的路由树
  • RouterGroup:路由表分组,方便中间件统一处理
  • Context:上下文,在 Handler 之间传递参数

1.2 HttpRouter

gin 使用路由框架 httprouter,它使用动态压缩前缀树 (compact prefix trie) 或称基数树 (radix tree) ,具有共同前缀的节点拥有相同的父节点,内存开销极小,没有反射。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// router.go
type Router struct {
trees map[string]*node // 每种请求方法,单独管理一棵树
RedirectTrailingSlash bool // 自动处理URL尾部的 “/”
RedirectFixedPath bool // 路径矫正,如../和//
HandleMethodNotAllowed bool
HandleOPTIONS bool // 开启OPTIONS自动匹配, 手动匹配优先级更高
NotFound http.Handler
MethodNotAllowed http.Handler
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
}

// tree.go
type node struct {
path string
indices string // 分支的首字母:indices = eu,下面的 s [earch, upport]
wildChild bool // 是否为参数节点,参数节点用:name表示
nType nodeType // static:没有handler,root: 第一个插入的节点,catchAll: 有*匹配的节点,param: 参数节点如:post
priority uint32 // 子节点越多,或说绑定handle方法越多的节点,priority优先级越高
children []*node
handle Handle
}