我在从我的Lagom环境连接到AWS托管Cassandra服务时遇到问题。以下是我所做的尝试,结果如何。
(1( 亚马逊提供了从Java代码连接到AWS MCS的说明:
https://docs.aws.amazon.com/fr_fr/mcs/latest/devguide/cqlsh.html#using_java_driver
说明的要点是,您需要安装一个证书,然后将其传递给JVM,如下所示:
-Djavax.net.ssl.trustStore=path_to_file/cassandra_truststore.jks
-Djavax.net.ssl.trustStorePassword=amazon
然后,您可以使用您选择的任何Cassandra Java驱动程序。我选择的是Lagom框架提供的DataStax驱动程序。
为此,我在build.sbt
中添加了以下内容:
javaOptions ++= Seq(
"-Djavax.net.ssl.trustStore=project/cassandra_truststore.jks",
"-Djavax.net.ssl.trustStorePassword=amazon"
)
// Must enable JVM forking to use javaOptions with runAll.
fork := true
(2( 在将我的Lagom应用程序部署到AWS之前,我想在开发模式下使用它,但将它连接到AWS MCS,而不是嵌入式Cassandra服务器。Lagom提供了在开发模式下执行此操作的说明:
https://www.lagomframework.com/documentation/1.6.x/scala/CassandraServer.html#Connecting-对空运行Cassandra实例
说明的要点是将以下行添加到build.sbt
:
lagomCassandraEnabled in ThisBuild := false
lagomUnmanagedServices in ThisBuild := Map("cas_native" -> "tcp://localhost:9042")
本例中的URI假设Cassandra服务器在localhost:9042
上运行。在我的情况下,我用cassandra.us-east-1.amazonaws.com:9142
代替了它。
(3( 尽管如此,当我运行sbt runAll
时,我在尝试访问AWS MCS:时会超时
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: cassandra.us-east-1.amazonaws.com/3.83.168.143:9142 (com.datastax.driver.core.exceptions.OperationTimedOutException: [cassandra.us-east-1.amazonaws.com/3.83.168.143:9142] Operation timed out))
(4( 我完全绕过Lagom,只写了一段非常简单的代码,就解决了这个问题,比如:
System.setProperty("javax.net.ssl.trustStore", "redacted_absolute_file_path/cassandra_truststore.jks")
System.setProperty("javax.net.ssl.trustStorePassword", "amazon")
val cluster = Cluster.builder.addContactPoint("cassandra.us-east-1.amazonaws.com").withPort(9142).build()
val session = cluster.connect()
session.close()
cluster.close()
这很简单。但同样的超时也会发生。我做错了什么?
通过查看AWS MCS Python文档找到了一个解决方案(Java文档明显没有提及此事(。事实证明,我确实需要配置MCS服务特定的凭据,然后在Lagom的application.conf
文件中提供它们,如下所示:
cassandra.default {
port = 9142
ssl.truststore {
path = "path/cassandra_truststore.jks"
password = "amazon"
}
authentication {
username = "service-specific username"
password = "service-specific password"
}
}
cassandra-journal {
port = ${cassandra.default.port}
ssl.truststore {
path = ${cassandra.default.ssl.truststore.path}
password = ${cassandra.default.ssl.truststore.password}
}
authentication {
username = ${cassandra.default.authentication.username}
password = ${cassandra.default.authentication.password}
}
}
cassandra-snapshot-store {
port = ${cassandra.default.port}
ssl.truststore {
path = ${cassandra.default.ssl.truststore.path}
password = ${cassandra.default.ssl.truststore.password}
}
authentication {
username = ${cassandra.default.authentication.username}
password = ${cassandra.default.authentication.password}
}
}
lagom.persistence.read-side.cassandra {
port = ${cassandra.default.port}
ssl.truststore {
path = ${cassandra.default.ssl.truststore.path}
password = ${cassandra.default.ssl.truststore.password}
}
authentication {
username = ${cassandra.default.authentication.username}
password = ${cassandra.default.authentication.password}
}
}