我正在运行一个Docker容器(GPC扳手),并试图使用org.testcontainers.containers (来自Junit BeforeClass)创建一个实例。连接被拒绝。
io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:9010
Cause: java.net.ConnectException: Connection refused代码:
private static final Integer[] SPANNER_EMULATOR_PORTS = {9010, 9020};
private static final WaitStrategy WAIT_FOR_START_LOG_MESSAGE =
Wait.forLogMessage(".*gRPC server listening.*", 1).withStartupTimeout(Duration.ofSeconds(30));
...
final GenericContainer<?> container =
new GenericContainer<>(DockerImageName.parse(emulatorDockerImage))
.withExposedPorts(SPANNER_EMULATOR_PORTS)
.waitingFor(
new WaitStrategy() {
@Override
public void waitUntilReady(WaitStrategyTarget waitStrategyTarget) {
// do not wait on #start call, so that we can hook up logger and print output
// for errors which we can do only after #start is invoked
}
@Override
public WaitStrategy withStartupTimeout(Duration startupTimeout) {
return this;
}
});
final StringBuilder containerOutput = new StringBuilder();
final long startTime = System.currentTimeMillis();
try {
System.out.println("Running Spanner Emulator Container");
container.start();
// TODO: See if there's a way to print the output as it's happening and not on timeout
recordOutput(containerOutput, startTime, "Container initialized\n");
} catch (Throwable ex) {
throw new RuntimeException(
"Failed to start up Spanner Emulator",
ex);
}
container.followOutput(
outputFrame -> recordOutput(containerOutput, startTime, outputFrame.getUtf8String()));
try {
WAIT_FOR_START_LOG_MESSAGE.waitUntilReady(container);
// we print only errors in the tests
System.err.println(
"It took "
+ (System.currentTimeMillis() - startTime) / 1000
+ " seconds to init Spanner");
} catch (Exception e) {
recordOutput(containerOutput, startTime, "Explicit timeout");
throw new RuntimeException("Spanner initialization timeout\n" + containerOutput, e);
}
SpannerEmulatorContainer spannerEmulatorContainer = new SpannerEmulatorContainer(container);
spannerEmulatorContainer.createSpannerClient(projectId);
return spannerEmulatorContainer;
...
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
// Create a instance
InstanceInfo instanceInfo =
InstanceInfo.newBuilder(
InstanceId.of(TestConstants.testProjectId, TestConstants.testInstanceId))
.setInstanceConfigId(
InstanceConfigId.of(TestConstants.testProjectId, TestConstants.testRegion))
.setNodeCount(1)
.setDisplayName(TestConstants.testInstanceId)
.build();
// ***** Errors out here ******
OperationFuture<Instance, CreateInstanceMetadata> instanceOperation =
instanceAdminClient.createInstance(instanceInfo);性传播疾病:
2022-10-09T01:35:40.244-0700 [DEBUG] [TestEventLogger] It took 3 seconds to init ...
io.grpc.StatusRuntimeException: UNAVAILABLE: io exception码头工人ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0d09b8a15f4 gcr.io/cloud-spanner-emulator/emulator:1.4.6 "./gateway_main --ho…" 9 minutes ago Up 9 minutes 0.0.0.0:55082->9010/tcp, 0.0.0.0:55081->9020/tcp sleepy_poitras
b1315b3c091e testcontainers/ryuk:0.3.0 "/app" 9 minutes ago Up 9 minutes 0.0.0.0:55080->8080/tcp testcontainers-ryuk-c0389f48-9da7-4731-9dc9-f5f03fc050e0docker日志a0d09b8a15f4
WARNING: proto: file "google/rpc/status.proto" is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: file "google/rpc/status.proto" has a name conflict over google.rpc.Status
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
WARNING: proto: message google.rpc.Status is already registered
previously from: "google.golang.org/genproto/googleapis/rpc/status"
currently from: "unknown"
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict
2022/10/09 08:37:50 gateway.go:140: Cloud Spanner emulator running.
2022/10/09 08:37:50 gateway.go:141: REST server listening at 0.0.0.0:9020
2022/10/09 08:37:50 gateway.go:142: gRPC server listening at 0.0.0.0:9010docker日志b1315b3c091e
2022/10/09 08:37:49 Pinging Docker...
2022/10/09 08:37:49 Docker daemon is available!
2022/10/09 08:37:49 Starting on port 8080...
2022/10/09 08:37:49 Started!
2022/10/09 08:37:49 Connected
2022/10/09 08:37:49 Adding {"label":{"org.testcontainers.sessionId=c0389f48-9da7-4731-9dc9-f5f03fc050e0":true,"org.testcontainers=true":true}}发布于 2022-10-09 09:30:48
问题是,在从测试中连接时,我没有使用随机端口。实际暴露的端口与它在容器中侦听的端口不同。
String address = container.getHost();
Integer port = container.getFirstMappedPort();https://stackoverflow.com/questions/74003367
复制相似问题