搜索

5.4. 使用 Service Registry Maven 插件手动添加工件引用

download PDF

有些 Service Registry 工件类型可能会包括从一个 工件文件到另一个工件引用。您可以通过定义可重复使用的模式或 API 工件来创建效率,然后从工件引用中的多个位置引用它们。

以下工件类型支持工件引用:

  • Apache Avro
  • Google Protobuf
  • JSON 架构
  • OpenAPI
  • AsyncAPI

本节展示了一个简单的示例,它使用 Service Registry Maven 插件手动注册存储在 Service Registry 中的简单 Avro schema 工件引用。本例假定已在 Service Registry 中创建了以下 Exchange 模式工件:

Exchange 模式

{
  "namespace": "com.kubetrade.schema.common",
  "type": "enum",
  "name": "Exchange",
  "symbols" : ["GEMINI"]
}

然后,这个示例会创建一个 TradeKey schema 工件,其中包含对嵌套 Exchange schema 工件的引用:

TradeKey 模式,带有嵌套对 Exchange schema 的引用

{
  "namespace": "com.kubetrade.schema.trade",
  "type": "record",
  "name": "TradeKey",
  "fields": [
    {
      "name": "exchange",
      "type": "com.kubetrade.schema.common.Exchange"
    },
    {
      "name": "key",
      "type": "string"
    }
  ]
}

先决条件

  • 您已为您的客户端应用程序创建了一个 Maven 项目。如需了解更多详细信息,请参阅 Apache Maven 文档
  • 引用的 Exchange 模式工件已在 Service 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
                    <clientScope>MY-CLIENT-SCOPE</clientScope>
                    <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>
    1
    指定 register 作为执行目标,将 schema 工件上传到 Service Registry。
    2
    使用 ./ apis/registry/v2 端点指定 Service Registry URL。
    3
    如果需要身份验证,您可以指定身份验证服务器和客户端凭证。
    4
    指定 Service Registry 工件组 ID。如果您不想使用唯一的组 ID,您可以指定 默认组
    5
    使用其组 ID、工件 ID、版本、类型和位置指定 Service Registry 工件引用。您可以以这种方式注册多个工件引用。
  2. 构建您的 Maven 项目,例如使用 mvn package 命令。

其他资源

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.