

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 更新组件配置
<a name="update-component-configurations"></a>

组件配置是定义每个组件参数的 JSON 对象。每个组件的配方都定义了其默认配置，当您将组件部署至核心设备时，可以修改这些配置。

创建部署时，可以指定要应用于每个组件的*配置更新*。配置更新是补丁操作，这意味着更新会修改核心设备上存在的组件配置。如果核心设备没有该组件，则配置更新会修改并应用该部署的默认配置。

配置更新定义了*重置*更新和*合并*更新。重置更新定义了要将哪些配置值重置为默认值或移除哪些配置值。合并更新定义要为组件设置的新配置值。部署配置更新时， AWS IoT Greengrass Core 软件会在合并更新之前运行重置更新。

组件可以验证您部署的配置更新。当部署更改配置时，该组件会订阅以接收通知，并可拒绝不支持的配置。有关更多信息，请参阅 [与组件配置交互](ipc-component-configuration.md)。

**Topics**
+ [重置更新](#reset-configuration-update)
+ [合并更新](#merge-configuration-update)
+ [示例](#configuration-update-example)

## 重置更新
<a name="reset-configuration-update"></a>

重置更新定义了要重置为核心设备默认值的配置值。如果配置值没有默认值，则重置更新会将该值从组件的配置中移除。这可以帮助您修复因配置无效而中断的组件。

使用 JSON 指针列表来定义要重置哪些配置值。JSON 指针以正斜杠 `/` 开头。要识别嵌套组件配置中的值，请使用正斜杠 (`/`) 分隔配置中每一级的键。有关更多信息，请参阅 [JSON 指针规范](https://tools.ietf.org/html/rfc6901)。

**注意**  
您只能将整个列表重置为其默认值。您不能使用重置更新来重置列表中的单个元素。

要将组件的整个配置重置为其默认值，请指定一个空字符串作为重置更新。

```
"reset": [""]
```

## 合并更新
<a name="merge-configuration-update"></a>

合并更新定义了要插入核心组件配置的配置值。合并更新是一个 JSON 对象， AWS IoT Greengrass 核心软件会在重置您在重置更新中指定的路径中重置值后合并该对象。使用 AWS CLI 或时 AWS SDKs，必须将此 JSON 对象序列化为字符串。

您可以合并组件默认配置中不存在的键值对。您也可以合并类型与具有相同键的值不同的键值对。新值将替换旧值。这意味着您可以更改配置对象的结构。

您可以合并空值和空字符串、列表和对象。

**注意**  
不能将合并更新用于在列表中插入元素或将元素附加到列表中。您可以替换整个列表，也可以定义一个对象，其中每个元素都有一个唯一键。  
<a name="configuration-value-type-note"></a>AWS IoT Greengrass 使用 JSON 作为配置值。JSON 指定了数字类型，但不区分整数和浮点数。因此， AWS IoT Greengrass中的配置值可能会转换为浮点数。为确保您的组件使用正确的数据类型，我们建议您将数字配置值定义为字符串。然后，让组件将其解析为整数或浮点数。这样可以确保您的配置值在配置和核心设备上具有相同的类型。

### 在合并更新中使用配方变量
<a name="merge-configuration-update-recipe-variables"></a>

此功能适用于 [Greengrass Nucleus 组件](greengrass-nucleus-component.md)的 v2.6.0 及更高版本。

如果您将 Greengrass n [interpolateComponentConfiguration](greengrass-nucleus-component.md#greengrass-nucleus-component-configuration-interpolate-component-configuration)ucleus 的配置选项设置为，则可以在合并更新中使用配方变量`true`以外的配`component_dependency_name:configuration:json_pointer`方变量。例如，您可以在合并更新中使用`{iot:thingName}`配方变量，将核心设备 AWS IoT 的事物名称包含在组件配置值（例如[进程间通信 (IPC) 授权策略）](interprocess-communication.md#ipc-authorization-policies)中。

## 示例
<a name="configuration-update-example"></a>

以下示例演示了具有以下默认配置的控制面板组件的配置更新。此示例组件显示有关工业设备的信息。

```
{
  "name": null,
  "mode": "REQUEST",
  "network": {
    "useHttps": true,
    "port": {
      "http": 80,
      "https": 443
    },
  },
  "tags": []
}
```

### 工业控制面板组件配方
<a name="w2ab1c24c25c22c16c17b7b1"></a>

------
#### [ JSON ]

```
{
  "RecipeFormatVersion": "2020-01-25",
  "ComponentName": "com.example.IndustrialDashboard",
  "ComponentVersion": "1.0.0",
  "ComponentDescription": "Displays information about industrial equipment.",
  "ComponentPublisher": "Amazon",
  "ComponentConfiguration": {
    "DefaultConfiguration": {
      "name": null,
      "mode": "REQUEST",
      "network": {
        "useHttps": true,
        "port": {
          "http": 80,
          "https": 443
        },
      },
      "tags": []
    }
  },
  "Manifests": [
    {
      "Platform": {
        "os": "linux"
      },
      "Lifecycle": {
        "Run": "python3 -u {artifacts:path}/industrial_dashboard.py"
      }
    },
    {
      "Platform": {
        "os": "windows"
      },
      "Lifecycle": {
        "Run": "py -3 -u {artifacts:path}/industrial_dashboard.py"
      }
    }
  ]
}
```

------
#### [ YAML ]

```
---
RecipeFormatVersion: '2020-01-25'
ComponentName: com.example.IndustrialDashboard
ComponentVersion: '1.0.0'
ComponentDescription: Displays information about industrial equipment.
ComponentPublisher: Amazon
ComponentConfiguration:
  DefaultConfiguration:
    name: null
    mode: REQUEST
    network:
      useHttps: true
      port:
        http: 80
        https: 443
    tags: []
Manifests:
  - Platform:
      os: linux
    Lifecycle:
      Run: |
        python3 -u {artifacts:path}/industrial_dashboard.py
  - Platform:
      os: windows
    Lifecycle:
      Run: |
        py -3 -u {artifacts:path}/industrial_dashboard.py
```

------

**Example 示例 1：合并更新**  
您可以创建应用以下配置更新的部署，指定合并更新，但不指定重置更新。此配置更新会告知组件在 HTTP 端口 8080 上显示控制面板，其中包含来自两个锅炉的数据。    
**要合并的配置**  

```
{
  "name": "Factory 2A",
  "network": {
    "useHttps": false,
    "port": {
      "http": 8080
    }
  },
  "tags": [
    "/boiler/1/temperature",
    "/boiler/1/pressure",
    "/boiler/2/temperature",
    "/boiler/2/pressure"
  ]
}
```
以下命令会创建对核心设备的部署。  

```
aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment.json
```
`dashboard-deployment.json` 文件包含以下 JSON 文档。  

```
{
  "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore",
  "deploymentName": "Deployment for MyGreengrassCore",
  "components": {
    "com.example.IndustrialDashboard": {
      "componentVersion": "1.0.0",
      "configurationUpdate": {
        "merge": "{\"name\":\"Factory 2A\",\"network\":{\"useHttps\":false,\"port\":{\"http\":8080}},\"tags\":[\"/boiler/1/temperature\",\"/boiler/1/pressure\",\"/boiler/2/temperature\",\"/boiler/2/pressure\"]}"
      }
    }
  }
}
```
以下 [Greengrass CLI](greengrass-cli-component.md) 命令会在核心设备上创建本地部署。  

```
sudo greengrass-cli deployment create \
  --recipeDir recipes \
  --artifactDir artifacts \
  --merge "com.example.IndustrialDashboard=1.0.0" \
  --update-config dashboard-configuration.json
```
`dashboard-configuration.json` 文件包含以下 JSON 文档。  

```
{
  "com.example.IndustrialDashboard": {
    "MERGE": {
      "name": "Factory 2A",
      "network": {
        "useHttps": false,
        "port": {
          "http": 8080
        }
      },
      "tags": [
        "/boiler/1/temperature",
        "/boiler/1/pressure",
        "/boiler/2/temperature",
        "/boiler/2/pressure"
      ]
    }
  }
}
```
此次更新后，控制面板组件具有以下配置。  

```
{
  "name": "Factory 2A",
  "mode": "REQUEST",
  "network": {
    "useHttps": false,
    "port": {
      "http": 8080,
      "https": 443
    }
  },
  "tags": [
    "/boiler/1/temperature",
    "/boiler/1/pressure",
    "/boiler/2/temperature",
    "/boiler/2/pressure"
  ]
}
```

**Example 示例 2：重置和合并更新**  
然后，您可以创建应用以下配置更新的部署，指定重置更新和合并更新。这些更新指定在默认的 HTTPS 端口上显示控制面板，其中包含来自不同锅炉的数据。这些更新修改了上一个示例中配置更新所产生的配置。    
**重置路径**  

```
[
  "/network/useHttps",
  "/tags"
]
```  
**要合并的配置**  

```
{
  "tags": [
    "/boiler/3/temperature",
    "/boiler/3/pressure",
    "/boiler/4/temperature",
    "/boiler/4/pressure"
  ]
}
```
以下命令会创建对核心设备的部署。  

```
aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment2.json
```
`dashboard-deployment2.json` 文件包含以下 JSON 文档。  

```
{
  "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore",
  "deploymentName": "Deployment for MyGreengrassCore",
  "components": {
    "com.example.IndustrialDashboard": {
      "componentVersion": "1.0.0",
      "configurationUpdate": {
        "reset": [
          "/network/useHttps",
          "/tags"
        ],
        "merge": "{\"tags\":[\"/boiler/3/temperature\",\"/boiler/3/pressure\",\"/boiler/4/temperature\",\"/boiler/4/pressure\"]}"
      }
    }
  }
}
```
以下 [Greengrass CLI](greengrass-cli-component.md) 命令会在核心设备上创建本地部署。  

```
sudo greengrass-cli deployment create \
  --recipeDir recipes \
  --artifactDir artifacts \
  --merge "com.example.IndustrialDashboard=1.0.0" \
  --update-config dashboard-configuration2.json
```
`dashboard-configuration2.json` 文件包含以下 JSON 文档。  

```
{
  "com.example.IndustrialDashboard": {
    "RESET": [
      "/network/useHttps",
      "/tags"
    ],
    "MERGE": {
      "tags": [
        "/boiler/3/temperature",
        "/boiler/3/pressure",
        "/boiler/4/temperature",
        "/boiler/4/pressure"
      ]
    }
  }
}
```
此次更新后，控制面板组件具有以下配置。  

```
{
  "name": "Factory 2A",
  "mode": "REQUEST",
  "network": {
    "useHttps": true,
    "port": {
      "http": 8080,
      "https": 443
    }
  },
  "tags": [
    "/boiler/3/temperature",
    "/boiler/3/pressure",
    "/boiler/4/temperature",
    "/boiler/4/pressure",
  ]
}
```