服务监听

简要概述

公共配置如 http、grpc 监听地址等,均使用 github.com/weaveworks/common/server/server.go 中定义的结构体。

配置示列

最小化

server:
  http_listen_port: 9009

默认值

server:
  http_listen_network: tcp
  http_listen_address: ""
  http_listen_port: 9009
  http_listen_conn_limit: 0
  grpc_listen_network: tcp
  grpc_listen_address: ""
  grpc_listen_port: 9095
  grpc_listen_conn_limit: 0
  http_tls_config:
    cert_file: ""
    key_file: ""
    client_auth_type: ""
    client_ca_file: ""
  grpc_tls_config:
    cert_file: ""
    key_file: ""
    client_auth_type: ""
    client_ca_file: ""
  register_instrumentation: true
  graceful_shutdown_timeout: 30s
  http_server_read_timeout: 30s
  http_server_write_timeout: 30s
  http_server_idle_timeout: 2m0s
  grpc_server_max_recv_msg_size: 104857600
  grpc_server_max_send_msg_size: 104857600
  grpc_server_max_concurrent_streams: 1000
  grpc_server_max_connection_idle: 2562047h47m16.854775807s
  grpc_server_max_connection_age: 2562047h47m16.854775807s
  grpc_server_max_connection_age_grace: 2562047h47m16.854775807s
  grpc_server_keepalive_time: 2h0m0s
  grpc_server_keepalive_timeout: 20s
  grpc_server_min_time_between_pings: 10s
  grpc_server_ping_without_stream_allowed: true
  log_format: logfmt
  log_level: info
  log_source_ips_enabled: false
  log_source_ips_header: ""
  log_source_ips_regex: ""
  log_request_at_info_level_enabled: false
  http_path_prefix: ""

参数解析

命令行参数

名称 默认值 说明

数据结构

Config

github.com/weaveworks/common/server/server.go

// Config for a Server
type Config struct {
    MetricsNamespace  string `yaml:"-"`
    HTTPListenNetwork string `yaml:"http_listen_network"`
    HTTPListenAddress string `yaml:"http_listen_address"`
    HTTPListenPort    int    `yaml:"http_listen_port"`
    HTTPConnLimit     int    `yaml:"http_listen_conn_limit"`
    GRPCListenNetwork string `yaml:"grpc_listen_network"`
    GRPCListenAddress string `yaml:"grpc_listen_address"`
    GRPCListenPort    int    `yaml:"grpc_listen_port"`
    GRPCConnLimit     int    `yaml:"grpc_listen_conn_limit"`

    HTTPTLSConfig TLSConfig `yaml:"http_tls_config"`
    GRPCTLSConfig TLSConfig `yaml:"grpc_tls_config"`

    RegisterInstrumentation  bool `yaml:"register_instrumentation"`
    ExcludeRequestInLog      bool `yaml:"-"`
    DisableRequestSuccessLog bool `yaml:"-"`

    ServerGracefulShutdownTimeout time.Duration `yaml:"graceful_shutdown_timeout"`
    HTTPServerReadTimeout         time.Duration `yaml:"http_server_read_timeout"`
    HTTPServerWriteTimeout        time.Duration `yaml:"http_server_write_timeout"`
    HTTPServerIdleTimeout         time.Duration `yaml:"http_server_idle_timeout"`

    GRPCOptions                   []grpc.ServerOption            `yaml:"-"`
    GRPCMiddleware                []grpc.UnaryServerInterceptor  `yaml:"-"`
    GRPCStreamMiddleware          []grpc.StreamServerInterceptor `yaml:"-"`
    HTTPMiddleware                []middleware.Interface         `yaml:"-"`
    Router                        *mux.Router                    `yaml:"-"`
    DoNotAddDefaultHTTPMiddleware bool                           `yaml:"-"`

    GPRCServerMaxRecvMsgSize           int           `yaml:"grpc_server_max_recv_msg_size"`
    GRPCServerMaxSendMsgSize           int           `yaml:"grpc_server_max_send_msg_size"`
    GPRCServerMaxConcurrentStreams     uint          `yaml:"grpc_server_max_concurrent_streams"`
    GRPCServerMaxConnectionIdle        time.Duration `yaml:"grpc_server_max_connection_idle"`
    GRPCServerMaxConnectionAge         time.Duration `yaml:"grpc_server_max_connection_age"`
    GRPCServerMaxConnectionAgeGrace    time.Duration `yaml:"grpc_server_max_connection_age_grace"`
    GRPCServerTime                     time.Duration `yaml:"grpc_server_keepalive_time"`
    GRPCServerTimeout                  time.Duration `yaml:"grpc_server_keepalive_timeout"`
    GRPCServerMinTimeBetweenPings      time.Duration `yaml:"grpc_server_min_time_between_pings"`
    GRPCServerPingWithoutStreamAllowed bool          `yaml:"grpc_server_ping_without_stream_allowed"`

    LogFormat             logging.Format    `yaml:"log_format"`
    LogLevel              logging.Level     `yaml:"log_level"`
    Log                   logging.Interface `yaml:"-"`
    LogSourceIPs          bool              `yaml:"log_source_ips_enabled"`
    LogSourceIPsHeader    string            `yaml:"log_source_ips_header"`
    LogSourceIPsRegex     string            `yaml:"log_source_ips_regex"`
    LogRequestAtInfoLevel bool              `yaml:"log_request_at_info_level_enabled"`

    // If not set, default signal handler is used.
    SignalHandler SignalHandler `yaml:"-"`

    // If not set, default Prometheus registry is used.
    Registerer prometheus.Registerer `yaml:"-"`
    Gatherer   prometheus.Gatherer   `yaml:"-"`

    PathPrefix string `yaml:"http_path_prefix"`
}

logging.Format

github.com/weaveworks/common/logging/format.go

// Format is a settable identifier for the output format of logs
type Format struct {
    s      string
    Logrus logrus.Formatter
}

// Set updates the value of the output format.  Implements flag.Value
func (f *Format) Set(s string) error {
    switch s {
    case "logfmt":
        f.Logrus = &logrus.JSONFormatter{}
    case "json":
        f.Logrus = &logrus.JSONFormatter{}
    default:
        return errors.Errorf("unrecognized log format %q", s)
    }
    f.s = s
    return nil
}

logging.Level

github.com/weaveworks/common/logging/level.go

// Level is a settable identifier for the minimum level a log entry
// must be have.
type Level struct {
    s      string
    Logrus logrus.Level
    Gokit  level.Option
}

// Set updates the value of the allowed level.  Implments flag.Value.
func (l *Level) Set(s string) error {
    switch s {
    case "debug":
        l.Logrus = logrus.DebugLevel
        l.Gokit = level.AllowDebug()
    case "info":
        l.Logrus = logrus.InfoLevel
        l.Gokit = level.AllowInfo()
    case "warn":
        l.Logrus = logrus.WarnLevel
        l.Gokit = level.AllowWarn()
    case "error":
        l.Logrus = logrus.ErrorLevel
        l.Gokit = level.AllowError()
    default:
        return errors.Errorf("unrecognized log level %q", s)
    }

    l.s = s
    return nil
}



最后修改 2023.06.18: docs: 更改为服务监听 (f039dad)