使用yaml中的micronaut框架配置cassandra

htzpubme  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(387)

我使用的是micronaut框架,我正在尝试从 application.yml 对于标准测试用例,我可以配置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如何为这个方法提供配置?

30byixjq

30byixjq1#

新功能由@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
    }
}

相关问题