5.4. 使用 Apicurio Registry Maven 插件手动添加工件引用
有些 Apicurio Registry 工件类型可以包含从一个 工件文件到另一个工件引用。您可以通过定义可重复使用的模式或 API 工件来创建效率,然后从工件引用中的多个位置引用它们。
以下工件类型支持工件引用:
- Apache Avro
- Google Protobuf
- JSON 架构
- OpenAPI
- AsyncAPI
本节展示了使用 Apicurio Registry Maven 插件对存储在 Apicurio Registry 中的简单 Avro schema 工件引用的简单示例。本例假定 Apicurio Registry 中已创建了以下 Exchange
模式工件:
Exchange 模式
{ "namespace": "com.kubetrade.schema.common", "type": "enum", "name": "Exchange", "symbols" : ["GEMINI"] }
{
"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" } ] }
{
"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
模式工件已在 Apicurio Registry 中创建。
流程
更新您的 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> </goals> <configuration> <registryUrl>MY-REGISTRY-URL/apis/registry/v2</registryUrl> <authServerUrl>MY-AUTH-SERVER</authServerUrl> <clientId>MY-CLIENT-ID</clientId> <clientSecret>MY-CLIENT-SECRET</clientSecret> <clientScope>MY-CLIENT-SCOPE</clientScope> <artifacts> <artifact> <groupId>test-group</groupId> <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> <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>
<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>
Copy to Clipboard Copied! -
构建您的 Maven 项目,例如使用
mvn package
命令。