我正在尝试为我的 Quarkus 应用程序实现集成测试,其中我使用测试容器和 Azurite 来实例化 Blob 容器。现在,当容器启动时,我可以使用容器在主机上映射到的端口构建连接字符串。
问题是 Quarkus 应用程序已经构建完毕,我在更新连接字符串的配置属性时遇到了问题。
有人知道该怎么做吗?
@Singleton
public class BlobStorageTestResource implements QuarkusTestResourceLifecycleManager {
@Container
final GenericContainer azurite = new GenericContainer(DockerImageName.parse("mcr.microsoft.com/azure-storage/azurite"))
.withExposedPorts(10000)
.withCommand("azurite-blob --blobHost 0.0.0.0 --blobPort 10000 --loose");
@Override
public Map<String, String> start() {
azurite.start();
String connectionString = String.format("DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:%d/devstoreaccount1;", azurite.getFirstMappedPort());
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
blobServiceClient.createBlobContainer("conversion");
return Map.of();
}
@Override
public void stop() {
azurite.stop();
}
}
@Produces
@Singleton
@IfBuildProfile("test")
BlobContainerAsyncClient provideTestBlobContainer() {
return new BlobServiceClientBuilder()
.connectionString(blobConfig.connectionString())
.buildAsyncClient()
.getBlobContainerAsyncClient(blobConfig.containerName());
}
如果你想在应用程序构建之前注入配置,你应该检查smallrye ConfigSourceFactory。
https://quarkus.io/guides/config-extending-support#config-source-factory
Quarkus 配置依赖于 microprofile 的 Smallrye 实现,因此他们的文档在处理编程配置注入方面提供了更多方法。
https://smallrye.io/smallrye-config/Main/config-sources/custom/#configsourcefactory
一旦提供了正确的配置,你就可以创建你的bean,并使用@ConfigProperty