5.5. 使用 Service Registry Maven 插件自动添加工件引用
Service Registry 工件类型,如 Apache Avro、Google Protobuf 和 JSON Schema 可以包含从一个 工件文件到另一个工件文件的工件引用。您可以通过定义可重复使用的模式或 API 工件来创建效率,然后从工件引用中的多个位置引用它们。
您可以指定单个工件并配置 Service Registry Maven 插件,以自动检测同一目录中对工件的所有引用,并自动注册这些引用。
本节展示了使用 Maven 插件注册 Avro 模式的简单示例,并自动检测并注册对简单 schema 工件引用。本例假定父 TradeKey 工件和嵌套 Exchange 模式工件在同一目录中都可用:
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"
}
]
}
Exchange 模式
{
"namespace": "com.kubetrade.schema.common",
"type": "enum",
"name": "Exchange",
"symbols" : ["GEMINI"]
}
先决条件
- 您已为您的客户端应用程序创建了一个 Maven 项目。如需了解更多详细信息,请参阅 Apache Maven 文档。
-
TradeKey模式工件和嵌套Exchange模式工件文件都位于同一目录中。
流程
更新您的 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.avsc5 </file> <ifExists>RETURN_OR_UPDATE</ifExists> <canonicalize>true</canonicalize> <analyzeDirectory>true</analyzeDirectory>6 </artifact> </artifacts> </configuration> </execution> </executions> </plugin>-
构建您的 Maven 项目,例如使用
mvn package命令。