5.3. 使用 Apicurio Registry Maven 插件添加工件引用


apicurio Registry 工件类型,如 Apache Avro、Google Protobuf 和 JSON Schema 可以包含从一个 工件文件到另一个工件文件的工件引用。您可以通过定义可重复使用的模式或 API 工件来创建参与,然后从工件引用中的多个位置引用它们。

本节演示了一个简单的例子,它使用了 Apicurio Registry Maven 插件将工件引用注册到存储在 Apicurio Registry 中的简单 Avro schema 工件。本例假定 Apicurio Registry 中已创建了以下 Exchange schema 工件:

Exchange schema

{
  "namespace": "com.kubetrade.schema.common",
  "type": "enum",
  "name": "Exchange",
  "symbols" : ["GEMINI"]
}
Copy to Clipboard Toggle word wrap

然后,创建一个 TradeKey 模式工件,其中包括对嵌套 Exchange schema 工件的引用:

带有嵌套交换模式的 TradeKey 模式

{
  "namespace": "com.kubetrade.schema.trade",
  "type": "record",
  "name": "TradeKey",
  "fields": [
    {
      "name": "exchange",
      "type": "com.kubetrade.schema.common.Exchange"
    },
    {
      "name": "key",
      "type": "string"
    }
  ]
}
Copy to Clipboard Toggle word wrap

前提条件

  • 您已为您的客户端应用程序创建了 Maven 项目。如需了解更多详细信息,请参阅 Apache Maven 文档
  • Exchange 模式工件已在 Apicurio Registry 中创建。

流程

  1. 更新您的 Maven pom.xml 文件,以使用 apicurio-registry-maven-plugin 注册 TradeKey 模式,其中包含对 Exchange 模式的嵌套引用,如下所示:

    <plugin>
        <groupId>io.apicurio</groupId>
        <artifactId>apicurio-registry-maven-plugin</artifactId>
        <version>${apicurio-registry.version}</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>register</goal> 
    1
    
                </goals>
                <configuration>
                    <registryUrl>MY-REGISTRY-URL/apis/registry/v2</registryUrl> 
    2
    
                    <authServerUrl>MY-AUTH-SERVER</authServerUrl>
                    <clientId>MY-CLIENT-ID</clientId>
                    <clientSecret>MY-CLIENT-SECRET</clientSecret> 
    3
    
                    <artifacts>
                        <artifact>
                            <groupId>test-group</groupId> 
    4
    
                            <artifactId>TradeKey</artifactId>
                            <version>2.0</version>
                            <type>AVRO</type>
                            <file>
                                ${project.basedir}/src/main/resources/schemas/TradeKey.avsc
                            </file>
                            <ifExists>RETURN_OR_UPDATE</ifExists>
                            <canonicalize>true</canonicalize>
                            <references>
                                <reference> 
    5
    
                                    <name>com.kubetrade.schema.common.Exchange</name>
                                    <groupId>test-group</groupId>
                                    <artifactId>Exchange</artifactId>
                                    <version>2.0</version>
                                    <type>AVRO</type>
                                    <file>
                                        ${project.basedir}/src/main/resources/schemas/Exchange.avsc
                                    </file>
                                    <ifExists>RETURN_OR_UPDATE</ifExists>
                                    <canonicalize>true</canonicalize>
                                </reference>
                            </references>
                        </artifact>
                    </artifacts>
                </configuration>
            </execution>
        </executions>
    </plugin>
    Copy to Clipboard Toggle word wrap
    1. 指定 register 作为执行目标,以便将 schema 构件上传到 registry。
    2. 使用 ../apis/registry/v2 端点指定 Apicurio Registry URL。
    3. 如果需要身份验证,您可以指定身份验证服务器和客户端凭证。
    4. 指定 Apicurio Registry 工件组 ID。如果您不想使用唯一组 ID,可以指定 default 组。
    5. 使用组 ID、工件 ID、版本、类型和位置指定 Apicurio Registry 工件引用。您可以以这种方式注册多个构件引用。
  2. 使用 mvn package 命令构建 Maven 项目。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat