0

我正在使用 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/Cas​​sandraConfiguration.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 方式是什么?

4

1 回答 1

0

通过@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
    }
}
于 2019-01-18T16:19:20.800 回答