Cassandra Springboot Spring Data 中的多个键空间,同时为每个键空间设置凭据

发布于 2025-01-11 22:22:19 字数 5421 浏览 5 评论 0原文

我正在尝试为多个密钥空间配置 cassandra ,我需要为 cassandra 设置用户名和密码,覆盖会话函数,这会给我一个身份验证错误,即使我可以找到在代码中打印的凭据。

我尝试了上面提到的不同方法,但在添加凭据时我无法弄清楚。

我收到此身份验证错误

Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 3 nodes, use getAllErrors() for more): Node(endPoint=10.0.213.69:9042, hostId=null, hashCode=6453355e): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.213.69:9042: Node 10.0.213.69:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured], Node(endPoint=10.0.211.74:9042, hostId=null, hashCode=6a94c67c): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.211.74:9042: Node 10.0.211.74:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured], Node(endPoint=10.0.208.57:9042, hostId=null, hashCode=606cad0c): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.208.57:9042: Node 10.0.208.57:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured]

我有一个像这样的基本配置

abstract class CassandraBaseConfig :
  AbstractCassandraConfiguration() {

  override fun getSchemaAction(): SchemaAction {
    return SchemaAction.CREATE_IF_NOT_EXISTS
  }

  override fun getSessionBuilderConfigurer(): SessionBuilderConfigurer? {
    return SessionBuilderConfigurer { sessionBuilder: CqlSessionBuilder ->
      val builder: ProgrammaticDriverConfigLoaderBuilder = DefaultProgrammaticDriverConfigLoaderBuilder(
        {
          ConfigFactory.invalidateCaches()
          ConfigFactory.defaultOverrides()
            .withFallback(buildConfig())
            .withFallback(ConfigFactory.defaultReference())
            .resolve()
        },
        DefaultDriverConfigLoader.DEFAULT_ROOT_PATH
      ).withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(60))
        .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(180))
        .withDuration(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, Duration.ofSeconds(60))
      sessionBuilder.withConfigLoader(builder.build())
      return@SessionBuilderConfigurer sessionBuilder
    }
  }

  private companion object CassandraDriverOptions {
    private val options: MutableMap<String, Any> = LinkedHashMap()
    fun addOption(option: DriverOption, value: Any): CassandraDriverOptions {
      val key = createKeyFor(option)
      options[key] = value
      return this
    }

    fun buildConfig() = ConfigFactory.parseMap(options, "Environment")

    private fun createKeyFor(option: DriverOption) = "${DefaultDriverConfigLoader.DEFAULT_ROOT_PATH}.${option.path}"
  }
}

和两个我像这样配置的密钥空间

@Configuration
@EnableCassandraRepositories(
  basePackages = ["com.expediagroup.dataquality.api.repository.cassandra.egdq"],
  cassandraTemplateRef = "cassandraEgdqPropertiesTemplate"
)
open class CassandraEgdqAppUser(
  private val cassandraEgdqProperties: CassandraEgdqProperties
) :
  CassandraBaseConfig() {

  override fun getKeyspaceName() = cassandraEgdqProperties.keySpace

  override fun getContactPoints() = cassandraEgdqProperties.contactPoints

  override fun getPort() = cassandraEgdqProperties.portNumber

  override fun getLocalDataCenter() = cassandraEgdqProperties.dataCenterName

  @Bean("cassandraEgdqPropertiesSession")
  open fun session(): CqlSessionFactoryBean {
    val cassandraSession = super.cassandraSession()
    cassandraSession.setUsername(cassandraEgdqProperties.username)
    cassandraSession.setPassword(cassandraEgdqProperties.password)
    return cassandraSession
  }

  @Bean("cassandraEgdqPropertiesTemplate")
  @Throws(Exception::class)
  open fun cassandraTemplate(
    @Qualifier("cassandraEgdqPropertiesSession") session: CqlSessionFactoryBean
  ): CassandraAdminOperations {
    return CassandraAdminTemplate(session.getObject(), cassandraConverter())
  }
}

第二个密钥空间

@Configuration
@EnableCassandraRepositories(
  basePackages = ["com.expediagroup.dataquality.api.repository.cassandra.egdqprofiles"],
  cassandraTemplateRef = "cassandraPropertiesTemplate"
)
open class CassandraEgdqProfilesAppUser(
  private val cassandraProperties: CassandraProperties
) : CassandraBaseConfig() {

  override fun getKeyspaceName() = cassandraProperties.keySpace

  override fun getContactPoints() = cassandraProperties.contactPoints

  override fun getPort() = cassandraProperties.portNumber

  override fun getLocalDataCenter() = cassandraProperties.dataCenterName


  @Bean("cassandraPropertiesSession")
  // @Primary
  open fun session(): CqlSessionFactoryBean {
    val cassandraSession = super.cassandraSession()
    cassandraSession.setUsername(cassandraProperties.username)
    cassandraSession.setPassword(cassandraProperties.password)
    return cassandraSession
  }

  @Bean("cassandraPropertiesTemplate")
  @Throws(Exception::class)
  open fun cassandraTemplate(
    @Qualifier("cassandraPropertiesSession") session: CqlSessionFactoryBean
  ): CassandraAdminOperations {
    return CassandraAdminTemplate(session.getObject(), cassandraConverter())
  }
}

任何帮助表示赞赏。

I am trying to configure cassandra for multiple keyspaces , I need to set userName and Password for cassandra, overriding session function which gives me an authentication error, even though I can find the credentials getting printed in code.

I tried with different approaches mentioned on SO, I haven't been able to figure things out while adding credentials.

I get this Authentication error

Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 3 nodes, use getAllErrors() for more): Node(endPoint=10.0.213.69:9042, hostId=null, hashCode=6453355e): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.213.69:9042: Node 10.0.213.69:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured], Node(endPoint=10.0.211.74:9042, hostId=null, hashCode=6a94c67c): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.211.74:9042: Node 10.0.211.74:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured], Node(endPoint=10.0.208.57:9042, hostId=null, hashCode=606cad0c): [com.datastax.oss.driver.api.core.auth.AuthenticationException: Authentication error on node 10.0.208.57:9042: Node 10.0.208.57:9042 requires authentication (org.apache.cassandra.auth.PasswordAuthenticator), but no authenticator configured]

I have a base config like this

abstract class CassandraBaseConfig :
  AbstractCassandraConfiguration() {

  override fun getSchemaAction(): SchemaAction {
    return SchemaAction.CREATE_IF_NOT_EXISTS
  }

  override fun getSessionBuilderConfigurer(): SessionBuilderConfigurer? {
    return SessionBuilderConfigurer { sessionBuilder: CqlSessionBuilder ->
      val builder: ProgrammaticDriverConfigLoaderBuilder = DefaultProgrammaticDriverConfigLoaderBuilder(
        {
          ConfigFactory.invalidateCaches()
          ConfigFactory.defaultOverrides()
            .withFallback(buildConfig())
            .withFallback(ConfigFactory.defaultReference())
            .resolve()
        },
        DefaultDriverConfigLoader.DEFAULT_ROOT_PATH
      ).withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(60))
        .withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(180))
        .withDuration(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, Duration.ofSeconds(60))
      sessionBuilder.withConfigLoader(builder.build())
      return@SessionBuilderConfigurer sessionBuilder
    }
  }

  private companion object CassandraDriverOptions {
    private val options: MutableMap<String, Any> = LinkedHashMap()
    fun addOption(option: DriverOption, value: Any): CassandraDriverOptions {
      val key = createKeyFor(option)
      options[key] = value
      return this
    }

    fun buildConfig() = ConfigFactory.parseMap(options, "Environment")

    private fun createKeyFor(option: DriverOption) = "${DefaultDriverConfigLoader.DEFAULT_ROOT_PATH}.${option.path}"
  }
}

And two keyspaces which I configure like this

@Configuration
@EnableCassandraRepositories(
  basePackages = ["com.expediagroup.dataquality.api.repository.cassandra.egdq"],
  cassandraTemplateRef = "cassandraEgdqPropertiesTemplate"
)
open class CassandraEgdqAppUser(
  private val cassandraEgdqProperties: CassandraEgdqProperties
) :
  CassandraBaseConfig() {

  override fun getKeyspaceName() = cassandraEgdqProperties.keySpace

  override fun getContactPoints() = cassandraEgdqProperties.contactPoints

  override fun getPort() = cassandraEgdqProperties.portNumber

  override fun getLocalDataCenter() = cassandraEgdqProperties.dataCenterName

  @Bean("cassandraEgdqPropertiesSession")
  open fun session(): CqlSessionFactoryBean {
    val cassandraSession = super.cassandraSession()
    cassandraSession.setUsername(cassandraEgdqProperties.username)
    cassandraSession.setPassword(cassandraEgdqProperties.password)
    return cassandraSession
  }

  @Bean("cassandraEgdqPropertiesTemplate")
  @Throws(Exception::class)
  open fun cassandraTemplate(
    @Qualifier("cassandraEgdqPropertiesSession") session: CqlSessionFactoryBean
  ): CassandraAdminOperations {
    return CassandraAdminTemplate(session.getObject(), cassandraConverter())
  }
}

Second keyspace

@Configuration
@EnableCassandraRepositories(
  basePackages = ["com.expediagroup.dataquality.api.repository.cassandra.egdqprofiles"],
  cassandraTemplateRef = "cassandraPropertiesTemplate"
)
open class CassandraEgdqProfilesAppUser(
  private val cassandraProperties: CassandraProperties
) : CassandraBaseConfig() {

  override fun getKeyspaceName() = cassandraProperties.keySpace

  override fun getContactPoints() = cassandraProperties.contactPoints

  override fun getPort() = cassandraProperties.portNumber

  override fun getLocalDataCenter() = cassandraProperties.dataCenterName


  @Bean("cassandraPropertiesSession")
  // @Primary
  open fun session(): CqlSessionFactoryBean {
    val cassandraSession = super.cassandraSession()
    cassandraSession.setUsername(cassandraProperties.username)
    cassandraSession.setPassword(cassandraProperties.password)
    return cassandraSession
  }

  @Bean("cassandraPropertiesTemplate")
  @Throws(Exception::class)
  open fun cassandraTemplate(
    @Qualifier("cassandraPropertiesSession") session: CqlSessionFactoryBean
  ): CassandraAdminOperations {
    return CassandraAdminTemplate(session.getObject(), cassandraConverter())
  }
}

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2025-01-18 22:22:19

为了解决第一个问题,我认为我在配置过程中使用了重叠的前缀。

接下来,我之前使用 super.cassandraSession() 修改了会话,而不是使用

val session = CqlSessionFactoryBean()
    session.setKeyspaceName(cassandraProperties.keySpace)
    session.setContactPoints(contactPoints)
    session.setPort(cassandraProperties.portNumber)
    session.setUsername(cassandraProperties.username)
    session.setPassword(cassandraProperties.password)
    session.setLocalDatacenter(cassandraProperties.dataCenterName)
    session.setSessionBuilderConfigurer { cqlSessionBuilder ->
      cqlSessionBuilder.withConfigLoader(getDriverConfigs())
    }

fun getDriverConfigs(cassandraProperties: CassandraProperties): DriverConfigLoader {
    val driverConfigs = OptionsMap.driverDefaults()
    driverConfigs.put(TypedDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(cassandraProperties.requestTimeoutMs))
    return DriverConfigLoader.fromMap(driverConfigs)
}

然后我需要在 yaml 文件中排除 CassandraDataAutoConfiguration

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration

To get rid of the first issue I figured I had overlapping prefixes I was using during configuration.

Next I modified session earlier using super.cassandraSession() to instead use

val session = CqlSessionFactoryBean()
    session.setKeyspaceName(cassandraProperties.keySpace)
    session.setContactPoints(contactPoints)
    session.setPort(cassandraProperties.portNumber)
    session.setUsername(cassandraProperties.username)
    session.setPassword(cassandraProperties.password)
    session.setLocalDatacenter(cassandraProperties.dataCenterName)
    session.setSessionBuilderConfigurer { cqlSessionBuilder ->
      cqlSessionBuilder.withConfigLoader(getDriverConfigs())
    }

fun getDriverConfigs(cassandraProperties: CassandraProperties): DriverConfigLoader {
    val driverConfigs = OptionsMap.driverDefaults()
    driverConfigs.put(TypedDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(cassandraProperties.requestTimeoutMs))
    return DriverConfigLoader.fromMap(driverConfigs)
}

Then I needed to exclude CassandraDataAutoConfiguration in my yaml file

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文