7.3. 서비스 레지스트리에 스키마 등록
Apache Avro와 같은 적절한 형식으로 스키마를 정의한 후 서비스 레지스트리에 스키마를 추가할 수 있습니다.
다음 방법을 사용하여 스키마를 추가할 수 있습니다.
- 서비스 레지스트리 웹 콘솔
- 서비스 레지스트리 REST API를 사용하는 curl 명령
- 서비스 레지스트리와 함께 제공되는 Maven 플러그인
- 클라이언트 코드에 스키마 구성 추가
클라이언트 애플리케이션은 스키마를 등록할 때까지 서비스 레지스트리를 사용할 수 없습니다.
서비스 레지스트리 웹 콘솔
Service Registry가 설치되면 ui 끝점에서 웹 콘솔에 연결할 수 있습니다.
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
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>
생산자 클라이언트를 사용한 구성 예
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();
}
}