9.7. 将架构注册到服务 registry
以适当格式定义了架构后,如 Apache Avro,您可以将架构添加到服务注册表。
您可以通过以下方法添加 schema:
- Service Registry Web 控制台
- 使用 Service Registry API 的 curl 命令
- Service Registry 提供的 Maven 插件
- 添加到客户端代码中的模式配置
在注册了架构前,客户端应用程序无法使用 Service Registry。
Service Registry Web 控制台
安装 Service Registry 后,您可以从 ui 端点连接到 web 控制台:
+http+://MY-REGISTRY-URL/ui
在控制台中,您可以添加、查看和配置模式。您还可以创建防止将无效内容添加到 registry 的规则。
有关使用 Service Registry Web 控制台的更多信息,请参阅 Service Registry 文档。
curl 示例
curl -X POST -H "Content-type: application/json; artifactType=AVRO" \
-H "X-Registry-ArtifactId: prices-value" \
--data '{ 1
"type":"record",
"name":"price",
"namespace":"com.redhat",
"fields":[{"name":"symbol","type":"string"},
{"name":"price","type":"string"}]
}'
https://my-cluster-service-registry-myproject.example.com/api/artifacts -s 2插件示例
<plugin>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-registry-maven-plugin</artifactId>
<version>${registry.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>register</goal>
</goals>
<configuration>
<registryUrl>https://my-cluster-service-registry-myproject.example.com/api</registryUrl>
<artifactType>AVRO</artifactType>
<artifacts>
<schema1>${project.basedir}/schemas/schema1.avsc</schema1>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>通过(producer)客户端示例进行配置
String registryUrl_node1 = PropertiesUtil.property(clientProperties, "registry.url.node1", 1 "https://my-cluster-service-registry-myproject.example.com/api"); try (RegistryService service = RegistryClient.create(registryUrl_node1)) { String artifactId = ApplicationImpl.INPUT_TOPIC + "-value"; try { service.getArtifactMetaData(artifactId); 2 } catch (WebApplicationException e) { CompletionStage <ArtifactMetaData> csa = service.createArtifact( ArtifactType.AVRO, artifactId, new ByteArrayInputStream(LogInput.SCHEMA$.toString().getBytes()) ); csa.toCompletableFuture().get(); } }