go-zero必知必会

服务注册发现

一、服务注册发现分类

【阐明】:其余服务发现形式看https://github.com/zeromicro/...官网demo,其中yaml中,etcd、IP直连、k8s三种配置文件也不是对立的

默认形式etcd
  1. api配置文件
#gateway-api.yaml#api服务发现配置模块AdminRpc:  Timeout: 10000  Etcd:    Hosts:      - 127.0.0.1:2379    Key: admin.rpc
  1. rpc配置文件
#admin.yamlName: admin.rpcListenOn: 127.0.0.1:8080#rpc服务发现配置模块Etcd:  Hosts:  - 127.0.0.1:2379  Key: admin.rpc
kubernetes

【阐明】:举荐应用k8s形式部署对应利用,这样能够缩小保护一套etcd集群。

1.api配置文件

#gateway-api.yaml#api服务发现配置模块AdminRpc:  Timeout: 10000  Endpoints:    - 127.0.0.1:39511

2.rpc配置文件

#admin.yamlName: admin.rpcListenOn: 127.0.0.1:8080#rpc服务发现配置模块,不须要配置
IP直连

1.api配置文件

#gateway-api.yaml#api服务发现配置模块AdminRpc:  Timeout: 10000  Endpoints:    - 127.0.0.1:39511

2.rpc配置文件

#admin.yamlName: admin.rpcListenOn: 127.0.0.1:8080#rpc服务发现配置模块,不须要配置
consul
nacos
polaris

二、框架组件

(一)、Api定义

(二)、我的项目配置

  1. rest.RestConf配置

    配置构造体

    RestConf struct {    service.ServiceConf    Host     string `json:",default=0.0.0.0"`    Port     int    CertFile string `json:",optional"`    KeyFile  string `json:",optional"`    Verbose  bool   `json:",optional"`    MaxConns int    `json:",default=10000"`    MaxBytes int64  `json:",default=1048576"`    // milliseconds    Timeout      int64         `json:",default=3000"`    CpuThreshold int64         `json:",default=900,range=[0:1000]"`    Signature    SignatureConf `json:",optional"`}//成员service.ServiceConftype ServiceConf struct {    Name       string    Log        logx.LogConf    Mode       string            `json:",default=pro,options=dev|test|rt|pre|pro"`    MetricsUrl string            `json:",optional"`    Prometheus prometheus.Config `json:",optional"`    Telemetry  trace.Config      `json:",optional"`}//成员SignatureSignatureConf struct {    Strict      bool          `json:",default=false"`    Expiry      time.Duration `json:",default=1h"`    PrivateKeys []PrivateKeyConf}

    第1步:在internal/config/config.go新增配置文件

    【阐明】:以后配置实践上只会存在于api模块中。

    package configimport (    "github.com/zeromicro/go-zero/rest")type Config struct {    rest.RestConf  }

    第2步:在YAML文件配置残缺版本:

    Name: gateway-api   #ServiceConf.NameHost: 0.0.0.0        #RestConf.HostPort: 9510            #RestConf.PortMysql:  Datasource: root:123456@tcp(127.0.0.1:3306)/admin?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghaiAdminRpc:  Timeout: 10000  Etcd:    Hosts:      - 127.0.0.1:2379    Key: admin.rpc
  1. zrpc.RpcClientConf配置
  2. zrpc.RpcServerConf配置
  3. 数据源Mysql配置
  4. Auth权限配置
  5. Redis缓存cache.CacheConf配置
  6. Redis数据库配置
  7. 其余自定义扩大配置

(三)、序列化.

  • form
  • json
  • protobuf
  • xml
  • yaml

(四)、错误处理.

(五)、日志.

(六)、元信息传递.

(七)、head申请头信息传递.

(八)、路由负载平衡.

(九)、中间件.

(十)、传输协定.

(十一)、model不定参数查问、中间件context传值、配置文件详解、SSL证书、ETCD证书、参数验证

(十二)、传输协定

(十三)、分布式事务

(十四)、接口限流和超时设置、协程形式应用

(十五)、生产环境抉择计划

  1. Docker模式+ETCD集群/单体【正文:vpc+slb】
  2. 守护过程+ETCD集群/单体
  3. k8s集群