7.3. 在 Service Registry 中注册模式


在定义了适当格式(如 Apache Avro)的模式后,您可以将 schema 添加到 Service Registry。

您可以使用以下方法添加 schema:

  • Service Registry Web 控制台
  • 使用 Service Registry REST API 的 curl 命令
  • Service Registry 提供的 Maven 插件
  • 添加到客户端代码的 schema 配置

在注册了 schema 之前,客户端应用程序无法使用 Service Registry。

Service Registry Web 控制台

安装 Service Registry 时,您可以从 ui 端点连接到 web 控制台:

http://MY-REGISTRY-URL/ui

在控制台中,您可以添加、查看和配置架构。您还可以创建防止将无效内容添加到 registry 中的规则。

curl 命令示例

 curl -X POST -H "Content-type: application/json; artifactType=AVRO" \
   -H "X-Registry-ArtifactId: share-price" \ 
1

   --data '{
     "type":"record",
     "name":"price",
     "namespace":"com.example",
     "fields":[{"name":"symbol","type":"string"},
     {"name":"price","type":"string"}]}'
   https://my-cluster-my-registry-my-project.example.com/apis/registry/v2/groups/my-group/artifacts -s 
2
Copy to Clipboard Toggle word wrap
  1. 简单 Avro 模式构件.
  2. 公开 Service Registry 的 OpenShift 路由名称。

Maven 插件示例

<plugin>
  <groupId>io.apicurio</groupId>
  <artifactId>apicurio-registry-maven-plugin</artifactId>
  <version>${apicurio.version}</version>
  <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>register</goal>  
1

        </goals>
        <configuration>
            <registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl> 
2

            <artifacts>
                <artifact>
                    <groupId>TestGroup</groupId> 
3

                    <artifactId>FullNameRecord</artifactId>
                    <file>${project.basedir}/src/main/resources/schemas/record.avsc</file>
                    <ifExists>FAIL</ifExists>
                </artifact>
                <artifact>
                    <groupId>TestGroup</groupId>
                    <artifactId>ExampleAPI</artifactId> 
4

                    <type>GRAPHQL</type>
                    <file>${project.basedir}/src/main/resources/apis/example.graphql</file>
                    <ifExists>RETURN_OR_UPDATE</ifExists>
                    <canonicalize>true</canonicalize>
                </artifact>
            </artifacts>
        </configuration>
    </execution>
  </executions>
 </plugin>
Copy to Clipboard Toggle word wrap
  1. 指定 register 作为执行目标,以便将 schema 构件上传到 registry。
  2. 使用 ./ apis/registry/v2 端点指定 Service Registry URL。
  3. 指定 Service Registry 工件组 ID。
  4. 您可以使用指定的组 ID、工件 ID 和位置上传多个工件。

使用制作者客户端示例进行配置

String registryUrl_node1 = PropertiesUtil.property(clientProperties, "registry.url.node1",
    "https://my-cluster-service-registry-myproject.example.com/apis/registry/v2"); 
1

try (RegistryService service = RegistryClient.create(registryUrl_node1)) {
    String artifactId = ApplicationImpl.INPUT_TOPIC + "-value";
    try {
        service.getArtifactMetaData(artifactId); 
2

    } catch (WebApplicationException e) {
        CompletionStage <ArtifactMetaData> csa = service.createArtifact(
            "AVRO",
            artifactId,
            new ByteArrayInputStream(LogInput.SCHEMA$.toString().getBytes())
        );
        csa.toCompletableFuture().get();
    }
}
Copy to Clipboard Toggle word wrap
  1. 您可以为多个 URL 节点注册属性。
  2. 检查以确认该架构是否已根据工件 ID 是否存在。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat