【问题标题】:Configure Cassandra with micronaut framework from yaml使用 yaml 中的 micronaut 框架配置 Cassandra
【发布时间】:2019-06-07 02:58:53
【问题描述】:

我正在使用 micronaut 框架,我正在尝试从 application.yml 配置 cassandra 数据访问 对于标准测试用例,我可以配置 datastax 驱动程序

cassandra:
    default:
        clusterName: "Test Cluster"
        contactPoint: "192.168.99.100"
        port: 9042
        maxSchemaAgreementWaitSeconds: 20
        ssl: false

但是我找不到方法来提供要与方法 .withCredentials 一起使用的配置

我看到https://github.com/micronaut-projects/micronaut-core/blob/dc8c423be1979817c9c8f53440f3b87e775523b2/configurations/cassandra/src/main/java/io/micronaut/configuration/cassandra/CassandraConfiguration.java中的实现

执行以下操作

 @ConfigurationBuilder(allowZeroArgs = true, prefixes = { "with", "add" })
    Cluster.Builder builder = Cluster.builder();

然而withCredentials方法需要2个参数https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Cluster.Builder.html#withCredentials-java.lang.String-java.lang.String-

public Cluster.Builder withCredentials(String username,
                                       String password)

向该方法提供配置的 yaml 方式是什么?

【问题讨论】:

  • 我们可能应该改进CassandraConfiguration 以使构建器可用于程序化定制。请随时报告此问题。
  • 感谢它的魅力,你的实现速度非常快!

标签: java cassandra micronaut


【解决方案1】:

通过@graeme-rocher 在https://github.com/micronaut-projects/micronaut-core/issues/1106 中添加的新功能,我能够做到以下几点:

import com.datastax.driver.core.Cluster
import io.micronaut.context.ApplicationContext
import io.micronaut.context.event.BeanCreatedEvent
import io.micronaut.context.event.BeanCreatedEventListener
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import javax.inject.Singleton

@Singleton
class ClusterBuilderListener implements BeanCreatedEventListener<Cluster.Builder> {
    private static final Logger LOG = LoggerFactory.getLogger(ClusterBuilderListener.class)

    @Override
    Cluster.Builder onCreated(BeanCreatedEvent<Cluster.Builder> event) {
        def builder = event.getBean()
        ApplicationContext applicationContext = (ApplicationContext) event.getSource()

        if(applicationContext.getEnvironment().getActiveNames().contains('pro') ){
            builder.withCredentials("username", "password")
        }
        return builder
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-20
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多