我正在尝试使用测试容器来运行针对Docker容器中启动的HBASE的集成测试。我遇到的问题可能与客户端与HBASE互动的方式有些独特。
当HBase主启动在容器中时,它将存储其主机名:端口在Zookeeper中,以便客户端可以找到它。在这种情况下,它存储" Localhost:16000"。
在我的测试案例中运行的容器外,客户端从Zookeeper中检索" Localhost:16000",并且无法连接。该连接失败,因为端口已通过测试范围已将其重新映射到其他随机端口,除了16000。
有什么想法如何克服这个?
(1(一个想法是找到一种方法来告诉HBase客户端使用重新装修的端口,忽略从Zookeeper检索的值,但我还没有找到一种方法来做到这一点。
(2(如果我可以让HBase主人写入外部可访问的主机:Zookeeper中的端口,这也可以解决问题。但是我认为容器本身对测试容器如何进行端口重新映射有任何了解。
(3(也许有一个不同的解决方案测试容器为这种情况提供的解决方案?
您可以查看Kafkacontainer的实现,在该实现中,我们首先启动SOCAT(快速TCP代理(容器,以获取半随机端口并以后使用它来配置目标容器。p>算法是:
- 在
doStart
中,首先启动SOCAT针对原始容器的网络别名&端口像12345 - 获取映射端口(将是32109指向12345(
- 使原始容器(例如,使用环境变量(使用映射的端口,除了原始容器,或者,如果只能配置一个端口,请参阅更高级选项的Couchbasecontainer
- 返回Socat的主持人&端口到客户端
我们构建了HBase的新图像,以符合测试容器。
使用此图像:
docker run -eenv hbase_master_port = 16000 -env hbase_region_port = 16020 jcjabouille/hbase-standalone:2.4.9
然后创建此容器(在此处的Scala中(
private[test] class GenericHbase2Container
extends GenericContainer[GenericHbase2Container](
DockerImageName.parse("jcjabouille/hbase-standalone:2.4.9")
) {
private val randomMasterPort: Int = FreePortFinder.findFreeLocalPort(18000)
private val randomRegionPort: Int = FreePortFinder.findFreeLocalPort(20000)
private val hostName: String = InetAddress.getLocalHost.getHostName
val hbase2Configuration: Configuration = HBaseConfiguration.create
addExposedPort(randomMasterPort)
addExposedPort(randomRegionPort)
addExposedPort(2181)
withCreateContainerCmdModifier { cmd: CreateContainerCmd =>
cmd.withHostName(hostName)
()
}
waitingFor(Wait.forLogMessage(".*0 row.*", 1))
withStartupTimeout(Duration.ofMinutes(10))
withEnv("HBASE_MASTER_PORT", randomMasterPort.toString)
withEnv("HBASE_REGION_PORT", randomRegionPort.toString)
setPortBindings(Seq(s"$randomMasterPort:$randomMasterPort", s"$randomRegionPort:$randomRegionPort").asJava)
override protected def doStart(): Unit = {
super.doStart()
hbase2Configuration.set("hbase.client.pause", "200")
hbase2Configuration.set("hbase.client.retries.number", "10")
hbase2Configuration.set("hbase.rpc.timeout", "3000")
hbase2Configuration.set("hbase.client.operation.timeout", "3000")
hbase2Configuration.set("hbase.client.scanner.timeout.period", "10000")
hbase2Configuration.set("zookeeper.session.timeout", "10000")
hbase2Configuration.set("hbase.zookeeper.quorum", "localhost")
hbase2Configuration.set("hbase.zookeeper.property.clientPort", getMappedPort(2181).toString)
}
}
更多详细信息:https://hub.docker.com/r/jcjabouille/hbase-andalone