---
title: "服务端配置"
url: "https://goframe.org/docs/micro-service/config"
markdown_url: "https://goframe.org/docs/micro-service/config.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "微服务开发"
category: "微服务开发"
published: "2024-10-21"
updated: "2026-02-11"
description: "详细介绍GoFrame框架gRPC服务端的配置管理方法和最佳实践。系统讲解GrpcServerConfig配置对象的各个参数含义和使用方式，包括服务名称、监听地址、日志存储目录、错误日志记录、访问日志记录等核心配置项。提供完整的YAML格式配置模板示例，涵盖基础配置和日志组件扩展配置的详细参数说明。阐述配置文件的自动读取和映射机制，以及访问端口配置、服务注册地址配置等重要注意事项，帮助开发者实现灵活高效的gRPC服务端配置管理和日志管理。"
keywords: ["GoFrame", "GoFrame框架", "配置管理", "服务端配置", "GrpcServerConfig", "日志配置", "日志组件", "服务监听", "自动映射", "错误日志", "gRPC服务", "配置文件", "访问日志", "服务注册"]
---

# 服务端配置

## 基本介绍

gRPC 服务端支持配置文件管理，所有的配置项将会自动映射到 `GrpcServerConfig` 配置对象中。配置对象定义如下：

```go
// 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.
}
```

配置文件的自动读取逻辑与框架一致，详细介绍请参考章节： [配置管理](https://goframe.org/docs/core/gcfg)

## 配置模板

一个完整的配置模板示例如下，包含基础配置和日志扩展配置：

```yaml
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` 的日志。关于日志组件的详细配置说明，请参考文档： [日志组件-配置管理](https://goframe.org/docs/core/glog-config)

## 注意事项

### 访问端口配置

在没有配置 `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` 自定义服务注册的地址。
