# 配置

**URL:** https://heroiclabs.com/docs/zh/nakama/getting-started/configuration/
**Summary:** 介绍如何为您的Nakama服务器提供自定义配置以及所有可用的配置属性。

---


# 配置

YAML 配置文件可以配置您的 Nakama 服务器运行方式的多项内容。您可以在不指定配置文件而是依赖默认配置的情况下运行 Nakama。

## 指定配置文件

您可以使用`--config`标志在运行时指定配置文件。

```sh
nakama --config my-special-config.yml
```

如果您正在通过 Docker-Compose 运行 Nakama，则需要在机器中绑定一个文件夹，使其在 Docker 中可用。在文档中查看[配置在 Docker](../install/docker/#configuration-file)上运行的 Nakama。

## 服务器端口

Nakama 系统非常灵活。您可以通过 gRPC, HTTP, Websockets 和 rUDP 与服务器交换数据。由于这种灵活性，Nakama 需要 4 个端口可用于绑定到：

- 在端口 7350 上绑定到 HTTP API 服务器*可以在配置中更改端口。*
- 在 7351 端口上绑定到 HTTP API 服务器，这个服务器启动嵌入式开发人员控制台。_可以在配置中更改端口。_
- 在端口 7349 上绑定到 gRPC API 服务器。_根据 API 服务器端口选择端口。_
- 在 7348 端口上绑定到 gRPC API 服务器，该服务器启动嵌入式控制台。_根据 API 服务器端口选择端口。_

## 共同属性

在大多数环境中以及在将 Nakama 部署到实时生产环境之前，需要更改部分配置属性，例如，您必须更改**`socket.server_key`**，**`session.encryption_key`** 和**`runtime.http_key`**的值。
配置的完整列表见[页面底部](#server-configuration)。

{{< table name="nakama.getting-started.configuration.properties" >}}

## 服务器配置

Nakama 有各种配置选项，可以充分用于各种用例和部署环境。

Nakama 为所有配置选项提供了合理的值，因此您可能仅需要覆盖选项的子集。您也可以仅设置您自己的配置文件，并通过命令行标志覆盖配置文件中的值。例如，覆盖运行时路径：

```sh
nakama --runtime.path /tmp/my-modules
```

对于非特殊字段，将使用默认值。查看[服务器命令行](../commands/)页面，获取关于如何覆盖标志的更多信息。

{{< table name="nakama.getting-started.configuration.configuration" >}}

### 集群

本节介绍如何连接各个节点形成集群。

{{< note "important" "Nakama Enterprise Only" >}}
以下配置选项仅在 Nakama 服务器的 Nakama 企业版中可用。

Nakama 的设计使其在生产环境中作为高度可用的集群运行。如果您正在运行[Nakama 企业版](https://heroiclabs.com/enterprise)，可以在您的开发机器上本地启动集群。在生产中，您可以使用 Nakama 企业版或我们的[Heroic Cloud](https://heroiclabs.com/heroic-cloud/)服务。
{{< / note >}}

{{< table name="nakama.getting-started.configuration.cluster" >}}

### 控制台

本节定义了与嵌入式开发人员控制台相关的配置。

{{< table name="nakama.getting-started.configuration.console" >}}

### 数据库

Nakama 需要一个可用的 CockroachDB 服务器。Nakama 在 CockroachDB 数据库中创建并管理自己的名为`nakama`的数据库。

{{< table name="nakama.getting-started.configuration.database" >}}

您可以通过命令将多个数据库地址传递给 Nakama，例如：

```sh
nakama --database.address "root@db1:26257" --database.address "root@db2:26257"
```

### IAP（应用程序内的购买行为)

Nakama 可以通过连接到各个商店并保存有效购买的分类账来验证应用程序内的购买行为。这有助于防止常见的使用有效收据进行应用程序内购买行为重放攻击，也有助于根据需要恢复用户账户的购买行为。

#### Apple

Apple 应用程序内购买行为的配置

{{< table name="nakama.getting-started.configuration.iap-apple" >}}

#### Google

Google 应用程序内购买行为的配置

{{< table name="nakama.getting-started.configuration.iap-google" >}}

#### 华为

{{< table name="nakama.getting-started.configuration.iap-huawei" >}}

### 排行榜

您可以更改与排行榜和锦标赛系统相关的配置选项。

{{< table name="nakama.getting-started.configuration.leaderboard" >}}

### 记录器

Nakama 生成 JSON 格式的日志，以便各种系统可以与日志交互。默认将这些日志写入到标准输出（控制台）中。

{{< table name="nakama.getting-started.configuration.logger" >}}

无论`logger.stdout`字段的值为多少，都会始终将标准的启动日志消息打印到控制台。

### 匹配

您可以更改与授权多玩家运行时相关的配置选项。

{{< table name="nakama.getting-started.configuration.match" >}}

### 指标

Nakama 生成指标信息。可以将该信息导出到 Prometheus。

{{< table name="nakama.getting-started.configuration.metrics" >}}

由于指标包含敏感的服务器信息，需要对指标导出进行保护。

### 运行时

与运行时引擎相关的选项。

{{< table name="nakama.getting-started.configuration.runtime" >}}

运行时环境是一个键值对。它们由`=`字符分隔：

```sh
nakama --runtime.env "key=value" --runtime.env "key2=value2" --runtime.env "key3=valuecanhave=sign"
```

### 会话

您可以更改与每个用户相关的配置选项，例如用于创建令牌的加密键。

{{< table name="nakama.getting-started.configuration.session" >}}

### 社交

Nakama 可以连接到多种社交网络，以便获取用户信息。它也可以作为通知中心，传递和保持通知。

#### Apple

与 Apple iOS/iPadOS 应用程序相关的配置。

{{< table name="nakama.getting-started.configuration.social-apple" >}}

#### Steam

配置 Steam 网络设置。Facebook, Google 和 GameCenter 不需要任何服务器设置。

{{< table name="nakama.getting-started.configuration.social-steam" >}}

#### Facebook 即时游戏

Facebook 即时游戏相关的配置。

{{< table name="nakama.getting-started.configuration.social-facebook" >}}

### 套接字

与服务器和客户端之间的连接套接字和传输协议相关的选项。

{{< note "important" >}}
提供的默认值已针对“响应式”感觉进行了优化，缺乏经验的用户不应对其进行修改。[联系 Heroic Labs](mailto:support@heroiclabs.com)获取帮助。
{{< / note >}}

{{< table name="nakama.getting-started.configuration.socket" >}}

### 跟踪器

您可以更改与会话跟踪相关的配置选项。

{{< table name="nakama.getting-started.configuration.tracker1" >}}

以下配置选项仅在 Nakama 服务器的 Nakama 企业版中可用

Nakama 的设计使其在生产环境中作为高度可用的集群运行。如果您正在运行[Nakama 企业版](https://heroiclabs.com/enterprise)，您可以在您的开发机器上本地启动集群。在生产中，您可以使用 Nakama 企业版或我们的[Heroic Cloud](https://heroiclabs.com/heroic-cloud/)服务。

{{< table name="nakama.getting-started.configuration.tracker2" >}}

### 配对程序

您可以更改与配对相关的配置选项。

{{< table name="nakama.getting-started.configuration.matchmaker" >}}

## 示例文件

您可以使用整个文件或仅使用配置的子集。

```yaml
name: nakama-node-1
data_dir: "./data/"

logger:
  stdout: false
  level: "warn"
  file: "/tmp/path/to/logfile.log"
  rotation: false
  max_size: 100
  max_age: 0
  max_backups: 0
  local_time: false
  compress: false

metrics:
  reporting_freq_sec: 60
  namespace: ""
  prometheus_port: 7354

database:
  address:
    - "root@localhost:26257"
  conn_max_lifetime_ms: 0
  max_open_conns: 0
  max_idle_conns: 100

runtime:
  env:
    - "example_apikey=example_apivalue"
    - "encryptionkey=afefa==e332*u13=971mldq"
  path: "/tmp/modules/folders"
  http_key: "defaulthttpkey"

socket:
  server_key: "defaultkey"
  port: 7350
  max_message_size_bytes: 4096 # bytes
  read_timeout_ms: 10000
  write_timeout_ms: 10000
  idle_timeout_ms: 60000
  write_wait_ms: 5000
  pong_wait_ms: 10000
  ping_period_ms: 8000 # Must be less than pong_wait_ms
  outgoing_queue_size: 16

session:
  encryption_key: "defaultencryptionkey"
  token_expiry_sec: 60
    refresh_encryption_key: "defaultrefreshencryptionkey"
    refresh_token_expiry_sec: 3600

social:
  steam:
  publisher_key: ""
  app_id: 0

console:
  port: 7351
  username: "admin"
  password: "password"

cluster:
  join:
    - "10.0.0.2:7352"
    - "10.0.0.3:7352"
  gossip_bindaddr: "0.0.0.0"
  gossip_bindport: 7352
  rpc_port: 7353
  local_priority: true
  work_factor_interval_ms: 1000

tracker:
  max_delta_sizes:
    - 100
    - 1000
    - 10000

matchmaker:
  max_tickets: 2
  interval_sec: 15
  max_intervals: 3

iap:
  apple:
    shared_password: "password"
  google:
    client_email: "email@google.com"
    private_key: "pk"
  huawei:
    public_key: "pk"
    client_id: "id"
    client_secret: "secret"
```
