5

我正在使用 Couchbase java 客户端 SDK 2.7.9,在尝试运行自动化集成测试时遇到了问题。在这样的测试中,我们通常使用随机端口来在同一个 Jenkins slave 上运行相同的东西(例如使用 docker)。

但是,对于客户端,我们可以指定许多自定义端口,但不能指定 8092、8093、8094 和 8095。

流行的 TestContainers 模块也提到这些端口必须在其 Couchbase 模块中保持静态:https ://www.testcontainers.org/modules/databases/couchbase/ 1

显然,也可以在服务器级别更改这些端口。

例子:

Docker-compose.yml

version: '3.0'
services:
  rapid_test_cb:
    build:
      context: ""
      dockerfile: cb.docker
    ports:
      - "8091"
      - "8092"
      - "8093"
      - "11210"

泊坞窗图像是 'couchbase:community-5.1.1'</p>

内部端口是上面写的端口,但在外部它们是随机的。在客户端级别,您可以设置 bootstrapHttpDirectPort 和 bootstrapCarrierDirectPort 但显然 8092 和 8093 端口是从服务器端获取的(谁不知道分配给他的端口)。

我想问您是否可以在客户端级别更改这些端口,如果没有,是否可以认真考虑添加该功能。

4

1 回答 1

5

因此,正如此处与 Couchbase 团队讨论的那样,

这是不可能的。所以我们找到了一种使用 Gradle 的 docker compose 插件使其工作的方法,但我想它可以在不同的情况下工作(TestContainer 可以使用类似的系统)。

码头工人-compose.yml:

version: '3.0'
services:
  rapid_test_cb:
    build:
      context: ""
      dockerfile: cb.docker
    ports:
      - "${COUCHBASE_RANDOM_PORT_8091}:${COUCHBASE_RANDOM_PORT_8091}"
      - "${COUCHBASE_RANDOM_PORT_8092}:${COUCHBASE_RANDOM_PORT_8092}"
      - "${COUCHBASE_RANDOM_PORT_8093}:${COUCHBASE_RANDOM_PORT_8093}"
      - "${COUCHBASE_RANDOM_PORT_11210}:${COUCHBASE_RANDOM_PORT_11210}"
    environment:
      COUCHBASE_RANDOM_PORT_8091: ${COUCHBASE_RANDOM_PORT_8091}
      COUCHBASE_RANDOM_PORT_8092: ${COUCHBASE_RANDOM_PORT_8092}
      COUCHBASE_RANDOM_PORT_8093: ${COUCHBASE_RANDOM_PORT_8093}
      COUCHBASE_RANDOM_PORT_11210: ${COUCHBASE_RANDOM_PORT_11210}

cb.docker:

FROM couchbase:community-5.1.1
COPY configure-node.sh /opt/couchbase
#HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost:8091/pools || exit 1
RUN chmod u+x /opt/couchbase/configure-node.sh
RUN echo "{rest_port, 8091}.\n{query_port, 8093}.\n{memcached_port, 11210}." >> /opt/couchbase/etc/couchbase/static_config
CMD ["/opt/couchbase/configure-node.sh"]

配置节点.sh:

#!/bin/bash

poll() {
  # The argument supplied to the function is invoked using "$@", we check the return value with $?
  "$@"
  while [ $? -ne 0 ]
    do
      echo 'waiting for couchbase to start'
      sleep 1
      "$@"
  done
}

set -x
set -m
if [[ -n "${COUCHBASE_RANDOM_PORT_8092}" ]]; then
    sed -i "s|8092|${COUCHBASE_RANDOM_PORT_8092}|g" /opt/couchbase/etc/couchdb/default.d/capi.ini
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_8091}" ]]; then
    sed -i "s|8091|${COUCHBASE_RANDOM_PORT_8091}|g" /opt/couchbase/etc/couchbase/static_config
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_8093}" ]]; then
    sed -i "s|8093|${COUCHBASE_RANDOM_PORT_8093}|g" /opt/couchbase/etc/couchbase/static_config
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_11210}" ]]; then
    sed -i "s|11210|${COUCHBASE_RANDOM_PORT_11210}|g" /opt/couchbase/etc/couchbase/static_config
fi

/entrypoint.sh couchbase-server &
poll curl -s localhost:${COUCHBASE_RANDOM_PORT_8091:-8091}

# Setup index and memory quota
curl -v -X POST http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/pools/default --noproxy '127.0.0.1' -d memoryQuota=300 -d indexMemoryQuota=300
# Setup services
curl -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/node/controller/setupServices --noproxy '127.0.0.1' -d services=kv%2Cn1ql%2Cindex
# Setup credentials
curl -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/settings/web --noproxy '127.0.0.1' -d port=${couchbase_random_port_8091:-8091} -d username=Administrator -d password=password
# Load the rapid_test bucket
curl -X POST -u Administrator:password -d name=rapid_test -d ramQuotaMB=128 --noproxy '127.0.0.1' -d authType=sasl -d saslPassword=password -d replicaNumber=0 -d flushEnabled=1 -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/pools/default/buckets

fg 1

Gradle 的 docker compose 配置:

def findRandomOpenPortOnAllLocalInterfaces = {
    new ServerSocket(0).withCloseable { socket ->
        return socket.getLocalPort().intValue()
    }
}

dockerCompose {
    environment.put 'COUCHBASE_RANDOM_PORT_8091', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_8092', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_8093', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_11210', findRandomOpenPortOnAllLocalInterfaces()
}
integTest.doFirst {
    systemProperty 'com.couchbase.bootstrapHttpDirectPort', couchbase_random_port_8091
    systemProperty 'com.couchbase.bootstrapCarrierDirectPort', couchbase_random_port_11210
}
于 2019-12-11T00:51:36.433 回答