目前我正在为 Cassandra 的这个错误而苦恼:
Error creating bean with name 'cassandraSession' defined in class path resource [com/project/config/CassandraConfiguration.class]: Invocation of init method failed; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses
在部署并查看单个 Pod 日志后,我能够看到此错误。从使用 IAM 用户静态凭证切换到使用 IAM 用户凭证后,此问题出现,因此除了我处理身份验证的方式之外,配置几乎相同。
@EnableCassandraRepositories
@Configuration
class CassandraConfiguration : AbstractCassandraConfiguration() {
@Value("\${aws.region}")
private lateinit var regionText: String
@Value("aws.iam.user.arn")
private lateinit var assumeRoleArn: String
@Value("\${aws.cassandra.host}")
private lateinit var cassandraHost: String
@Value("\${AWS_KEYSPACE_NAME:keyspace}")
private lateinit var keySpaceName: String
@Value("\${AWS_KEYSPACE_NAME_SUFFIX:}")
private lateinit var keySpaceNameSuffix: String
override fun cassandraSession(): CqlSessionFactoryBean {
val awsCqlSessionFactoryBean = AWSCqlSessionFactoryBean(
regionText,
assumeRoleArn,
cassandraHost,
keySpaceName + keySpaceNameSuffix
)
awsCqlSessionFactoryBean.setKeyspaceName(keySpaceName + keySpaceNameSuffix)
return awsCqlSessionFactoryBean
}
override fun getContactPoints(): String {
return cassandraHost
}
override fun getKeyspaceName(): String {
return keySpaceName
}
override fun getPort(): Int {
return 9042
}
}
CQL Bean 正在使用 Cassandra 配置
class AWSCqlSessionFactoryBean(
private val regionName: String,
private val assumeRoleArn: String,
private val cassandraHost: String,
private val keySpaceName: String
) : CqlSessionFactoryBean() {
override fun buildSession(sessionBuilder: CqlSessionBuilder): CqlSession {
val contactPoints = listOf(
InetSocketAddress.createUnresolved(cassandraHost, 9042)
)
return CqlSession.builder()
.addContactPoints(contactPoints)
.withSslContext(SSLContext.getDefault())
.withLocalDatacenter(regionName)
.withKeyspace(keySpaceName)
.withConfigLoader(DriverConfigLoader.fromClasspath("cassandra.conf"))
.withAuthProvider(
SigV4AuthProvider(
getAWSCredientials(),
"region"
)
)
.build()
}
override fun buildSystemSession(sessionBuilder: CqlSessionBuilder): CqlSession {
return buildSession(sessionBuilder)
}
fun getAWSCredientials(): AwsCredentialsProvider {
val region = Region.of(regionName)
val stsClient: StsClient = StsClient.builder()
.credentialsProvider(WebIdentityTokenFileCredentialsProvider.create())
.region(region)
.build()
val assumeRoleRequest: AssumeRoleRequest = AssumeRoleRequest.builder()
.roleArn(assumeRoleArn)
.roleSessionName("my-service")
.build()
return StsAssumeRoleCredentialsProvider.builder()
.stsClient(stsClient)
.refreshRequest(Supplier<AssumeRoleRequest> { assumeRoleRequest })
部门
"org.springframework.data:spring-data-cassandra:3.0.1.RELEASE"
"org.springframework.data:spring-data-cassandra:5.2.6.RELEASE"
"software.aws.mcs:aws-sigv4-auth-cassandra-java-driver-plugin:4.0.9"
"io.github.acm19:aws-request-signing-apache-interceptor:2.3.1"
id("org.springframework.boot") version "2.3.0.RELEASE"
我现在很茫然,因为看起来我的配置都是正确的,但我不确定如何进一步调试。
密钥空间的 AWS 端口位于 SSL 9142 端口,但您引用的是 9042。