

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon Neptune Serverless
<a name="neptune-serverless-using"></a>

您可以將新的 Neptune 資料庫叢集建立為無伺服器資料庫叢集，或在某些情況下，您可以轉換現有的資料庫叢集以使用無伺服器。您也可以在無伺服器資料庫叢集中的資料庫執行個體與無伺服器執行個體之間進行來回轉換。您只能在支援 Neptune Serverless AWS 區域 的其中一個 中使用 Neptune Serverless，但有一些其他限制 （請參閱 [Amazon Neptune Serverless 限制條件](neptune-serverless.md#neptune-serverless-limitations))。

您也可以使用 [Neptune CloudFormation 堆疊](get-started-cfn-create.md)來建立 Neptune Serverless 資料庫叢集。

## 建立一個使用無伺服器的新資料庫叢集
<a name="neptune-serverless-create"></a>

若要建立一個使用無伺服器的 Neptune 資料庫叢集，您可以透過您建立一個佈建叢集所用的同一方式[使用 AWS 管理主控台](manage-console-launch-console.md)來執行此操作。不同之處在於，您需要在**資料庫執行個體大小**下將**資料庫執行個體類別**設定為**無伺服器**。當您這麼做時，需要針對叢集[設定無伺服器容量範圍](neptune-serverless-capacity-scaling.md)。

您也可以使用 AWS CLI 具有這類命令的 來建立無伺服器資料庫叢集 （在 Windows 上，以 '^' 取代 '\\')：

```
aws neptune create-db-cluster \
  --region {{(an AWS 區域 region that supports serverless)}} \
  --db-cluster-identifier {{(ID for the new serverless DB cluster)}} \
  --engine neptune \
  --engine-version {{(optional: 1.2.0.1 or above)}} \
  --serverless-v2-scaling-configuration "MinCapacity={{1.0}}, MaxCapacity={{128.0}}"
```

您也可以指定 `serverless-v2-scaling-configuration` 參數，如下所示：

```
  --serverless-v2-scaling-configuration '{"MinCapacity":{{1.0}}, "MaxCapacity":{{128.0}}}'
```

然後，您可以執行 `ServerlessV2ScalingConfiguration` 屬性的 `describe-db-clusters` 命令，其應會傳回您指定的容量範圍設定：

```
"ServerlessV2ScalingConfiguration": {
    "MinCapacity": {{(the specified minimum number of NCUs)}},
    "MaxCapacity": {{(the specified maximum number of NCUs)}}
}
```

## 將現有的資料庫叢集或執行個體轉換為無伺服器
<a name="neptune-conversion-to-serverless"></a>

如果您具有正在使用引擎 1.2.0.1 版或更新版本的 Neptune 資料庫叢集，則可以將其轉換為無伺服器。此過程確實會產生一些停機時間。

第一步是將容量範圍新增至現有叢集。您可以使用 AWS 管理主控台或使用類似這樣的 AWS CLI 命令來執行此操作 （在 Windows 上，將 '\\' 取代為 '^')：

```
aws neptune modify-db-cluster \
  --db-cluster-identifier {{(your DB cluster ID)}} \
  --serverless-v2-scaling-configuration \
      MinCapacity={{(minimum number of NCUs, such as  2.0)}}, \
      MaxCapacity={{(maximum number of NCUs, such as 24.0)}}
```

下一步是建立新的無伺服器資料庫執行個體，以取代叢集中現有的主要執行個體 (寫入器)。同樣地，您可以使用 AWS 管理主控台 或 來執行此操作和所有後續步驟 AWS CLI。在任一情況下，請將資料庫執行個體類別指定為無伺服器。 AWS CLI 命令看起來像這樣 （在 Windows 上，將 '\\' 取代為 '^')：

```
aws neptune create-db-instance \
  --db-instance-identifier {{(an instance ID for the new writer instance)}} \
  --db-cluster-identifier {{(ID of the DB cluster)}} \
  --db-instance-class db.serverless \
  --engine neptune
```

當新的寫入器執行個體變成可用時，請執行容錯移轉，使其成為叢集的寫入器執行個體：

```
aws neptune failover-db-cluster \
  --db-cluster-identifier {{(ID of the DB cluster)}} \
  --target-db-instance-identifier {{(instance ID of the new serverless instance)}}
```

接下來，刪除舊的寫入器執行個體：

```
aws neptune delete-db-instance \
  --db-instance-identifier {{(instance ID of the old writer instance)}} \
  --skip-final-snapshot
```

最後，執行相同的操作來建立新的無伺服器執行個體，以取代您想要轉換為無伺服器執行個體的每個現有的佈建讀取器執行個體，並刪除現有的佈建執行個體 (讀取器執行個體不需要容錯移轉)。

## 修改現有無伺服器資料庫叢集的容量範圍
<a name="neptune-modify-capacity-range"></a>

您可以使用類似這樣的 AWS CLI 變更 Neptune Serverless 資料庫叢集的容量範圍 (在 Windows 上，將「\\」取代為「^」)：

```
aws neptune modify-db-cluster \
  --region {{(an AWS region that supports serverless)}} \
  --db-cluster-identifier {{(ID of the serverless DB cluster)}} \
  --apply-immediately \
  --serverless-v2-scaling-configuration MinCapacity={{4.0}}, MaxCapacity={{32}}
```

變更容量範圍會造成某些組態參數的預設值產生變更。Neptune 可以立即套用其中某些新的預設值，但某些動態參數變更僅在重新啟動之後才會生效。狀態 `pending-reboot` 表示需要重新啟動才能套用某些參數的變更。

## 將無伺服器資料庫執行個體變更為佈建
<a name="neptune-conversion-to-provisioned"></a>

若要將 Neptune Serverless 執行個體轉換為佈建的執行個體，您只需將其執行個體類別變更為其中一個佈建的執行個體類別即可。請參閱 [修改 Neptune 資料庫執行個體 (並立即套用)](manage-console-instances-modify.md)。

## 為 Serverless 設定 Gremlin 用戶端
<a name="neptune-serverless-client-config"></a>

搭配 Neptune Serverless 使用 Gremlin WebSocket 用戶端時，您需要適當設定用戶端的活動訊號間隔，以在擴展事件期間維持穩定的連線。如需 Java、Go、JavaScript/Node.js 和 Python 用戶端的詳細組態說明，請參閱 [Neptune Serverless 的活動訊號組態](best-practices-gremlin-heartbeat-serverless.md)。

## 使用 Amazon CloudWatch 監控無伺服器容量
<a name="neptune-serverless-monitoring"></a>

您可以使用 CloudWatch 來監控資料庫叢集中 Neptune Serverless 執行個體的容量和使用率。有兩個 CloudWatch 指標可讓您同時在叢集層級和執行個體層級追蹤目前的無伺服器容量：
+ **`ServerlessDatabaseCapacity`** – 作為執行個體層級指標，`ServerlessDatabaseCapacity` 會報告目前的執行個體容量，以 NCU 為單位。作為叢集層級指標，其會報告叢集中所有資料庫執行個體的所有 `ServerlessDatabaseCapacity` 值的平均值。
+ **`NCUUtilization`** – 此指標報告可能使用的容量百分比。其計算方式為將目前的 `ServerlessDatabaseCapacity` (在執行個體層級或叢集層級) 除以資料庫叢集的最大容量設定。

  如果此指標在叢集層級接近 100%，表示叢集已盡可能高地擴展，請考慮增加最大容量設定。

  如果對於讀取器執行個體，它接近 100%，而寫入器執行個體未接近最大容量，請考慮新增更多的讀取器執行個體來分散讀取工作負載。

請注意，`CPUUtilization` 和 `FreeableMemory` 指標對無伺服器執行個體的意義與對佈建執行個體的意義略有不同。在無伺服器內容中，`CPUUtilization` 是百分比，其計算方法為目前使用的 CPU 數量除以最大容量可用的 CPU 數量。同樣地，`FreeableMemory` 會報告執行個體達到最大容量時將可用的可用記憶體數量。

下列範例說明如何在 Linux AWS CLI 上使用 來擷取指定資料庫執行個體的最小、最大和平均容量值，在一小時內每 10 分鐘測量一次。Linux `date` 命令指定相對於目前日期和時間的開始和結束時間。`--query` 參數中的 `sort_by` 函數根據 `Timestamp` 欄位依時間順序對結果進行排序。

```
aws cloudwatch get-metric-statistics \
  --metric-name "ServerlessDatabaseCapacity" \
  --start-time "$(date -d '1 hour ago')" \
  --end-time "$(date -d 'now')" \
  --period 600 \
  --namespace "AWS/Neptune"
  --statistics Minimum Maximum Average \
  --dimensions Name=DBInstanceIdentifier,Value={{(instance ID)}} \
  --query 'sort_by(Datapoints[*].{min:Minimum,max:Maximum,avg:Average,ts:Timestamp},&ts)' \
  --output table
```