【问题标题】:TestContainers Scala Cassandra Random PortsTestContainers Scala Cassandra 随机端口
【发布时间】:2021-09-26 01:12:35
【问题描述】:

我正在使用 spark 和 cassandra 在 scala 中运行测试,对于测试我使用的是 testcontainers 并且由于某些工作原因我们没有使用 testcontainers 的 scala 变体,testcontainers 的问题是端口是随机分配的,我没有知道使用什么参数来获取端口,以便我可以连接

//code block
@RunWith(classOf[JUnitRunner])
class ConnectorSpec extends AnyFlatSpec
  with BeforeAndAfterAll{
  val container =  new CassandraContainer("cassandra:latest")
  container.withExposedPorts(9042)
  container.waitingFor(Wait.forListeningPort())
  container.start()

  val ip = container.getContainerIpAddress() //output localhost
  val port = container.getMappedPort(9042)
  val cluster = container.getCluster()
  val session = cluster.connect()


  session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH replication = {'class':'SimpleStrategy','replication_factor':'1'};")
  session.execute("CREATE TABLE IF NOT EXISTS test.eureka (id int PRIMARY KEY,  city varchar, role varchar);")

  session.execute("INSERT INTO test.tester (id,  city, role) VALUES (1, 'Jakarta', 'Devops')")
  session.execute("SELECT * FROM test.tester")

  assert(container.isRunning)

  val spark = SparkSession
    .builder()
    .appName("ReadCassandra")
    .master("local[*]")
    .getOrCreate()
  spark.setCassandraConf(CassandraConnectorConf.KeepAliveMillisParam.option(10000))
  spark.setCassandraConf(cluster.getClusterName(), CassandraConnectorConf.ConnectionHostParam.option("127.0.0.1"))

  val df = spark.read
    .format("org.apache.spark.sql.cassandra")
    .options(Map( "table" -> "tester", "keyspace" -> "test"))
    .load()

  df.show()


#sbt dependency
libraryDependencies += "org.testcontainers" % "cassandra" % "1.15.3" % Test

显示实际端口的输出

Testing started at 5:30 PM ...
Connected to the target VM, address: '127.0.0.1:53611', transport: 'socket'
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/kenneth/.cache/coursier/v1/https/repo1.maven.org/maven2/org/slf4j/slf4j-log4j12/1.7.16/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/kenneth/.cache/coursier/v1/https/repo1.maven.org/maven2/org/slf4j/slf4j-nop/1.7.30/slf4j-nop-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

【问题讨论】:

    标签: scala testcontainers


    【解决方案1】:

    您的代码中已经有了本地端口:

    val port = container.getMappedPort(9042)
    

    TestContainers 专门为此目的提供getMappedPort:它将为您提供映射到容器中作为参数指定的端口的本地端口(在您的情况下,本地端口映射到 Cassandra 容器中的 9042)。

    注意:您必须在调用 getMappedPort 之前启动容器,但这已经是您的情况了。

    【讨论】:

    • 我不断收到以下错误 ``` 异常或错误导致运行中止:无法在 {localhost:9042} 打开与 Cassandra 的本机连接 :: 无法到达任何联系点,请确保您提供了有效地址(显示前 1 个节点,更多信息请使用 getAllErrors()): Node(endPoint=localhost/127.0.0.1:9042, hostId=null, hashCode=72a5db29): [com.datastax.oss.driver .api.core.connection.ConnectionInitException: [s0|control|connecting...] 协议初始化请求,步骤 1 (OPTIONS): failed to send request (java.nio.channels.ClosedChannelException)] ```
    • 9042是你在容器内的端口,你应该在容器外使用getMappedPort给出的port
    • 嗨,我已经尝试了 9042 和端口的输出,似乎都不起作用,每当我运行测试时,我都会看到输出 Connected to the target VM, address: '127.0.0.1:53611', transport: 'socket',每次使用不同的端口。
    • “连接到目标VM”与testcontainers无关,直接忽略即可。
    猜你喜欢
    • 2023-03-22
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2017-07-22
    • 2021-08-12
    • 2015-03-18
    • 1970-01-01
    相关资源
    最近更新 更多