7.3. Apicurio Registry にスキーマを登録する
スキーマを Apache Avro などの適切な形式で定義したら、スキーマを Apicurio Registry に追加できます。
以下の方法を使用してスキーマを追加できます。
- Apicurio Registry Web コンソール
- Apicurio Registry REST API を使用した curl コマンド
- Apicurio Registry に付属の Maven プラグイン
- クライアントコードに加えられたスキーマ設定
スキーマを登録するまで、クライアントアプリケーションは Apicurio Registry を使用できません。
Apicurio Registry Web コンソール
Apicurio Registry がインストールされると、ui
エンドポイントから Web コンソールに接続できます。
http://MY-REGISTRY-URL/ui
コンソールから、スキーマを追加、表示、および設定できます。また、レジストリーに無効なコンテンツが追加されないようにするルールを作成することもできます。
curl コマンドの例
curl -X POST -H "Content-type: application/json; artifactType=AVRO" \ -H "X-Registry-ArtifactId: share-price" \ --data '{ "type":"record", "name":"price", "namespace":"com.example", "fields":[{"name":"symbol","type":"string"}, {"name":"price","type":"string"}]}' https://my-cluster-my-registry-my-project.example.com/apis/registry/v2/groups/my-group/artifacts -s
curl -X POST -H "Content-type: application/json; artifactType=AVRO" \
-H "X-Registry-ArtifactId: share-price" \
--data '{
"type":"record",
"name":"price",
"namespace":"com.example",
"fields":[{"name":"symbol","type":"string"},
{"name":"price","type":"string"}]}'
https://my-cluster-my-registry-my-project.example.com/apis/registry/v2/groups/my-group/artifacts -s
Maven プラグインの例
<plugin> <groupId>io.apicurio</groupId> <artifactId>apicurio-registry-maven-plugin</artifactId> <version>${apicurio.version}</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>register</goal> </goals> <configuration> <registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl> <artifacts> <artifact> <groupId>TestGroup</groupId> <artifactId>FullNameRecord</artifactId> <file>${project.basedir}/src/main/resources/schemas/record.avsc</file> <ifExists>FAIL</ifExists> </artifact> <artifact> <groupId>TestGroup</groupId> <artifactId>ExampleAPI</artifactId> <type>GRAPHQL</type> <file>${project.basedir}/src/main/resources/apis/example.graphql</file> <ifExists>RETURN_OR_UPDATE</ifExists> <canonicalize>true</canonicalize> </artifact> </artifacts> </configuration> </execution> </executions> </plugin>
<plugin>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-registry-maven-plugin</artifactId>
<version>${apicurio.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>register</goal>
</goals>
<configuration>
<registryUrl>http://REGISTRY-URL/apis/registry/v2</registryUrl>
<artifacts>
<artifact>
<groupId>TestGroup</groupId>
<artifactId>FullNameRecord</artifactId>
<file>${project.basedir}/src/main/resources/schemas/record.avsc</file>
<ifExists>FAIL</ifExists>
</artifact>
<artifact>
<groupId>TestGroup</groupId>
<artifactId>ExampleAPI</artifactId>
<type>GRAPHQL</type>
<file>${project.basedir}/src/main/resources/apis/example.graphql</file>
<ifExists>RETURN_OR_UPDATE</ifExists>
<canonicalize>true</canonicalize>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
プロデューサークライアントを使用した設定例
String registryUrl_node1 = PropertiesUtil.property(clientProperties, "registry.url.node1", "https://my-cluster-service-registry-myproject.example.com/apis/registry/v2"); try (RegistryService service = RegistryClient.create(registryUrl_node1)) { String artifactId = ApplicationImpl.INPUT_TOPIC + "-value"; try { service.getArtifactMetaData(artifactId); } catch (WebApplicationException e) { CompletionStage <ArtifactMetaData> csa = service.createArtifact( "AVRO", artifactId, new ByteArrayInputStream(LogInput.SCHEMA$.toString().getBytes()) ); csa.toCompletableFuture().get(); } }
String registryUrl_node1 = PropertiesUtil.property(clientProperties, "registry.url.node1",
"https://my-cluster-service-registry-myproject.example.com/apis/registry/v2");
try (RegistryService service = RegistryClient.create(registryUrl_node1)) {
String artifactId = ApplicationImpl.INPUT_TOPIC + "-value";
try {
service.getArtifactMetaData(artifactId);
} catch (WebApplicationException e) {
CompletionStage <ArtifactMetaData> csa = service.createArtifact(
"AVRO",
artifactId,
new ByteArrayInputStream(LogInput.SCHEMA$.toString().getBytes())
);
csa.toCompletableFuture().get();
}
}