5.4. Apicurio Registry Maven プラグインを使用したアーティファクト参照の手動追加
一部の Apicurio Registry アーティファクトタイプには、あるアーティファクトファイルから別のアーティファクトファイルへの アーティファクト参照 を含めることができます。再利用可能なスキーマまたは API アーティファクトを定義し、アーティファクト参照の複数の場所からそれらを参照することで、効率を高めることができます。
次のアーティファクトタイプはアーティファクト参照をサポートしています。
- Apache Avro
- Google Protobuf
- JSON スキーマ
- OpenAPI
- AsyncAPI
このセクションでは、Apicurio Registry Maven プラグインを使用して、Apicurio Registry に格納されている単純な Avro スキーマアーティファクトへのアーティファクト参照を手動で登録する簡単な例を示します。この例では、次の Exchange
スキーマアーティファクトが Apicurio Registry にすでに作成されていることを前提としています。
交換スキーマ
{ "namespace": "com.kubetrade.schema.common", "type": "enum", "name": "Exchange", "symbols" : ["GEMINI"] }
{
"namespace": "com.kubetrade.schema.common",
"type": "enum",
"name": "Exchange",
"symbols" : ["GEMINI"]
}
次に、この例では、ネストされた Exchange
スキーマアーティファクトへの参照を含む TradeKey
スキーマアーティファクトを作成します。
Exchange スキーマへのネストされた参照を含む TradeKey スキーマ
{ "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 にすでに作成されている。
手順
apicurio-registry-maven-plugin
を使用してTradeKey
スキーマを登録するように Mavenpom.xml
ファイルを更新します。これには、次のように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! - 1
register
を実行ゴールとして指定し、スキーマアーティファクトを Apicurio Registry にアップロードします。- 2
../apis/registry/v2
エンドポイントを使用して Apicurio Registry URL を指定します。- 3
- 認証が必要な場合は、認証サーバーおよびクライアントの認証情報を指定できます。
- 4
- Apicurio Registry アーティファクトグループ ID を指定します。一意のグループ ID を使用しない場合は、
default
のグループを指定できます。 - 5
- グループ ID、アーティファクト ID、バージョン、タイプ、および場所を使用して、Apicurio Registry アーティファクト参照を指定します。この方法で、複数のアーティファクト参照を登録できます。
-
たとえば、
mvn package
コマンドを使用して、Maven プロジェクトをビルドします。