【发布时间】:2021-09-05 22:29:26
【问题描述】:
我创建了一个最小、完整且可验证的示例来说明我的问题:https://github.com/Virkom/CouchbaseMCVE.
我使用 Testcontainers 和 CouchbaseContainer 创建了一个集成测试。 我的 gradle.build:
implementation "com.couchbase.client:java-client:3.1.3"
testImplementation "io.micronaut.test:micronaut-test-junit5"
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:testcontainers"
testImplementation "org.testcontainers:couchbase"
Couchbase 客户端:
ClusterEnvironment env = ClusterEnvironment.builder()
.transcoder(SerializableTranscoder.INSTANCE)
.aggregatingMeterConfig(AggregatingMeterConfig.builder()
.enabled(true)
.emitInterval(Duration.ofSeconds(60)))
.build();
ClusterOptions opts = ClusterOptions.clusterOptions(bucket, password).environment(env);
couchbaseCluster = Cluster.connect("localhost", opts);
couchbaseBucket = couchbaseCluster.bucket(bucket);
当bucket为“testBucket”,密码为“testtest”时。
测试中的容器创建代码:
BucketDefinition bucketDefinition = new BucketDefinition("testBucket");
CouchbaseContainer couchbaseContainer = new CouchbaseContainer("couchbase/server")
.withCredentials("testBucket", "testtest")
.withBucket(bucketDefinition);
couchbaseContainer.start();
容器启动,我可以通过webinterface连接它,testBucket存在并且一切都很好,但是当我尝试更新值时出现异常。
方法代码:
public void set(String key, Serializable o, int ttlSeconds) {
UpsertOptions opts = UpsertOptions.upsertOptions()
.durability(PersistTo.NONE, ReplicateTo.NONE)
.expiry(Duration.ofSeconds(ttlSeconds));
couchbaseBucket.defaultCollection().upsert(key, o, opts);
}
结果:
UpsertRequest, Reason: TIMEOUT
com.couchbase.client.core.error.AmbiguousTimeoutException: UpsertRequest, Reason: TIMEOUT {"cancelled":true,"completed":true,"coreId":"0x48ac5a3200000001","idempotent":false,"reason":"TIMEOUT","requestId":5,"requestType":"UpsertRequest","retried":14,"retryReasons":["BUCKET_OPEN_IN_PROGRESS"],"service":{"bucket":"testBucket","collection":"_default","documentId":"0","opaque":"0x3","scope":"_default","type":"kv"},"timeoutMs":2500,"timings":{"encodingMicros":1434,"totalMicros":8118167}}
我在终端中也有很多警告,例如:
12:54:15.896 [cb-events] WARN com.couchbase.endpoint - [com.couchbase.endpoint][EndpointConnectionFailedEvent][948us] Connect attempt 9 failed because of AnnotatedConnectException: finishConnect(..) failed: connection refused: localhost/127.0.0.1:8091 {"circuitBreaker":"DISABLED","coreId":"0x48ac5a3200000001","remote":"localhost:8091","type":"MANAGER"}
com.couchbase.client.core.deps.io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: connection refused: localhost/127.0.0.1:8091
Caused by: java.net.ConnectException: finishConnect(..) failed: connection refused
我花了很多时间寻找原因,尝试使用 couchbase 容器端口 couchbaseCluster = Cluster.connect(serverIp + ":" + serverPort, opts); 连接到集群,并尝试使用暴露端口 .withExposedPorts(8091, 8092, 8093, 8094, 11207, 11210, 11211, 18091, 18092, 18093) 创建容器,但它不起作用。有人可以帮帮我吗?
【问题讨论】:
-
你能发布你的测试的完整源代码吗(或者更好——MCVE)?
-
是的,当然。我已经创建了示例项目:github.com/Virkom/CouchbaseMCVE
-
你关注these instructions了吗?
-
是的,我是从这本手册开始的。但它适用于过时版本的 couchbase (2.6.2)。我在项目中有 3.1.3,我不能像那样更改它。所以在测试部分创建容器是一样的,但是我不能使用 CouchbaseEnvironment、DefaultCouchbaseEnvironment 类和
CouchbaseCluster.create()方法 -
couchbase的测试容器在哪个端口启动?
标签: java junit couchbase micronaut testcontainers