服务端配置

基本介绍

服务端支持配置文件,所有的配置将会自动映射到配置对象中。配置对象如下:

// GrpcServerConfig is the configuration for server.
type GrpcServerConfig struct {
    Address          string              // (optional) Address for server listening.
    Name             string              // (optional) Name for current service.
    Logger           *glog.Logger        // (optional) Logger for server.
    LogPath          string              // (optional) LogPath specifies the directory for storing logging files.
    LogStdout        bool                // (optional) LogStdout specifies whether printing logging content to stdout.
    ErrorStack       bool                // (optional) ErrorStack specifies whether logging stack information when error.
    ErrorLogEnabled  bool                // (optional) ErrorLogEnabled enables error logging content to files.
    ErrorLogPattern  string              // (optional) ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log
    AccessLogEnabled bool                // (optional) AccessLogEnabled enables access logging content to file.
    AccessLogPattern string              // (optional) AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log
    Endpoints        []string            // (optional) Endpoints are custom endpoints for service register, it uses Address if empty.
}

配置文件的自动读取逻辑与框架一致,详细介绍请参考章节: 配置管理

配置模板

一个完整的配置模板示例:

grpc:
  name:             "demo"  # 服务名称
  address:          ":8000" # 自定义服务监听地址
  logPath:          "./log" # 日志存储目录路径
  logStdout:        true    # 日志是否输出到终端
  errorLogEnabled:  true    # 是否开启错误日志记录
  accessLogEnabled: true    # 是否开启访问日志记录
  errorStack:       true    # 当产生错误时,是否记录错误堆栈
  endpoints:        ["192.168.1.3:8000"] # 自定义服务注册地址列表

  # 日志扩展配置(参数日志组件配置)
  logger:
    path:                  "/var/log/"   # 日志文件路径。默认为空,表示关闭,仅输出到终端
    file:                  "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log"
    prefix:                ""            # 日志内容输出前缀。默认为空
    level:                 "all"         # 日志输出级别
    stdout:                true          # 日志是否同时输出到终端。默认true
    rotateSize:            0             # 按照日志文件大小对文件进行滚动切分。默认为0,表示关闭滚动切分特性
    rotateExpire:          0             # 按照日志文件时间间隔对文件滚动切分。默认为0,表示关闭滚动切分特性
    rotateBackupLimit:     0             # 按照切分的文件数量清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除
    rotateBackupExpire:    0             # 按照切分的文件有效期清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除
    rotateBackupCompress:  0             # 滚动切分文件的压缩比(0-9)。默认为0,表示不压缩
    rotateCheckInterval:   "1h"          # 滚动切分的时间检测间隔,一般不需要设置。默认为1小时

其中的日志配置与 http server 一致,可以独立使用日志组件的配置项来配置 grpc server 的日志。关于日志组件的配置,可参考该文档详细了解: 日志组件-配置管理

注意事项

访问端口配置

在没有配置 address 的情况下, grpc server 将会使用本地网卡的所有 ip 地址加上 随机空闲端口 来启动(默认配置 :0)。如果想要指定 ip 但是使用随机空闲端口启动 grpc server,可以使用 ip:0 的格式来配置 address,例如: 192.168.1.1:0, 10.0.1.1:0。

服务注册地址

服务注册地址用于对外提供服务发现和访问,可以通过endpoints配置服务注册地址。

在没有配置endpoints的情况下, grpc server将会使用address的地址来注册服务。如果没有指定address配置,或者使用的是0.0.0.0的address配置,那么将会自动获取本机的所有局域网IP地址作为服务注册的地址。如果您对注册的地址有要求,可以通过endpoints自定义服务注册的地址。